8000 Py_TYPE · adamtheturtle/pythoncapi@431d36a · GitHub
[go: up one dir, main page]

Skip to content

Commit 431d36a

Browse files
committed
Py_TYPE
1 parent 8025306 commit 431d36a

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

doc/bad_api.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,31 @@ Macros
7373

7474
Border line:
7575

76-
* ``Py_TYPE()``
7776
* ``Py_SETREF()``, ``Py_XSETREF()``: the caller has to manually increment the
7877
reference counter of the new value
7978
* ``N`` format of ``Py_BuildValue()``?
8079

80+
81+
.. _py-type:
82+
83+
Py_TYPE() corner case
84+
=====================
85+
86+
Technically, ``Py_TYPE()`` returns a borrowed reference to a ``PyTypeObject*``.
87+
In pratice, for heap types, an instance holds already a strong reference
88+
to the type in ``PyObject.ob_type``. For static types, instances use a borrowed
89+
reference, but static types are never destroyed.
90+
91+
Hugh Fisher summarized:
92+
93+
It don't think it is worth forcing every C extension module to be rewritten,
94+
and incur a performance hit, to eliminate a rare bug from badly written
95+
code.
96+
97+
See the discussion on capi-sig: `Open questions about borrowed reference.
98+
<https://mail.python.org/mm3/archives/list/capi-sig@python.org/thread/V5EMBIIJFJGJGBQPLCFFXCHAUFNTA45H/>`_
99+
(Sept 2018).
100+
81101
Borrowed references: PyEval_GetFuncName()
82102
=========================================
83103

doc/new_api.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@ New functions
3434
reference on the item
3535
* ``PySequence_Fast_GetItemRef()``
3636
* ``PyStructSequence_SetItemRef()``
37-
* ``Py_GetType()``: similar to ``Py_TYPE()`` but returns a strong reference
38-
* ``Py_TYPE_IS(ob, type)``: equivalent to ``Py_TYPE(ob) == type``
39-
* ``%T`` format for ``PyUnicode_FromFormat()``
4037

4138
XXX private functions:
4239

4340
* ``_Py_SET_TYPE()``: see :ref:`Implement a PyTypeObject in C <impl-pytype>`
4441
* ``_Py_SET_SIZE()``
4542

43+
If we decide that :ref:`Py_TYPE() <py-type>` should go away, 3 more
44+
functions/features are needed:
45+
46+
* ``Py_GetType()``: similar to ``Py_TYPE()`` but returns a strong reference
47+
* ``Py_TYPE_IS(ob, type)``: equivalent to ``Py_TYPE(ob) == type``
48+
* ``%T`` format for ``PyUnicode_FromFormat()``
49+
50+
4651
Non-goal
4752
========
4853

0 commit comments

Comments
 (0)
0