@@ -15,7 +15,10 @@ See also :ref:`Remove functions <remove-funcs>`.
15
15
Borrowed references
16
16
===================
17
17
18
- CPython 3.7 has 36 functions and macros which return borrowed references.
18
+ CPython 3.7 has many functions and macros which return or use borrowed
19
+ references. For example, ``PyTuple_GetItem() `` returns a borrowed reference,
20
+ whereas ``PyTuple_SetItem() `` stores a borrowed reference (store an item into a
21
+ tuple without increasing the reference counter).
19
22
20
23
CPython contains ``Doc/data/refcounts.dat `` (file is edited manually) which
21
24
documents how functions handle reference count.
@@ -40,6 +43,7 @@ Functions
40
43
* ``Py_InitModule4() ``
41
44
* ``PyImport_GetModuleDict() ``
42
45
* ``PyList_GetItem() ``
46
+ * ``PyList_SetItem() ``
43
47
* ``PyMethod_Class() ``
44
48
* ``PyMethod_Function() ``
45
49
* ``PyMethod_Self() ``
@@ -50,20 +54,28 @@ Functions
50
54
* ``PySys_GetXOptions() ``
51
55
* ``PyThreadState_GetDict() ``
52
56
* ``PyTuple_GetItem() ``
57
+ * ``PyTuple_SetItem() ``
53
58
* ``PyWeakref_G
10000
etObject() ``
54
59
55
60
Macros
56
61
------
57
62
58
- * ``PyCell_GET() ``: access directly ``PyCellObject.ob_ref ``
59
- * ``PyList_GET_ITEM() ``: access directly ``PyListObject.ob_item ``
63
+ * ``PyCell_GET() ``
64
+ * ``PyList_GET_ITEM() ``
65
+ * ``PyList_SET_ITEM() ``
60
66
* ``PyMethod_GET_CLASS() ``
61
- * ``PyMethod_GET_FUNCTION() ``: access directly ``PyMethodObject.im_func ``
62
- * ``PyMethod_GET_SELF() ``: access directly ``PyMethodObject.im_self ``
63
- * ``PySequence_Fast_GET_ITEM() ``: use ``PyList_GET_ITEM() ``
64
- or ``PyTuple_GET_ITEM() ``
65
- * ``PyTuple_GET_ITEM() ``: access directly ``PyTupleObject.ob_item ``
66
- * ``PyWeakref_GET_OBJECT() ``: access directly ``PyWeakReference.wr_object ``
67
+ * ``PyMethod_GET_FUNCTION() ``
68
+ * ``PyMethod_GET_SELF() ``
69
+ * ``PySequence_Fast_GET_ITEM() ``
70
+ * ``PyTuple_GET_ITEM() ``
71
+ * ``PyTuple_SET_ITEM() ``
72
+ * ``PyWeakref_GET_OBJECT() ``
73
+ * ``Py_TYPE() ``
74
+
75
+ Border line:
76
+
77
+ * ``Py_SETREF() ``, ``Py_XSETREF() ``: the caller has to manually increment the
78
+ reference counter of the new value
67
79
68
80
Borrowed references: PyEval_GetFuncName()
69
81
=========================================
@@ -121,6 +133,17 @@ Don't leak the structures like ``PyObject`` or ``PyTupleObject`` to not
121
133
access directly fields, to not use fixed offset at the ABI level. Replace
122
134
macros with functions calls. PyPy already does this in its C API (``cpyext ``).
123
135
136
+ Example of macros:
137
+
138
+ * ``PyCell_GET() ``: access directly ``PyCellObject.ob_ref ``
139
+ * ``PyList_GET_ITEM() ``: access directly ``PyListObject.ob_item ``
140
+ * ``PyMethod_GET_FUNCTION() ``: access directly ``PyMethodObject.im_func ``
141
+ * ``PyMethod_GET_SELF() ``: access directly ``PyMethodObject.im_self ``
142
+ * ``PySequence_Fast_GET_ITEM() ``: use ``PyList_GET_ITEM() ``
143
+ or ``PyTuple_GET_ITEM() ``
144
+ * ``PyTuple_GET_ITEM() ``: access directly ``PyTupleObject.ob_item ``
145
+ * ``PyWeakref_GET_OBJECT() ``: access directly ``PyWeakReference.wr_object ``
146
+
124
147
PyType_Ready() and setting directly PyTypeObject fields
125
148
=======================================================
126
149
0 commit comments