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.

119 lines
3.4 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. #ifdef MS_WINDOWS
  11. extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
  12. PyObject *pathname, FILE *fp);
  13. #else
  14. extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
  15. const char *pathname, FILE *fp);
  16. #endif
  17. PyObject *
  18. _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
  19. {
  20. PyObject *m = NULL;
  21. #ifndef MS_WINDOWS
  22. PyObject *pathbytes;
  23. #endif
  24. PyObject *nameascii;
  25. char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
  26. dl_funcptr p0;
  27. PyObject* (*p)(void);
  28. struct PyModuleDef *def;
  29. m = _PyImport_FindExtensionObject(name, path);
  30. if (m != NULL) {
  31. Py_INCREF(m);
  32. return m;
  33. }
  34. /* name must be encodable to ASCII because dynamic module must have a
  35. function called "PyInit_NAME", they are written in C, and the C language
  36. doesn't accept non-ASCII identifiers. */
  37. nameascii = PyUnicode_AsEncodedString(name, "ascii", NULL);
  38. if (nameascii == NULL)
  39. return NULL;
  40. namestr = PyBytes_AS_STRING(nameascii);
  41. if (namestr == NULL)
  42. goto error;
  43. lastdot = strrchr(namestr, '.');
  44. if (lastdot == NULL) {
  45. packagecontext = NULL;
  46. shortname = namestr;
  47. }
  48. else {
  49. packagecontext = namestr;
  50. shortname = lastdot+1;
  51. }
  52. #ifdef MS_WINDOWS
  53. p0 = _PyImport_GetDynLoadWindows(shortname, path, fp);
  54. #else
  55. pathbytes = PyUnicode_EncodeFSDefault(path);
  56. if (pathbytes == NULL)
  57. goto error;
  58. p0 = _PyImport_GetDynLoadFunc(shortname,
  59. PyBytes_AS_STRING(pathbytes), fp);
  60. Py_DECREF(pathbytes);
  61. #endif
  62. p = (PyObject*(*)(void))p0;
  63. if (PyErr_Occurred())
  64. goto error;
  65. if (p == NULL) {
  66. PyObject *msg = PyUnicode_FromFormat("dynamic module does not define "
  67. "init function (PyInit_%s)",
  68. shortname);
  69. PyErr_SetImportError(msg, name, path);
  70. Py_DECREF(msg);
  71. goto error;
  72. }
  73. oldcontext = _Py_PackageContext;
  74. _Py_PackageContext = packagecontext;
  75. m = (*p)();
  76. _Py_PackageContext = oldcontext;
  77. if (m == NULL)
  78. goto error;
  79. if (PyErr_Occurred()) {
  80. PyErr_Format(PyExc_SystemError,
  81. "initialization of %s raised unreported exception",
  82. shortname);
  83. goto error;
  84. }
  85. /* Remember pointer to module init function. */
  86. def = PyModule_GetDef(m);
  87. def->m_base.m_init = p;
  88. /* Remember the filename as the __file__ attribute */
  89. if (PyModule_AddObject(m, "__file__", path) < 0)
  90. PyErr_Clear(); /* Not important enough to report */
  91. else
  92. Py_INCREF(path);
  93. if (_PyImport_FixupExtensionObject(m, name, path) < 0)
  94. goto error;
  95. Py_DECREF(nameascii);
  96. return m;
  97. error:
  98. Py_DECREF(nameascii);
  99. Py_XDECREF(m);
  100. return NULL;
  101. }
  102. #endif /* HAVE_DYNAMIC_LOADING */