10
10
Reference Counts and Python Containers
11
11
======================================
12
12
13
- Given the descriptions of *New *, *Stolen * and *Borrowed *
14
- references described in the preceeding chapter, this chapter looks
15
- in more detail of how the Python C API works with different containers.
13
+ The descriptions of *New *, *Stolen * and *Borrowed * references were described in the preceding chapter.
14
+ This chapter looks in more detail of how the Python C API works with different containers, tuple, list, set and dict.
16
15
17
- Of particular interest is *Setters *, *Getters * and the behaviour of
18
- `` Py_BuildValue `` .
16
+ Of particular interest is *Setters *, *Getters * and the behaviour of `` Py_BuildValue `` for each of those containers.
17
+ This chapter also clarifies the Python documentation where it is inaccurate or misleading .
19
18
20
19
Buckle up.
21
20
22
21
--------------------------
23
22
Methodology
24
23
--------------------------
25
24
26
- Firstly a utility function for creating new, uncached, Python objects :
25
+ This chapter explores the CPython C API in several ways :
27
26
28
- .. code-block :: c
29
-
30
- /* This is used to guarantee that Python is not caching a string value when we want to check the
31
- * reference counts after each string creation.
32
- * */
33
- static int debug_test_count = 0;
34
-
35
- static PyObject *
36
- new_unique_string(const char *function_name) {
37
- return PyUnicode_FromFormat("%s-%d", function_name, debug_test_count++);
38
- }
27
+ * Tests of the CPython C API that can be stepped through in the debugger.
28
+ This code is in ``src/cpy/Containers/DebugContainers.h `` and ``src/cpy/Containers/DebugContainers.c ``
29
+ and ``asserts `` are used to check the results, particularly reference counts.
30
+ It is exercised by ``src/main.c ``.
31
+ * Similar test code is in ``src/cpy/RefCount/cRefCount.c `` which is built into the Python module ``cRefCount ``.
32
+ This can be run under ``pytest ``.
33
+ * A review of the Python C API documentation.
39
34
40
35
Here is an example of exploring reference counts and tuples.
41
36
@@ -91,7 +86,7 @@ Here is an example of exploring reference counts and tuples.
91
86
Py_RETURN_NONE;
92
87
}
93
88
94
-
89
+ Firstly Tuples:
95
90
96
91
-----------------------
97
92
Tuple
@@ -116,6 +111,34 @@ The Python documentation for the `Tuple API <https://docs.python.org/3/c-api/tup
116
111
- Steals, leaks original.
117
112
- More stuff.
118
113
114
+
115
+ ``PyTuple_SetItem() ``
116
+ ---------------------
117
+
118
+ `PyTuple_SetItem() <https://docs.python.org/3/c-api/tuple.html#c.PyTuple_SetItem >`_
119
+
120
+
121
+ ``PyTuple_SET_ITEM() ``
122
+ ---------------------
123
+
124
+ `PyTuple_SetItem() <https://docs.python.org/3/c-api/tuple.html#c.PyTuple_SET_ITEM >`_
125
+
126
+
127
+ ``Py_BuildValue() ``
128
+ -------------------
129
+
130
+ `Py_BuildValue() <https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue >`_
131
+
132
+
133
+ ``PyTuple_Pack() ``
134
+ ------------------
135
+
136
+ `PyTuple_Pack() <https://docs.python.org/3/c-api/tuple.html#c.PyTuple_Pack >`_
137
+ is a wrapper around
138
+ `Py_BuildValue() <https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue >`_
139
+ so is not explored any further.
140
+
141
+
119
142
-----------------------
120
143
List
121
144
-----------------------
0 commit comments