Registry / backports-weakref

backports-weakref

JSON →
library1.0.post1pypypi✓ verified 23d ago

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.

pip install backports-weakref
INSTALL
IMPORT
SIG · BACKPORTS-WEAKREF
B
backports-weakref
pythonv1.0.post1
Install
1.5s avg
Import
16ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.post1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.018s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.014s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

finalize
from backports.weakref import finalize

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.

import sys import os from backports.weakref import finalize class Resource: def __init__(self, name): self.name = name print(f"Resource '{self.name}' created.") def cleanup(self, message): print(f"Cleanup for '{self.name}': {message}") # Create a resource my_resource = Resource("DatabaseConnection") # Register a finalizer to clean up when my_resource is garbage collected # The finalizer ensures cleanup even if `my_resource` goes out of scope. finalizer = finalize(my_resource, my_resource.cleanup, "Closing connection...") print("Resource created, finalizer registered.") # Simulate removing the strong reference del my_resource # The finalizer will be called when the object is garbage collected. # Force garbage collection for demonstration (not typically needed in real code) import gc gc.collect() print("Program finished.")
Debug
Known issues
deprecatedThis 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.
fix
For Python 3.4+ environments, use `import weakref` directly, as the functionality is built-in.
affects: < 3.4 (relevant), >= 3.4 (superseded)
gotchaStandard 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`.
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.
affects: All Python versions (including those using this backport)
gotchaExceptions 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.
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.
affects: All Python versions using `weakref.finalize` (including this backport)
gotchaWhile 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.
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.
affects: Python 2.7, Python 3.0-3.3
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'backports'
The top-level 'backports' namespace package, which contains 'backports.weakref', is not installed or not accessible in the current Python environment.
fix
Ensure the package is installed using pip: `pip install backports-weakref`.
ImportError: No module named weakref
This error occurs when a module attempts to `from backports.weakref import finalize` but the `backports.weakref` subpackage is not found, often implying the `backports-weakref` package was not installed or is incorrectly configured in the environment.
fix
Install the package: `pip install backports-weakref`. If already installed, check your Python path and virtual environment.
AttributeError: module 'backports' has no attribute 'weakref'
The 'backports' namespace package is found, but the 'weakref' submodule or its contents are not correctly installed or recognized within it, possibly due to a corrupted installation or a path issue.
fix
Reinstall the package to ensure all components are correctly placed: `pip uninstall backports-weakref && pip install backports-weakref`.
ImportError: cannot import name 'finalize' from 'backports.weakref'
While the `backports.weakref` module is found, the specific name `finalize` cannot be imported, which might indicate a typo, an attempt to import from an older version of `backports.weakref` that didn't expose `finalize`, or an issue with the module's contents.
fix
Verify that `backports-weakref` is installed and up-to-date (`pip install --upgrade backports-weakref`). Also, double-check the import statement for typos: `from backports.weakref import finalize`.
Upgrade
Version history
1.0.post1latest on PyPI · released Sep 17, 2017
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
backports-weakref — pip install backports-weakref · libregistry