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.

235 lines
6.9 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. if (_Py_PackageContext == NULL) {
  132. _Py_PackageContext = oldcontext;
  133. goto error;
  134. }
  135. m = p0();
  136. _Py_PackageContext = oldcontext;
  137. if (m == NULL) {
  138. if (!PyErr_Occurred()) {
  139. PyErr_Format(
  140. PyExc_SystemError,
  141. "initialization of %s failed without raising an exception",
  142. name_buf);
  143. }
  144. goto error;
  145. } else if (PyErr_Occurred()) {
  146. PyErr_Clear();
  147. PyErr_Format(
  148. PyExc_SystemError,
  149. "initialization of %s raised unreported exception",
  150. name_buf);
  151. m = NULL;
  152. goto error;
  153. }
  154. if (Py_TYPE(m) == NULL) {
  155. /* This can happen when a PyModuleDef is returned without calling
  156. * PyModuleDef_Init on it
  157. */
  158. PyErr_Format(PyExc_SystemError,
  159. "init function of %s returned uninitialized object",
  160. name_buf);
  161. m = NULL; /* prevent segfault in DECREF */
  162. goto error;
  163. }
  164. if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
  165. Py_DECREF(name_unicode);
  166. Py_DECREF(name);
  167. Py_DECREF(path);
  168. return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
  169. }
  170. /* Fall back to single-phase init mechanism */
  171. if (hook_prefix == nonascii_prefix) {
  172. /* don't allow legacy init for non-ASCII module names */
  173. PyErr_Format(
  174. PyExc_SystemError,
  175. "initialization of * did not return PyModuleDef",
  176. name_buf);
  177. goto error;
  178. }
  179. /* Remember pointer to module init function. */
  180. def = PyModule_GetDef(m);
  181. if (def == NULL) {
  182. PyErr_Format(PyExc_SystemError,
  183. "initialization of %s did not return an extension "
  184. "module", name_buf);
  185. goto error;
  186. }
  187. def->m_base.m_init = p0;
  188. /* Remember the filename as the __file__ attribute */
  189. if (PyModule_AddObject(m, "__file__", path) < 0)
  190. PyErr_Clear(); /* Not important enough to report */
  191. else
  192. Py_INCREF(path);
  193. if (_PyImport_FixupExtensionObject(m, name_unicode, path) < 0)
  194. goto error;
  195. Py_DECREF(name_unicode);
  196. Py_DECREF(name);
  197. Py_DECREF(path);
  198. return m;
  199. error:
  200. Py_DECREF(name_unicode);
  201. Py_XDECREF(name);
  202. Py_XDECREF(path);
  203. Py_XDECREF(m);
  204. return NULL;
  205. }
  206. #endif /* HAVE_DYNAMIC_LOADING */