importdl.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Support for dynamic loading of extension modules */
  2. #include "Python.h"
  3. #include "pycore_call.h"
  4. #include "pycore_import.h"
  5. #include "pycore_pystate.h"
  6. #include "pycore_runtime.h"
  7. /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
  8. supported on this platform. configure will then compile and link in one
  9. of the dynload_*.c files, as appropriate. We will call a function in
  10. those modules to get a function pointer to the module's init function.
  11. */
  12. #ifdef HAVE_DYNAMIC_LOADING
  13. #include "importdl.h"
  14. #ifdef MS_WINDOWS
  15. extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
  16. const char *shortname,
  17. PyObject *pathname,
  18. FILE *fp);
  19. #else
  20. extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
  21. const char *shortname,
  22. const char *pathname, FILE *fp);
  23. #endif
  24. static const char * const ascii_only_prefix = "PyInit";
  25. static const char * const nonascii_prefix = "PyInitU";
  26. /* Get the variable part of a module's export symbol name.
  27. * Returns a bytes instance. For non-ASCII-named modules, the name is
  28. * encoded as per PEP 489.
  29. * The hook_prefix pointer is set to either ascii_only_prefix or
  30. * nonascii_prefix, as appropriate.
  31. */
  32. static PyObject *
  33. get_encoded_name(PyObject *name, const char **hook_prefix) {
  34. PyObject *tmp;
  35. PyObject *encoded = NULL;
  36. PyObject *modname = NULL;
  37. Py_ssize_t name_len, lastdot;
  38. /* Get the short name (substring after last dot) */
  39. name_len = PyUnicode_GetLength(name);
  40. if (name_len < 0) {
  41. return NULL;
  42. }
  43. lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1);
  44. if (lastdot < -1) {
  45. return NULL;
  46. } else if (lastdot >= 0) {
  47. tmp = PyUnicode_Substring(name, lastdot + 1, name_len);
  48. if (tmp == NULL)
  49. return NULL;
  50. name = tmp;
  51. /* "name" now holds a new reference to the substring */
  52. } else {
  53. Py_INCREF(name);
  54. }
  55. /* Encode to ASCII or Punycode, as needed */
  56. encoded = PyUnicode_AsEncodedString(name, "ascii", NULL);
  57. if (encoded != NULL) {
  58. *hook_prefix = ascii_only_prefix;
  59. } else {
  60. if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
  61. PyErr_Clear();
  62. encoded = PyUnicode_AsEncodedString(name, "punycode", NULL);
  63. if (encoded == NULL) {
  64. goto error;
  65. }
  66. *hook_prefix = nonascii_prefix;
  67. } else {
  68. goto error;
  69. }
  70. }
  71. /* Replace '-' by '_' */
  72. modname = _PyObject_CallMethod(encoded, &_Py_ID(replace), "cc", '-', '_');
  73. if (modname == NULL)
  74. goto error;
  75. Py_DECREF(name);
  76. Py_DECREF(encoded);
  77. return modname;
  78. error:
  79. Py_DECREF(name);
  80. Py_XDECREF(encoded);
  81. return NULL;
  82. }
  83. PyObject *
  84. _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
  85. {
  86. #ifndef MS_WINDOWS
  87. PyObject *pathbytes = NULL;
  88. #endif
  89. PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL;
  90. const char *name_buf, *hook_prefix;
  91. const char *oldcontext, *newcontext;
  92. dl_funcptr exportfunc;
  93. PyModuleDef *def;
  94. PyModInitFunction p0;
  95. name_unicode = PyObject_GetAttrString(spec, "name");
  96. if (name_unicode == NULL) {
  97. return NULL;
  98. }
  99. if (!PyUnicode_Check(name_unicode)) {
  100. PyErr_SetString(PyExc_TypeError,
  101. "spec.name must be a string");
  102. goto error;
  103. }
  104. newcontext = PyUnicode_AsUTF8(name_unicode);
  105. if (newcontext == NULL) {
  106. goto error;
  107. }
  108. name = get_encoded_name(name_unicode, &hook_prefix);
  109. if (name == NULL) {
  110. goto error;
  111. }
  112. name_buf = PyBytes_AS_STRING(name);
  113. path = PyObject_GetAttrString(spec, "origin");
  114. if (path == NULL)
  115. goto error;
  116. if (PySys_Audit("import", "OOOOO", name_unicode, path,
  117. Py_None, Py_None, Py_None) < 0) {
  118. goto error;
  119. }
  120. #ifdef MS_WINDOWS
  121. exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf,
  122. path, fp);
  123. #else
  124. pathbytes = PyUnicode_EncodeFSDefault(path);
  125. if (pathbytes == NULL)
  126. goto error;
  127. exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf,
  128. PyBytes_AS_STRING(pathbytes),
  129. fp);
  130. Py_DECREF(pathbytes);
  131. #endif
  132. if (exportfunc == NULL) {
  133. if (!PyErr_Occurred()) {
  134. PyObject *msg;
  135. msg = PyUnicode_FromFormat(
  136. "dynamic module does not define "
  137. "module export function (%s_%s)",
  138. hook_prefix, name_buf);
  139. if (msg == NULL)
  140. goto error;
  141. PyErr_SetImportError(msg, name_unicode, path);
  142. Py_DECREF(msg);
  143. }
  144. goto error;
  145. }
  146. p0 = (PyModInitFunction)exportfunc;
  147. /* Package context is needed for single-phase init */
  148. oldcontext = _PyImport_SwapPackageContext(newcontext);
  149. m = _PyImport_InitFunc_TrampolineCall(p0);
  150. _PyImport_SwapPackageContext(oldcontext);
  151. if (m == NULL) {
  152. if (!PyErr_Occurred()) {
  153. PyErr_Format(
  154. PyExc_SystemError,
  155. "initialization of %s failed without raising an exception",
  156. name_buf);
  157. }
  158. goto error;
  159. } else if (PyErr_Occurred()) {
  160. _PyErr_FormatFromCause(
  161. PyExc_SystemError,
  162. "initialization of %s raised unreported exception",
  163. name_buf);
  164. m = NULL;
  165. goto error;
  166. }
  167. if (Py_IS_TYPE(m, NULL)) {
  168. /* This can happen when a PyModuleDef is returned without calling
  169. * PyModuleDef_Init on it
  170. */
  171. PyErr_Format(PyExc_SystemError,
  172. "init function of %s returned uninitialized object",
  173. name_buf);
  174. m = NULL; /* prevent segfault in DECREF */
  175. goto error;
  176. }
  177. if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
  178. Py_DECREF(name_unicode);
  179. Py_DECREF(name);
  180. Py_DECREF(path);
  181. return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
  182. }
  183. /* Fall back to single-phase init mechanism */
  184. if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
  185. goto error;
  186. }
  187. if (hook_prefix == nonascii_prefix) {
  188. /* don't allow legacy init for non-ASCII module names */
  189. PyErr_Format(
  190. PyExc_SystemError,
  191. "initialization of %s did not return PyModuleDef",
  192. name_buf);
  193. goto error;
  194. }
  195. /* Remember pointer to module init function. */
  196. def = PyModule_GetDef(m);
  197. if (def == NULL) {
  198. PyErr_Format(PyExc_SystemError,
  199. "initialization of %s did not return an extension "
  200. "module", name_buf);
  201. goto error;
  202. }
  203. def->m_base.m_init = p0;
  204. /* Remember the filename as the __file__ attribute */
  205. if (PyModule_AddObjectRef(m, "__file__", path) < 0) {
  206. PyErr_Clear(); /* Not important enough to report */
  207. }
  208. PyObject *modules = PyImport_GetModuleDict();
  209. if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
  210. goto error;
  211. Py_DECREF(name_unicode);
  212. Py_DECREF(name);
  213. Py_DECREF(path);
  214. return m;
  215. error:
  216. Py_DECREF(name_unicode);
  217. Py_XDECREF(name);
  218. Py_XDECREF(path);
  219. Py_XDECREF(m);
  220. return NULL;
  221. }
  222. #endif /* HAVE_DYNAMIC_LOADING */