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.

127 lines
3.6 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. if (msg == NULL)
  70. goto error;
  71. PyErr_SetImportError(msg, name, path);
  72. Py_DECREF(msg);
  73. goto error;
  74. }
  75. oldcontext = _Py_PackageContext;
  76. _Py_PackageContext = packagecontext;
  77. m = (*p)();
  78. _Py_PackageContext = oldcontext;
  79. if (m == NULL)
  80. goto error;
  81. if (PyErr_Occurred()) {
  82. PyErr_Format(PyExc_SystemError,
  83. "initialization of %s raised unreported exception",
  84. shortname);
  85. goto error;
  86. }
  87. /* Remember pointer to module init function. */
  88. def = PyModule_GetDef(m);
  89. if (def == NULL) {
  90. PyErr_Format(PyExc_SystemError,
  91. "initialization of %s did not return an extension "
  92. "module", shortname);
  93. goto error;
  94. }
  95. def->m_base.m_init = p;
  96. /* Remember the filename as the __file__ attribute */
  97. if (PyModule_AddObject(m, "__file__", path) < 0)
  98. PyErr_Clear(); /* Not important enough to report */
  99. else
  100. Py_INCREF(path);
  101. if (_PyImport_FixupExtensionObject(m, name, path) < 0)
  102. goto error;
  103. Py_DECREF(nameascii);
  104. return m;
  105. error:
  106. Py_DECREF(nameascii);
  107. Py_XDECREF(m);
  108. return NULL;
  109. }
  110. #endif /* HAVE_DYNAMIC_LOADING */