You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/sphinx/source/module_globals.rst
+16-13
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,8 @@ Setting and Getting Module Globals
10
10
11
11
This section describes how you create and access module globals from Python C Extensions.
12
12
13
-
In this module, written as a Python extension in C, we are going to have a string, int, list, tuple and dict in global scope. In the C code we firstly define names for them:
13
+
In this module, written as a Python extension in C, we are going to have a string, int, list, tuple and dict in global
14
+
scope. In the C code we firstly define names for them:
14
15
15
16
16
17
.. code-block:: c
@@ -31,13 +32,14 @@ These are the names of the objects that will appear in the Python module::
31
32
Initialising Module Globals
32
33
------------------------------------
33
34
34
-
This is the module declaration, it will be called ``cModuleGlobals`` and has just one function; ``print()`` that will access the module globals from C:
35
+
This is the module declaration, it will be called ``cModuleGlobals`` and has just one function; ``print()`` that will
@@ -82,8 +84,8 @@ The module initialisation code is next, this uses the Python C API to create the
82
84
if (PyModule_AddObject(m, NAME_LST, Py_BuildValue("[iii]", 66, 68, 73))) {
83
85
goto except;
84
86
}
85
-
/* An invented convenience function for this dict. */
86
-
if (_add_map_to_module(m)) {
87
+
/* An invented convenience function for this dict. See below. */
88
+
if (add_map_to_module(m)) {
87
89
goto except;
88
90
}
89
91
goto finally;
@@ -101,7 +103,7 @@ The dict is added in a separate C function merely for readability:
101
103
/* Add a dict of {str : int, ...}.
102
104
* Returns 0 on success, 1 on failure.
103
105
*/
104
-
int _add_map_to_module(PyObject *module) {
106
+
int add_map_to_module(PyObject *module) {
105
107
int ret = 0;
106
108
PyObject *pMap = NULL;
107
109
@@ -157,11 +159,13 @@ Once the module is built we can access the globals from Python as usual::
157
159
Getting Module Globals From C
158
160
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
159
161
160
-
Accessing Python module globals from C is a little bit more tedious as we are getting borrowed references from the modules ``__dict__`` and we should increment and decrement them appropriately. Here we print out the global ``INT`` as both a Python object and a 'C' ``long``:
162
+
Accessing Python module globals from C is a little bit more tedious as we are getting borrowed references from the
163
+
modules ``__dict__`` and we should increment and decrement them appropriately.
164
+
Here we print out the global ``INT`` as both a Python object and a 'C' ``long``:
@@ -202,7 +206,7 @@ Accessing Python module globals from C is a little bit more tedious as we are ge
202
206
return ret;
203
207
}
204
208
205
-
From Python we would see this (C's ``_print_global_INT()`` is mapped to Python's ``cModuleGlobals.printINT()``):
209
+
From Python we would see this (C's ``print_global_INT()`` is mapped to Python's ``cModuleGlobals.printINT()``):
206
210
207
211
>>> import cModuleGlobals
208
212
>>> cModuleGlobals.printINT()
@@ -214,7 +218,8 @@ From Python we would see this (C's ``_print_global_INT()`` is mapped to Python's
214
218
Setting Module Globals From C
215
219
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
216
220
217
-
This is similar to the get code above but using ``int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)`` where val will be a *stolen* reference:
221
+
This is similar to the get code above but using ``int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)``
222
+
where val will be a *stolen* reference:
218
223
219
224
.. code-block:: c
220
225
@@ -241,5 +246,3 @@ This is similar to the get code above but using ``int PyDict_SetItemString(PyObj
0 commit comments