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.

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