You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

  1. /* Support for dynamic loading of extension modules */
  2. #include "Python.h"
  3. /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
  4. supported on this platform. configure will then compile and link in one
  5. of the dynload_*.c files, as appropriate. We will call a function in
  6. those modules to get a function pointer to the module's init function.
  7. */
  8. #ifdef HAVE_DYNAMIC_LOADING
  9. #include "importdl.h"
  10. extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
  11. const char *shortname,
  12. const char *pathname, FILE *fp);
  13. PyObject *
  14. _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
  15. {
  16. PyObject *m;
  17. PyObject *path;
  18. char *lastdot, *shortname, *packagecontext, *oldcontext;
  19. dl_funcptr p0;
  20. PyObject* (*p)(void);
  21. struct PyModuleDef *def;
  22. if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
  23. Py_INCREF(m);
  24. return m;
  25. }
  26. lastdot = strrchr(name, '.');
  27. if (lastdot == NULL) {
  28. packagecontext = NULL;
  29. shortname = name;
  30. }
  31. else {
  32. packagecontext = name;
  33. shortname = lastdot+1;
  34. }
  35. p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
  36. p = (PyObject*(*)(void))p0;
  37. if (PyErr_Occurred())
  38. return NULL;
  39. if (p == NULL) {
  40. PyErr_Format(PyExc_ImportError,
  41. "dynamic module does not define init function (PyInit_%.200s)",
  42. shortname);
  43. return NULL;
  44. }
  45. oldcontext = _Py_PackageContext;
  46. _Py_PackageContext = packagecontext;
  47. m = (*p)();
  48. _Py_PackageContext = oldcontext;
  49. if (m == NULL)
  50. return NULL;
  51. if (PyErr_Occurred()) {
  52. Py_DECREF(m);
  53. PyErr_Format(PyExc_SystemError,
  54. "initialization of %s raised unreported exception",
  55. shortname);
  56. return NULL;
  57. }
  58. /* Remember pointer to module init function. */
  59. def = PyModule_GetDef(m);
  60. def->m_base.m_init = p;
  61. /* Remember the filename as the __file__ attribute */
  62. path = PyUnicode_DecodeFSDefault(pathname);
  63. if (PyModule_AddObject(m, "__file__", path) < 0)
  64. PyErr_Clear(); /* Not important enough to report */
  65. if (_PyImport_FixupExtension(m, name, pathname) < 0)
  66. return NULL;
  67. if (Py_VerboseFlag)
  68. PySys_WriteStderr(
  69. "import %s # dynamically loaded from %s\n",
  70. name, pathname);
  71. return m;
  72. }
  73. #endif /* HAVE_DYNAMIC_LOADING */