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; }
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; }
PyObject *_apache_module_init() { PyObject *m, *d, *o; PyType_Ready(&MpTable_Type); PyType_Ready(&MpTableIter_Type); PyType_Ready(&MpServer_Type); PyType_Ready(&MpConn_Type); PyType_Ready(&MpRequest_Type); PyType_Ready(&MpFilter_Type); PyType_Ready(&MpHList_Type); #if PY_MAJOR_VERSION < 3 m = Py_InitModule("_apache", _apache_module_methods); #else m = PyModule_Create(&_apache_moduledef); PyObject *name = PyUnicode_FromString("_apache"); _PyImport_FixupExtensionObject(m, name, name); #endif d = PyModule_GetDict(m); Mp_ServerReturn = PyErr_NewException("_apache.SERVER_RETURN", NULL, NULL); if (Mp_ServerReturn == NULL) return NULL; PyDict_SetItemString(d, "SERVER_RETURN", Mp_ServerReturn); PyDict_SetItemString(d, "table", (PyObject *)&MpTable_Type); o = PyLong_FromLong(AP_CONN_UNKNOWN); PyDict_SetItemString(d, "AP_CONN_UNKNOWN", o); Py_DECREF(o); o = PyLong_FromLong(AP_CONN_CLOSE); PyDict_SetItemString(d, "AP_CONN_CLOSE", o); Py_DECREF(o); o = PyLong_FromLong(AP_CONN_KEEPALIVE); PyDict_SetItemString(d, "AP_CONN_KEEPALIVE", o); Py_DECREF(o); o = PyLong_FromLong(APR_NOFILE); PyDict_SetItemString(d, "APR_NOFILE", o); Py_DECREF(o); o = PyLong_FromLong(APR_REG); PyDict_SetItemString(d, "APR_REG", o); Py_DECREF(o); o = PyLong_FromLong(APR_DIR); PyDict_SetItemString(d, "APR_DIR", o); Py_DECREF(o); o = PyLong_FromLong(APR_CHR); PyDict_SetItemString(d, "APR_CHR", o); Py_DECREF(o); o = PyLong_FromLong(APR_BLK); PyDict_SetItemString(d, "APR_BLK", o); Py_DECREF(o); o = PyLong_FromLong(APR_PIPE); PyDict_SetItemString(d, "APR_PIPE", o); Py_DECREF(o); o = PyLong_FromLong(APR_LNK); PyDict_SetItemString(d, "APR_LNK", o); Py_DECREF(o); o = PyLong_FromLong(APR_SOCK); PyDict_SetItemString(d, "APR_SOCK", o); Py_DECREF(o); o = PyLong_FromLong(APR_UNKFILE); PyDict_SetItemString(d, "APR_UNKFILE", o); Py_DECREF(o); o = PyLong_FromLong(MODULE_MAGIC_NUMBER_MAJOR); PyDict_SetItemString(d, "MODULE_MAGIC_NUMBER_MAJOR", o); Py_DECREF(o); o = PyLong_FromLong(MODULE_MAGIC_NUMBER_MINOR); PyDict_SetItemString(d, "MODULE_MAGIC_NUMBER_MINOR", o); Py_DECREF(o); return m; }