static PyObject * call_state_registration_func(PyObject *mod, PyObject *args) { int i, ret; PyModuleDef *def = PyModule_GetDef(mod); if (def == NULL) { return NULL; } if (!PyArg_ParseTuple(args, "i:call_state_registration_func", &i)) return NULL; switch (i) { case 0: mod = PyState_FindModule(def); if (mod == NULL) { Py_RETURN_NONE; } return mod; case 1: ret = PyState_AddModule(mod, def); if (ret != 0) { return NULL; } break; case 2: ret = PyState_RemoveModule(def); if (ret != 0) { return NULL; } break; } Py_RETURN_NONE; }
bool uninstall_py_dlls(void) { PyObject *modules = PyImport_GetModuleDict(); int i = 0; while(true) { if(g_funs[i] == NULL) break; DEBUG_MSG(fmt::format("Script::uninstall_py_dlls(): {}\n", g_sfuns[i])); PyObject * m = g_importedModules[i++]; if(m == NULL) { return false; } Py_DECREF(m); struct PyModuleDef *def; def = PyModule_GetDef(m); if (!def) { PyErr_BadInternalCall(); return false; } if (PyState_RemoveModule(def) < 0 || PyDict_DelItemString(modules, def->m_name) < 0) return false; } return true; }
int do_import(FARPROC init_func, char *modname) { int res = -1; PyObject* (*p)(void); PyObject *m = NULL; struct PyModuleDef *def; char *oldcontext; PyObject *name = PyUnicode_FromString(modname); if (name == NULL) return -1; m = _PyImport_FindExtensionObject(name, name); if (m != NULL) { Py_DECREF(name); return 0; } if (init_func == NULL) { PyObject *msg = PyUnicode_FromFormat("dynamic module does not define " "init function (PyInit_%s)", modname); if (msg == NULL) return -1; PyErr_SetImportError(msg, name, NULL); Py_DECREF(msg); Py_DECREF(name); return -1; } oldcontext = _Py_PackageContext; _Py_PackageContext = modname; p = (PyObject*(*)(void))init_func; m = (*p)(); _Py_PackageContext = oldcontext; if (PyErr_Occurred()) { Py_DECREF(name); return -1; } /* Remember pointer to module init function. */ def = PyModule_GetDef(m); if (def == NULL) { PyErr_Format(PyExc_SystemError, "initialization of %s did not return an extension " "module", modname); Py_DECREF(name); return -1; } def->m_base.m_init = p; res = _PyImport_FixupExtensionObject(m, name, name); Py_DECREF(name); return res; }
/* used by import.c:PyImport_Cleanup */ void _PyState_ClearModules(void) { PyInterpreterState *state = PyThreadState_GET()->interp; if (state->modules_by_index) { Py_ssize_t i; for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) { PyObject *m = PyList_GET_ITEM(state->modules_by_index, i); if (PyModule_Check(m)) { /* cleanup the saved copy of module dicts */ PyModuleDef *md = PyModule_GetDef(m); if (md) Py_CLEAR(md->m_base.m_copy); } } /* Setting modules_by_index to NULL could be dangerous, so we clear the list instead. */ if (PyList_SetSlice(state->modules_by_index, 0, PyList_GET_SIZE(state->modules_by_index), NULL)) PyErr_WriteUnraisable(state->modules_by_index); } }
PyObject * _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp) { PyObject *m; PyObject *pathbytes; char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext; dl_funcptr p0; PyObject* (*p)(void); struct PyModuleDef *def; namestr = _PyUnicode_AsString(name); if (namestr == NULL) return NULL; m = _PyImport_FindExtensionObject(name, path); if (m != NULL) { Py_INCREF(m); return m; } lastdot = strrchr(namestr, '.'); if (lastdot == NULL) { packagecontext = NULL; shortname = namestr; } else { packagecontext = namestr; shortname = lastdot+1; } pathbytes = PyUnicode_EncodeFSDefault(path); if (pathbytes == NULL) return NULL; p0 = _PyImport_GetDynLoadFunc(shortname, PyBytes_AS_STRING(pathbytes), fp); Py_DECREF(pathbytes); p = (PyObject*(*)(void))p0; if (PyErr_Occurred()) return NULL; if (p == NULL) { PyErr_Format(PyExc_ImportError, "dynamic module does not define init function" " (PyInit_%s)", shortname); return NULL; } oldcontext = _Py_PackageContext; _Py_PackageContext = packagecontext; m = (*p)(); _Py_PackageContext = oldcontext; if (m == NULL) return NULL; if (PyErr_Occurred()) { Py_DECREF(m); PyErr_Format(PyExc_SystemError, "initialization of %s raised unreported exception", shortname); return NULL; } /* Remember pointer to module init function. */ def = PyModule_GetDef(m); def->m_base.m_init = p; /* Remember the filename as the __file__ attribute */ if (PyModule_AddObject(m, "__file__", path) < 0) PyErr_Clear(); /* Not important enough to report */ else Py_INCREF(path); if (_PyImport_FixupExtensionObject(m, name, path) < 0) return NULL; if (Py_VerboseFlag) PySys_FormatStderr( "import %U # dynamically loaded from %R\n", name, path); return m; }
PyObject * _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp) { #ifndef MS_WINDOWS PyObject *pathbytes = NULL; #endif PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL; const char *name_buf, *hook_prefix; const char *oldcontext; dl_funcptr exportfunc; PyModuleDef *def; PyObject *(*p0)(void); name_unicode = PyObject_GetAttrString(spec, "name"); if (name_unicode == NULL) { return NULL; } name = get_encoded_name(name_unicode, &hook_prefix); if (name == NULL) { goto error; } name_buf = PyBytes_AS_STRING(name); path = PyObject_GetAttrString(spec, "origin"); if (path == NULL) goto error; #ifdef MS_WINDOWS exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf, path, fp); #else pathbytes = PyUnicode_EncodeFSDefault(path); if (pathbytes == NULL) goto error; exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf, PyBytes_AS_STRING(pathbytes), fp); Py_DECREF(pathbytes); #endif if (exportfunc == NULL) { if (!PyErr_Occurred()) { PyObject *msg; msg = PyUnicode_FromFormat( "dynamic module does not define " "module export function (%s_%s)", hook_prefix, name_buf); if (msg == NULL) goto error; PyErr_SetImportError(msg, name_unicode, path); Py_DECREF(msg); } goto error; } p0 = (PyObject *(*)(void))exportfunc; /* Package context is needed for single-phase init */ oldcontext = _Py_PackageContext; _Py_PackageContext = PyUnicode_AsUTF8(name_unicode); if (_Py_PackageContext == NULL) { _Py_PackageContext = oldcontext; goto error; } m = p0(); _Py_PackageContext = oldcontext; if (m == NULL) { if (!PyErr_Occurred()) { PyErr_Format( PyExc_SystemError, "initialization of %s failed without raising an exception", name_buf); } goto error; } else if (PyErr_Occurred()) { PyErr_Clear(); PyErr_Format( PyExc_SystemError, "initialization of %s raised unreported exception", name_buf); m = NULL; goto error; } if (Py_TYPE(m) == NULL) { /* This can happen when a PyModuleDef is returned without calling * PyModuleDef_Init on it */ PyErr_Format(PyExc_SystemError, "init function of %s returned uninitialized object", name_buf); m = NULL; /* prevent segfault in DECREF */ goto error; } if (PyObject_TypeCheck(m, &PyModuleDef_Type)) { Py_DECREF(name_unicode); Py_DECREF(name); Py_DECREF(path); return PyModule_FromDefAndSpec((PyModuleDef*)m, spec); } /* Fall back to single-phase init mechanism */ if (hook_prefix == nonascii_prefix) { /* don't allow legacy init for non-ASCII module names */ PyErr_Format( PyExc_SystemError, "initialization of * did not return PyModuleDef", name_buf); goto error; } /* Remember pointer to module init function. */ def = PyModule_GetDef(m); if (def == NULL) { PyErr_Format(PyExc_SystemError, "initialization of %s did not return an extension " "module", name_buf); goto error; } def->m_base.m_init = p0; /* Remember the filename as the __file__ attribute */ if (PyModule_AddObject(m, "__file__", path) < 0) PyErr_Clear(); /* Not important enough to report */ else Py_INCREF(path); if (_PyImport_FixupExtensionObject(m, name_unicode, path) < 0) goto error; Py_DECREF(name_unicode); Py_DECREF(name); Py_DECREF(path); return m; error: Py_DECREF(name_unicode); Py_XDECREF(name); Py_XDECREF(path); Py_XDECREF(m); return NULL; }