summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2008-06-11 05:26:20 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2008-06-11 05:26:20 (GMT)
commit1a21451b1d73b65af949193208372e86bf308411 (patch)
tree8e98d7be9e249b011ae9380479656e5284ec0234 /Python/pystate.c
parentcdf94635d7e364f9ce1905bafa5b540f4d16147c (diff)
downloadcpython-1a21451b1d73b65af949193208372e86bf308411.zip
cpython-1a21451b1d73b65af949193208372e86bf308411.tar.gz
cpython-1a21451b1d73b65af949193208372e86bf308411.tar.bz2
Implement PEP 3121: new module initialization and finalization API.
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index a4d493f..c841eb3 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -69,6 +69,7 @@ PyInterpreterState_New(void)
#endif
interp->modules = NULL;
interp->modules_reloading = NULL;
+ interp->modules_by_index = NULL;
interp->sysdict = NULL;
interp->builtins = NULL;
interp->tstate_head = NULL;
@@ -108,6 +109,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
Py_CLEAR(interp->codec_search_cache);
Py_CLEAR(interp->codec_error_registry);
Py_CLEAR(interp->modules);
+ Py_CLEAR(interp->modules_by_index);
Py_CLEAR(interp->modules_reloading);
Py_CLEAR(interp->sysdict);
Py_CLEAR(interp->builtins);
@@ -208,6 +210,40 @@ PyThreadState_New(PyInterpreterState *interp)
return tstate;
}
+PyObject*
+PyState_FindModule(struct PyModuleDef* m)
+{
+ Py_ssize_t index = m->m_base.m_index;
+ PyInterpreterState *state = PyThreadState_GET()->interp;
+ PyObject *res;
+ if (index == 0)
+ return NULL;
+ if (state->modules_by_index == NULL)
+ return NULL;
+ if (index > PyList_GET_SIZE(state->modules_by_index))
+ return NULL;
+ res = PyList_GET_ITEM(state->modules_by_index, index);
+ return res==Py_None ? NULL : res;
+}
+
+int
+_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
+{
+ PyInterpreterState *state = PyThreadState_GET()->interp;
+ if (!def)
+ return -1;
+ if (!state->modules_by_index) {
+ state->modules_by_index = PyList_New(20);
+ if (!state->modules_by_index)
+ return -1;
+ }
+ while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
+ if (PyList_Append(state->modules_by_index, Py_None) < 0)
+ return -1;
+ Py_INCREF(module);
+ return PyList_SetItem(state->modules_by_index,
+ def->m_base.m_index, module);
+}
void
PyThreadState_Clear(PyThreadState *tstate)