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.

231 lines
6.8 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. 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. name = get_encoded_name(name_unicode, &hook_prefix);
  94. if (name == NULL) {
  95. goto error;
  96. }
  97. name_buf = PyBytes_AS_STRING(name);
  98. path = PyObject_GetAttrString(spec, "origin");
  99. if (path == NULL)
  100. goto error;
  101. #ifdef MS_WINDOWS
  102. exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf,
  103. path, fp);
  104. #else
  105. pathbytes = PyUnicode_EncodeFSDefault(path);
  106. if (pathbytes == NULL)
  107. goto error;
  108. exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf,
  109. PyBytes_AS_STRING(pathbytes),
  110. fp);
  111. Py_DECREF(pathbytes);
  112. #endif
  113. if (exportfunc == NULL) {
  114. if (!PyErr_Occurred()) {
  115. PyObject *msg;
  116. msg = PyUnicode_FromFormat(
  117. "dynamic module does not define "
  118. "module export function (%s_%s)",
  119. hook_prefix, name_buf);
  120. if (msg == NULL)
  121. goto error;
  122. PyErr_SetImportError(msg, name_unicode, path);
  123. Py_DECREF(msg);
  124. }
  125. goto error;
  126. }
  127. p0 = (PyObject *(*)(void))exportfunc;
  128. /* Package context is needed for single-phase init */
  129. oldcontext = _Py_PackageContext;
  130. _Py_PackageContext = PyUnicode_AsUTF8(name_unicode);
  131. m = p0();
  132. _Py_PackageContext = oldcontext;
  133. if (m == NULL) {
  134. if (!PyErr_Occurred()) {
  135. PyErr_Format(
  136. PyExc_SystemError,
  137. "initialization of %s failed without raising an exception",
  138. name_buf);
  139. }
  140. goto error;
  141. } else if (PyErr_Occurred()) {
  142. PyErr_Clear();
  143. PyErr_Format(
  144. PyExc_SystemError,
  145. "initialization of %s raised unreported exception",
  146. name_buf);
  147. m = NULL;
  148. goto error;
  149. }
  150. if (Py_TYPE(m) == NULL) {
  151. /* This can happen when a PyModuleDef is returned without calling
  152. * PyModuleDef_Init on it
  153. */
  154. PyErr_Format(PyExc_SystemError,
  155. "init function of %s returned uninitialized object",
  156. name_buf);
  157. m = NULL; /* prevent segfault in DECREF */
  158. goto error;
  159. }
  160. if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
  161. Py_DECREF(name_unicode);
  162. Py_DECREF(name);
  163. Py_DECREF(path);
  164. return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
  165. }
  166. /* Fall back to single-phase init mechanism */
  167. if (hook_prefix == nonascii_prefix) {
  168. /* don't allow legacy init for non-ASCII module names */
  169. PyErr_Format(
  170. PyExc_SystemError,
  171. "initialization of * did not return PyModuleDef",
  172. name_buf);
  173. goto error;
  174. }
  175. /* Remember pointer to module init function. */
  176. def = PyModule_GetDef(m);
  177. if (def == NULL) {
  178. PyErr_Format(PyExc_SystemError,
  179. "initialization of %s did not return an extension "
  180. "module", name_buf);
  181. goto error;
  182. }
  183. def->m_base.m_init = p0;
  184. /* Remember the filename as the __file__ attribute */
  185. if (PyModule_AddObject(m, "__file__", path) < 0)
  186. PyErr_Clear(); /* Not important enough to report */
  187. else
  188. Py_INCREF(path);
  189. if (_PyImport_FixupExtensionObject(m, name_unicode, path) < 0)
  190. goto error;
  191. Py_DECREF(name_unicode);
  192. Py_DECREF(name);
  193. Py_DECREF(path);
  194. return m;
  195. error:
  196. Py_DECREF(name_unicode);
  197. Py_XDECREF(name);
  198. Py_XDECREF(path);
  199. Py_XDECREF(m);
  200. return NULL;
  201. }
  202. #endif /* HAVE_DYNAMIC_LOADING */