summaryrefslogtreecommitdiff
path: root/docs/examples/userguide/language_basics/parameter_refcount.py
blob: 2b25915ba90017199214f115008d9415f6e67d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from __future__ import print_function

from cython.cimports.cpython.ref import PyObject

import sys

python_dict = {"abc": 123}
python_dict_refcount = sys.getrefcount(python_dict)

@cython.cfunc
def owned_reference(obj: object):
    refcount = sys.getrefcount(python_dict)
    print('Inside owned_reference: {refcount}'.format(refcount=refcount))

@cython.cfunc
def borrowed_reference(obj: cython.pointer(PyObject)):
    refcount = obj.ob_refcnt
    print('Inside borrowed_reference: {refcount}'.format(refcount=refcount))

def main():
    print('Initial refcount: {refcount}'.format(refcount=python_dict_refcount))
    owned_reference(python_dict)
    borrowed_reference(cython.cast(cython.pointer(PyObject), python_dict))