PyThreadState * Py_NewInterpreter(void) { PyInterpreterState *interp; PyThreadState *tstate, *save_tstate; PyObject *bimod, *sysmod; if (!initialized) Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); interp = PyInterpreterState_New(); if (interp == NULL) return NULL; tstate = PyThreadState_New(interp); if (tstate == NULL) { PyInterpreterState_Delete(interp); return NULL; } save_tstate = PyThreadState_Swap(tstate); /* XXX The following is lax in error checking */ interp->modules = PyDict_New(); bimod = _PyImport_FindExtension("__builtin__", "__builtin__"); if (bimod != NULL) { interp->builtins = PyModule_GetDict(bimod); Py_INCREF(interp->builtins); } sysmod = _PyImport_FindExtension("sys", "sys"); if (bimod != NULL && sysmod != NULL) { interp->sysdict = PyModule_GetDict(sysmod); Py_INCREF(interp->sysdict); PySys_SetPath(Py_GetPath()); PyDict_SetItemString(interp->sysdict, "modules", interp->modules); initmain(); if (!Py_NoSiteFlag) initsite(); } if (!PyErr_Occurred()) return tstate; /* Oops, it didn't work. Undo it all. */ PyErr_Print(); PyThreadState_Clear(tstate); PyThreadState_Swap(save_tstate); PyThreadState_Delete(tstate); PyInterpreterState_Delete(interp); return NULL; }
static PyObject * p_update (PyObject *self, PyObject *args) { if (_PyImport_FindExtension("neo_util","neo_util") == NULL) initneo_util(); if (_PyImport_FindExtension("neo_cs","neo_cs") == NULL) initneo_cs(); Py_INCREF(Py_None); return Py_None; }
int do_import(FARPROC init_func, char *modname) { PyObject *m; char *oldcontext; if ((m = _PyImport_FindExtension(modname, modname)) != NULL) { return -1; } if (init_func == NULL) { PyErr_Format(PyExc_ImportError, "dynamic module does not define init function (init%.200s)", modname); return -1; } oldcontext = _Py_PackageContext; _Py_PackageContext = modname; (*init_func)(); _Py_PackageContext = oldcontext; if (PyErr_Occurred()) { return -1; } if (_PyImport_FixupExtension(modname, modname) == NULL) return -1; if (Py_VerboseFlag) PySys_WriteStderr( "import %s # dynamically loaded from zipfile\n", modname); return 0; }
PyObject * _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp) { PyObject *m, *d, *s; char *lastdot, *shortname, *packagecontext; dl_funcptr p; if ((m = _PyImport_FindExtension(name, pathname)) != NULL) { Py_INCREF(m); return m; } lastdot = strrchr(name, '.'); if (lastdot == NULL) { packagecontext = NULL; shortname = name; } else { packagecontext = name; shortname = lastdot+1; } p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp); if (PyErr_Occurred()) return NULL; if (p == NULL) { PyErr_Format(PyExc_ImportError, "dynamic module does not define init function (init%.200s)", shortname); return NULL; } _Py_PackageContext = packagecontext; (*p)(); _Py_PackageContext = NULL; if (PyErr_Occurred()) return NULL; if (_PyImport_FixupExtension(name, pathname) == NULL) return NULL; m = PyDict_GetItemString(PyImport_GetModuleDict(), name); if (m == NULL) { PyErr_SetString(PyExc_SystemError, "dynamic module not initialized properly"); return NULL; } /* Remember the filename as the __file__ attribute */ d = PyModule_GetDict(m); s = PyString_FromString(pathname); if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0) PyErr_Clear(); /* Not important enough to report */ Py_XDECREF(s); if (Py_VerboseFlag) PySys_WriteStderr( "import %s # dynamically loaded from %s\n", name, pathname); Py_INCREF(m); return m; }