trackref: use autodoc (#7771)

This commit is contained in:
Adrian 2026-07-24 13:24:54 +02:00 committed by GitHub
parent abbc024bbe
commit 0b578c1cbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 25 deletions

View File

@ -187,30 +187,13 @@ scrapy.utils.trackref module
Here are the functions available in the :mod:`~scrapy.utils.trackref` module.
.. class:: object_ref
.. autoclass:: object_ref
Inherit from this class if you want to track live
instances with the ``trackref`` module.
.. autofunction:: print_live_refs(ignore=NoneType)
.. function:: print_live_refs(ignore=NoneType)
.. autofunction:: get_oldest
Print a report of live references, grouped by class name.
:param ignore: if given, all objects from the specified class (or tuple of
classes) will be ignored.
:type ignore: type or tuple
.. function:: get_oldest(class_name)
Return the oldest object alive with the given class name, or ``None`` if
none is found. Use :func:`print_live_refs` first to get a list of all
tracked live objects per class name.
.. function:: iter_all(class_name)
Return an iterator over all objects alive with the given class name. Use
:func:`print_live_refs` first to get a list of all tracked live objects
per class name.
.. autofunction:: iter_all
.. skip: end

View File

@ -34,7 +34,8 @@ live_refs: defaultdict[type, WeakKeyDictionary[object, float]] = defaultdict(
class object_ref:
"""Inherit from this class to a keep a record of live instances"""
"""Inherit from this class if you want to track live instances with the
``trackref`` module."""
__slots__ = ()
@ -60,12 +61,19 @@ def format_live_refs(ignore: Any = NoneType) -> str:
def print_live_refs(*a: Any, **kw: Any) -> None:
"""Print tracked objects"""
"""Print a report of live references, grouped by class name.
:param ignore: if given, all objects from the specified class (or tuple of
classes) will be ignored.
:type ignore: type or tuple
"""
print(format_live_refs(*a, **kw))
def get_oldest(class_name: str) -> Any:
"""Get the oldest object for a specific class name"""
"""Return the oldest object alive with the given class name, or ``None`` if
none is found. Use :func:`print_live_refs` first to get a list of all
tracked live objects per class name."""
for cls, wdict in live_refs.items():
if cls.__name__ == class_name:
if not wdict:
@ -75,7 +83,9 @@ def get_oldest(class_name: str) -> Any:
def iter_all(class_name: str) -> Iterable[Any]:
"""Iterate over all objects of the same class by its class name"""
"""Return an iterator over all objects alive with the given class name. Use
:func:`print_live_refs` first to get a list of all tracked live objects per
class name."""
for cls, wdict in live_refs.items():
if cls.__name__ == class_name:
return wdict.keys()