{"id":3421,"library":"backports-weakref","title":"backports-weakref","description":"This package provides backports of new features in Python's standard `weakref` module under the `backports` namespace. Its primary backported functionality is `weakref.finalize`, which was introduced in Python 3.4. The library, currently at version `1.0.post1`, was last released in September 2017 and is primarily useful for Python 2.7 and Python 3 versions up to 3.6, as newer Python versions include these features natively.","status":"maintenance","version":"1.0.post1","language":"en","source_language":"en","source_url":"https://github.com/pjdelport/backports.weakref","tags":["weak references","backport","memory management","garbage collection","python2","python3"],"install":[{"cmd":"pip install backports-weakref","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"finalize","correct":"from backports.weakref import finalize"}],"quickstart":{"code":"import sys\nimport os\nfrom backports.weakref import finalize\n\nclass Resource:\n    def __init__(self, name):\n        self.name = name\n        print(f\"Resource '{self.name}' created.\")\n\n    def cleanup(self, message):\n        print(f\"Cleanup for '{self.name}': {message}\")\n\n# Create a resource\nmy_resource = Resource(\"DatabaseConnection\")\n\n# Register a finalizer to clean up when my_resource is garbage collected\n# The finalizer ensures cleanup even if `my_resource` goes out of scope.\nfinalizer = finalize(my_resource, my_resource.cleanup, \"Closing connection...\")\n\nprint(\"Resource created, finalizer registered.\")\n\n# Simulate removing the strong reference\ndel my_resource\n\n# The finalizer will be called when the object is garbage collected.\n# Force garbage collection for demonstration (not typically needed in real code)\nimport gc\ngc.collect()\n\nprint(\"Program finished.\")","lang":"python","description":"This example demonstrates how to use the backported `finalize` to ensure a cleanup function is called when an object is garbage collected. This is particularly useful for managing external resources like file handles or network connections."},"warnings":[{"fix":"For Python 3.4+ environments, use `import weakref` directly, as the functionality is built-in.","message":"This library backports `weakref.finalize` (new in Python 3.4) and other features. For Python 3.4 and newer, these features are part of the standard `weakref` module, making this backport largely unnecessary for modern Python development.","severity":"deprecated","affected_versions":"< 3.4 (relevant), >= 3.4 (superseded)"},{"fix":"Ensure the object type supports weak references. Custom classes (without `__slots__` preventing `__weakref__`) and certain built-in types (e.g., instances of user-defined classes, functions) generally work.","message":"Standard Python built-in types such as `int`, `str`, `list`, `dict`, and `tuple` generally do not support weak references, even when subclassed. Attempting to create a weak reference to such an object will raise a `TypeError`.","severity":"gotcha","affected_versions":"All Python versions (including those using this backport)"},{"fix":"Ensure finalizer callbacks are robust and handle their own internal errors to prevent silent failures. Avoid complex or potentially error-prone logic within callbacks.","message":"Exceptions raised by `weakref.finalize` callbacks during garbage collection are typically caught and printed to `sys.stderr`, but they cannot be propagated or handled by standard `try...except` blocks in the main program flow.","severity":"gotcha","affected_versions":"All Python versions using `weakref.finalize` (including this backport)"},{"fix":"Check your Python version. For Python 3.4+, use the native `weakref.WeakMethod`. For older Python versions, if `WeakMethod` is required, a different dedicated backport or a custom implementation might be necessary.","message":"While the standard `weakref` module includes `WeakMethod` (from Python 3.4+), `backports.weakref` explicitly lists only `weakref.finalize` as its backported functionality. Users expecting `WeakMethod` might find it missing from this backport.","severity":"gotcha","affected_versions":"Python 2.7, Python 3.0-3.3"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}