Skip to content

base

Base classes for reference counting.

Classes:

  • NativeHandle

    A base class for wrappers around otherwise "unmanaged" resources e.g. in a native library.

  • ReferenceCounter

    A base class for reference counters.

NativeHandle

NativeHandle(handle: Any = None, prior_ref_count: int = 0)

Bases: ReferenceCounter

A base class for wrappers around otherwise "unmanaged" resources e.g. in a native library.

Attributes:

  • _handle (object) –

    The handle (e.g. cffi pointer) to the native resource.

  • _finalizing (bool) –

    a flag telling whether this object is in its deletion phase. This has a use in some advanced cases with reverse callback, possibly not relevant in Python.

Parameters:

  • handle (object, default: None ) –

    The handle (e.g. cffi pointer) to the native resource.

  • prior_ref_count (int, default: 0 ) –

    the initial reference count. Default 0 if this NativeHandle is sole responsible for the lifecycle of the resource.

Methods:

  • add_ref

    Manually increment the reference count.

  • decrement_ref

    Manually increment the reference count.

Attributes:

Source code in src/refcount/base.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def __init__(self, handle: Any = None, prior_ref_count: int = 0):
    """Initialize a reference counter for a resource handle, with an initial reference count.

    Args:
        handle (object): The handle (e.g. cffi pointer) to the native resource.
        prior_ref_count (int): the initial reference count. Default 0 if this NativeHandle is sole responsible for the lifecycle of the resource.
    """
    super().__init__(prior_ref_count)
    # TODO checks
    self._finalizing: bool = False
    self._handle: Any = None
    if handle is None:
        return  # defer setting handle to the inheritor.
    self._set_handle(handle, prior_ref_count)

reference_count property

reference_count: int

Get the current reference count.

add_ref

add_ref() -> None

Manually increment the reference count.

Users usually have no need to call this method. They may have to if they manage cases where one native handle wrapper uses another wrapper (and its underlying resource).

Source code in src/refcount/base.py
26
27
28
29
30
31
32
def add_ref(self) -> None:
    """Manually increment the reference count.

    Users usually have no need to call this method. They may have to if they
    manage cases where one native handle wrapper uses another wrapper (and its underlying resource).
    """
    self._ref_count = self._ref_count + 1

decrement_ref

decrement_ref() -> None

Manually increment the reference count.

Users usually have no need to call this method. They may have to if they manage cases where one native handle wrapper uses another wrapper (and its underlying resource).

Source code in src/refcount/base.py
34
35
36
37
38
39
40
def decrement_ref(self) -> None:
    """Manually increment the reference count.

    Users usually have no need to call this method. They may have to if they
    manage cases where one native handle wrapper uses another wrapper (and its underlying resource).
    """
    self._ref_count = self._ref_count - 1

ReferenceCounter

ReferenceCounter(prior_ref_count: int = 0)

A base class for reference counters.

Attributes:

Parameters:

  • prior_ref_count (int, default: 0 ) –

    the initial reference count. Default 0 if this object is sole responsible for the lifecycle of the resource.

Methods:

  • add_ref

    Manually increment the reference count.

  • decrement_ref

    Manually increment the reference count.

Attributes:

Source code in src/refcount/base.py
13
14
15
16
17
18
19
def __init__(self, prior_ref_count: int = 0):
    """Initialize this with an initial reference count.

    Args:
        prior_ref_count (int): the initial reference count. Default 0 if this object is sole responsible for the lifecycle of the resource.
    """
    self._ref_count: int = prior_ref_count + 1

reference_count property

reference_count: int

Get the current reference count.

add_ref

add_ref() -> None

Manually increment the reference count.

Users usually have no need to call this method. They may have to if they manage cases where one native handle wrapper uses another wrapper (and its underlying resource).

Source code in src/refcount/base.py
26
27
28
29
30
31
32
def add_ref(self) -> None:
    """Manually increment the reference count.

    Users usually have no need to call this method. They may have to if they
    manage cases where one native handle wrapper uses another wrapper (and its underlying resource).
    """
    self._ref_count = self._ref_count + 1

decrement_ref

decrement_ref() -> None

Manually increment the reference count.

Users usually have no need to call this method. They may have to if they manage cases where one native handle wrapper uses another wrapper (and its underlying resource).

Source code in src/refcount/base.py
34
35
36
37
38
39
40
def decrement_ref(self) -> None:
    """Manually increment the reference count.

    Users usually have no need to call this method. They may have to if they
    manage cases where one native handle wrapper uses another wrapper (and its underlying resource).
    """
    self._ref_count = self._ref_count - 1