summaryrefslogtreecommitdiff
path: root/docs/examples/userguide/language_basics/parameter_refcount.py
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/userguide/language_basics/parameter_refcount.py')
-rw-r--r--docs/examples/userguide/language_basics/parameter_refcount.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/docs/examples/userguide/language_basics/parameter_refcount.py b/docs/examples/userguide/language_basics/parameter_refcount.py
new file mode 100644
index 000000000..2b25915ba
--- /dev/null
+++ b/docs/examples/userguide/language_basics/parameter_refcount.py
@@ -0,0 +1,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))