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.

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