diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2015-05-23 22:24:10 +1000 |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2015-05-23 22:24:10 +1000 |
commit | d5cacbb1d9c3edc02bf0ba01702e7c06da5bc318 (patch) | |
tree | e92dda9e119e043482b0aa0ad1fdefff785d54c0 /Modules/xxmodule.c | |
parent | Issue #23086: Add start and stop arguments to the Sequence.index() mixin method. (diff) | |
download | cpython-d5cacbb1d9c3edc02bf0ba01702e7c06da5bc318.tar.gz cpython-d5cacbb1d9c3edc02bf0ba01702e7c06da5bc318.tar.bz2 cpython-d5cacbb1d9c3edc02bf0ba01702e7c06da5bc318.zip |
PEP 489: Multi-phase extension module initialization
Known limitations of the current implementation:
- documentation changes are incomplete
- there's a reference leak I haven't tracked down yet
The leak is most visible by running:
./python -m test -R3:3 test_importlib
However, you can also see it by running:
./python -X showrefcount
Importing the array or _testmultiphase modules, and
then deleting them from both sys.modules and the local
namespace shows significant increases in the total
number of active references each cycle. By contrast,
with _testcapi (which continues to use single-phase
initialisation) the global refcounts stabilise after
a couple of cycles.
Diffstat (limited to 'Modules/xxmodule.c')
-rw-r--r-- | Modules/xxmodule.c | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c index 0feff662d53..85230d9c976 100644 --- a/Modules/xxmodule.c +++ b/Modules/xxmodule.c @@ -334,26 +334,10 @@ static PyMethodDef xx_methods[] = { PyDoc_STRVAR(module_doc, "This is a template module just for instruction."); -/* Initialization function for the module (*must* be called PyInit_xx) */ - -static struct PyModuleDef xxmodule = { - PyModuleDef_HEAD_INIT, - "xx", - module_doc, - -1, - xx_methods, - NULL, - NULL, - NULL, - NULL -}; - -PyMODINIT_FUNC -PyInit_xx(void) +static int +xx_exec(PyObject *m) { - PyObject *m = NULL; - /* Due to cross platform compiler issues the slots must be filled * here. It's required for portability to Windows without requiring * C++. */ @@ -366,11 +350,6 @@ PyInit_xx(void) if (PyType_Ready(&Xxo_Type) < 0) goto fail; - /* Create the module and add the functions */ - m = PyModule_Create(&xxmodule); - if (m == NULL) - goto fail; - /* Add some symbolic constants to the module */ if (ErrorObject == NULL) { ErrorObject = PyErr_NewException("xx.error", NULL, NULL); @@ -389,8 +368,33 @@ PyInit_xx(void) if (PyType_Ready(&Null_Type) < 0) goto fail; PyModule_AddObject(m, "Null", (PyObject *)&Null_Type); - return m; + return 0; fail: Py_XDECREF(m); - return NULL; + return -1; +} + +static struct PyModuleDef_Slot xx_slots[] = { + {Py_mod_exec, xx_exec}, + {0, NULL}, +}; + +static struct PyModuleDef xxmodule = { + PyModuleDef_HEAD_INIT, + "xx", + module_doc, + 0, + xx_methods, + xx_slots, + NULL, + NULL, + NULL +}; + +/* Export function for the module (*must* be called PyInit_xx) */ + +PyMODINIT_FUNC +PyInit_xx(void) +{ + return PyModuleDef_Init(&xxmodule); } |