Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
3c4e11dd
Commit
3c4e11dd
authored
Jul 08, 2019
by
Alex Willmer
Committed by
Stefan Behnel
Jul 08, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Describe refcount behaviour of object vs PyObject* (GH-3013)
parent
70def42a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
0 deletions
+35
-0
docs/examples/userguide/language_basics/parameter_refcount.pyx
...examples/userguide/language_basics/parameter_refcount.pyx
+20
-0
docs/src/userguide/language_basics.rst
docs/src/userguide/language_basics.rst
+15
-0
No files found.
docs/examples/userguide/language_basics/parameter_refcount.pyx
0 → 100644
View file @
3c4e11dd
from
__future__
import
print_function
from
cpython.ref
cimport
PyObject
import
sys
python_dict
=
{
"abc"
:
123
}
python_dict_refcount
=
sys
.
getrefcount
(
python_dict
)
cdef
owned_reference
(
object
obj
):
refcount
=
sys
.
getrefcount
(
python_dict
)
print
(
'Inside owned_reference: {refcount}'
.
format
(
refcount
=
refcount
))
cdef
borrowed_reference
(
PyObject
*
obj
):
refcount
=
obj
.
ob_refcnt
print
(
'Inside borrowed_reference: {refcount}'
.
format
(
refcount
=
refcount
))
print
(
'Initial refcount: {refcount}'
.
format
(
refcount
=
python_dict_refcount
))
owned_reference
(
python_dict
)
borrowed_reference
(
<
PyObject
*>
python_dict
)
docs/src/userguide/language_basics.rst
View file @
3c4e11dd
...
...
@@ -288,6 +288,17 @@ In the interests of clarity, it is probably a good idea to always be explicit
about object parameters in C functions.
To create a borrowed reference, specify the parameter type as ``PyObject*``.
Cython won't perform automatic ``Py_INCREF``, or ``Py_DECREF``, e.g.:
.. literalinclude:: ../../examples/userguide/language_basics/parameter_refcount.pxd
will display::
Initial refcount: 2
Inside owned_reference: 3
Inside borrowed_reference: 2
.. _optional_arguments:
Optional Arguments
...
...
@@ -579,6 +590,10 @@ Here is an example:
The precedence of ``<...>`` is such that ``<type>a.b.c`` is interpreted as ``<type>(a.b.c)``.
Casting to ``<object>`` creates an owned reference. Cython will automatically
perform a ``Py_INCREF`` and ``Py_DECREF`` operation. Casting to
``<PyObject *>`` creates a borrowed reference, leaving the refcount unchanged.
.. _checked_type_casts:
Checked Type Casts
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment