Skip to content

Commit 8d6ef03

Browse files
committed
Add tests for module globals.
1 parent 3651fe0 commit 8d6ef03

File tree

4 files changed

+140
-103
lines changed

4 files changed

+140
-103
lines changed

doc/sphinx/source/module_globals.rst

+16-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Setting and Getting Module Globals
1010

1111
This section describes how you create and access module globals from Python C Extensions.
1212

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:
1415

1516

1617
.. code-block:: c
@@ -31,13 +32,14 @@ These are the names of the objects that will appear in the Python module::
3132
Initialising Module Globals
3233
------------------------------------
3334

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
36+
access the module globals from C:
3537

3638
.. code-block:: c
3739
3840
static PyMethodDef cModuleGlobals_methods[] = {
39-
{"print", (PyCFunction)_print_globals, METH_NOARGS,
40-
"Access and print out th globals."
41+
{"print", (PyCFunction)print_globals, METH_NOARGS,
42+
"Access and print out the globals."
4143
},
4244
{NULL, NULL, 0, NULL} /* Sentinel */
4345
};
@@ -82,8 +84,8 @@ The module initialisation code is next, this uses the Python C API to create the
8284
if (PyModule_AddObject(m, NAME_LST, Py_BuildValue("[iii]", 66, 68, 73))) {
8385
goto except;
8486
}
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)) {
8789
goto except;
8890
}
8991
goto finally;
@@ -101,7 +103,7 @@ The dict is added in a separate C function merely for readability:
101103
/* Add a dict of {str : int, ...}.
102104
* Returns 0 on success, 1 on failure.
103105
*/
104-
int _add_map_to_module(PyObject *module) {
106+
int add_map_to_module(PyObject *module) {
105107
int ret = 0;
106108
PyObject *pMap = NULL;
107109
@@ -157,11 +159,13 @@ Once the module is built we can access the globals from Python as usual::
157159
Getting Module Globals From C
158160
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
159161

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``:
161165

162166
.. code-block:: c
163167
164-
static PyObject *_print_global_INT(PyObject *pMod) {
168+
static PyObject *print_global_INT(PyObject *pMod) {
165169
PyObject *ret = NULL;
166170
PyObject *pItem = NULL;
167171
long val;
@@ -202,7 +206,7 @@ Accessing Python module globals from C is a little bit more tedious as we are ge
202206
return ret;
203207
}
204208
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()``):
206210

207211
>>> import cModuleGlobals
208212
>>> cModuleGlobals.printINT()
@@ -214,7 +218,8 @@ From Python we would see this (C's ``_print_global_INT()`` is mapped to Python's
214218
Setting Module Globals From C
215219
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
216220

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:
218223

219224
.. code-block:: c
220225
@@ -241,5 +246,3 @@ This is similar to the get code above but using ``int PyDict_SetItemString(PyObj
241246
finally:
242247
return ret;
243248
}
244-
245-

setup.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@
6464
library_dirs = [os.getcwd(),], # path to .a or .so file(s)
6565
extra_compile_args=extra_compile_args,
6666
),
67-
# Extension(f"{PACKAGE_NAME}.cModuleGlobals", sources=['src/cpy/cModuleGlobals.c',],
68-
# include_dirs = ['/usr/local/include',], # os.path.join(os.getcwd(), 'include'),],
69-
# library_dirs = [os.getcwd(),], # path to .a or .so file(s)
70-
# extra_compile_args=extra_compile_args,
71-
# ),
67+
Extension(f"{PACKAGE_NAME}.cModuleGlobals", sources=['src/cpy/cModuleGlobals.c',],
68+
include_dirs = ['/usr/local/include',], # os.path.join(os.getcwd(), 'include'),],
69+
library_dirs = [os.getcwd(),], # path to .a or .so file(s)
70+
extra_compile_args=extra_compile_args,
71+
),
7272
# Extension(f"{PACKAGE_NAME}.cObj", sources=['src/cpy/cObjmodule.c',],
7373
# include_dirs = ['/usr/local/include',], # os.path.join(os.getcwd(), 'include'),],
7474
# library_dirs = [os.getcwd(),], # path to .a or .so file(s)

0 commit comments

Comments
 (0)