[Python-checkins] cpython: Issue #15042: Add PyState_AddModule and PyState_RemoveModule.

martin.v.loewis python-checkins at python.org
Fri Jun 22 12:21:25 CEST 2012


http://hg.python.org/cpython/rev/55e8cba34b11
changeset:   77564:55e8cba34b11
user:        Martin v. Löwis <martin at v.loewis.de>
date:        Fri Jun 22 12:20:55 2012 +0200
summary:
  Issue #15042: Add PyState_AddModule and PyState_RemoveModule.
Add version  guard for Py_LIMITED_API additions.
Issue #15081: Document PyState_FindModule.
Patch by Robin Schreiber.

files:
  Doc/c-api/module.rst |  22 ++++++++++++++-
  Include/pystate.h    |   5 +++
  Misc/NEWS            |   6 ++++
  PC/python3.def       |   2 +
  Python/pystate.c     |  45 ++++++++++++++++++++++++++++++-
  5 files changed, 77 insertions(+), 3 deletions(-)


diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst
--- a/Doc/c-api/module.rst
+++ b/Doc/c-api/module.rst
@@ -113,8 +113,28 @@
 
    Return a pointer to the :c:type:`PyModuleDef` struct from which the module was
    created, or *NULL* if the module wasn't created with
-   :c:func:`PyModule_Create`.
+   :c:func:`PyModule_Create`.i
 
+.. c:function:: PyObject* PyState_FindModule(PyModuleDef *def)
+
+   Returns the module object that was created from *def* for the current interpreter.
+   This method requires that the module object has been attached to the interpreter state with
+   :c:func:`PyState_AddModule` beforehand. In case the corresponding module object is not
+   found or has not been attached to the interpreter state yet, it returns NULL.
+
+.. c:function:: int PyState_AddModule(PyModuleDef *def, PyObject *module)
+
+   Attaches the module object passed to the function to the interpreter state. This allows
+   the module object to be accessible via
+   :c:func:`PyState_FindModule`.
+
+   .. versionadded:: 3.3
+
+.. c:function:: int PyState_RemoveModule(PyModuleDef *def, PyObject *module)
+
+   Removes the module object created from *def* from the interpreter state.
+
+   .. versionadded:: 3.3
 
 Initializing C modules
 ^^^^^^^^^^^^^^^^^^^^^^
diff --git a/Include/pystate.h b/Include/pystate.h
--- a/Include/pystate.h
+++ b/Include/pystate.h
@@ -124,6 +124,11 @@
 PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
 PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
 PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
+/* New in 3.3 */
+PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
+PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
+#endif
 PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
 
 PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #15042: Add PyState_AddModule and PyState_RemoveModule. Add version
+  guard for Py_LIMITED_API additions. Patch by Robin Schreiber.
+
 - Issue #10053: Don't close FDs when FileIO.__init__ fails. Loosely based on
   the work by Hirokazu Yamamoto.
 
@@ -154,6 +157,9 @@
 Documentation
 -------------
 
+- Issue #15081: Document PyState_FindModule.
+  Patch by Robin Schreiber.
+
 - Issue #14814: Added first draft of ipaddress module API reference
 
 Tests
diff --git a/PC/python3.def b/PC/python3.def
--- a/PC/python3.def
+++ b/PC/python3.def
@@ -471,6 +471,8 @@
   PySlice_Type=python33.PySlice_Type DATA
   PySortWrapper_Type=python33.PySortWrapper_Type DATA
   PyState_FindModule=python33.PyState_FindModule
+  PyState_AddModule=python33.PyState_AddModule
+  PyState_RemoveModule=python33.PyState_RemoveModule
   PyStructSequence_GetItem=python33.PyStructSequence_GetItem
   PyStructSequence_New=python33.PyStructSequence_New
   PyStructSequence_NewType=python33.PyStructSequence_NewType
diff --git a/Python/pystate.c b/Python/pystate.c
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -239,9 +239,9 @@
 }
 
 PyObject*
-PyState_FindModule(struct PyModuleDef* m)
+PyState_FindModule(struct PyModuleDef* module)
 {
-    Py_ssize_t index = m->m_base.m_index;
+    Py_ssize_t index = module->m_base.m_index;
     PyInterpreterState *state = PyThreadState_GET()->interp;
     PyObject *res;
     if (index == 0)
@@ -273,6 +273,47 @@
                           def->m_base.m_index, module);
 }
 
+int
+PyState_AddModule(PyObject* module, struct PyModuleDef* def)
+{
+    Py_ssize_t index;
+    PyInterpreterState *state = PyThreadState_GET()->interp;
+    if (!def) {
+        Py_FatalError("PyState_AddModule: Module Definition is NULL");
+        return -1;
+    }
+    index = def->m_base.m_index;
+    if (state->modules_by_index) {
+        if(PyList_GET_SIZE(state->modules_by_index) >= index) {
+            if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
+                Py_FatalError("PyState_AddModule: Module already added!");
+                return -1;
+            }
+        }
+    }
+    return _PyState_AddModule(module, def);
+}
+
+int
+PyState_RemoveModule(struct PyModuleDef* def)
+{
+    Py_ssize_t index = def->m_base.m_index;
+    PyInterpreterState *state = PyThreadState_GET()->interp;
+    if (index == 0) {
+        Py_FatalError("PyState_RemoveModule: Module index invalid.");
+        return -1;
+    }
+    if (state->modules_by_index == NULL) {
+        Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
+        return -1;
+    }
+    if (index > PyList_GET_SIZE(state->modules_by_index)) {
+        Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
+        return -1;
+    }
+    return PyList_SetItem(state->modules_by_index, index, Py_None);
+}
+
 void
 PyThreadState_Clear(PyThreadState *tstate)
 {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list