File: parameter_refcount.pyx

package info (click to toggle)
cython 3.1.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,932 kB
  • sloc: python: 92,172; ansic: 19,275; cpp: 1,407; xml: 1,031; javascript: 511; makefile: 373; sh: 223; sed: 11
file content (34 lines) | stat: -rw-r--r-- 1,244 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Py_REFCNT and _Py_REFCNT are the same, except _Py_REFCNT takes
# a raw pointer and Py_REFCNT takes a normal Python object
from cpython.ref cimport PyObject, _Py_REFCNT, Py_REFCNT

import sys

python_dict = {"abc": 123}
python_dict_refcount = Py_REFCNT(python_dict)


cdef owned_reference(object obj):
    refcount1 = Py_REFCNT(obj)
    print(f'Inside owned_reference initially: {refcount1}')
    another_ref_to_object = obj
    refcount2 = Py_REFCNT(obj)
    print(f'Inside owned_reference after new ref: {refcount2}')


cdef borrowed_reference(PyObject * obj):
    refcount1 = _Py_REFCNT(obj)
    print(f'Inside borrowed_reference initially: {refcount1}')
    another_ptr_to_object = obj
    refcount2 = _Py_REFCNT(obj)
    print(f'Inside borrowed_reference after new pointer: {refcount2}')
    # Casting to a managed reference to call a cdef function doesn't increase the count
    refcount3 = Py_REFCNT(<object>obj)
    print(f'Inside borrowed_reference with temporary managed reference: {refcount3}')
    # However calling a Python function may depending on the Python version and the number
    # of arguments.


print(f'Initial refcount: {python_dict_refcount}')
owned_reference(python_dict)
borrowed_reference(<PyObject *>python_dict)