| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Support for dynamic loading of extension modules */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 20:08:16 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
 | 
					
						
							|  |  |  |    supported on this platform. configure will then compile and link in one | 
					
						
							|  |  |  |    of the dynload_*.c files, as appropriate. We will call a function in | 
					
						
							|  |  |  |    those modules to get a function pointer to the module's init function. | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											1999-12-20 21:20:42 +00:00
										 |  |  | #ifdef HAVE_DYNAMIC_LOADING
 | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | #include "importdl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix, | 
					
						
							|  |  |  |                                                      const char *shortname, | 
					
						
							|  |  |  |                                                      PyObject *pathname, | 
					
						
							|  |  |  |                                                      FILE *fp); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix, | 
					
						
							|  |  |  |                                               const char *shortname, | 
					
						
							|  |  |  |                                               const char *pathname, FILE *fp); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-11-22 21:53:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-25 19:53:18 +02:00
										 |  |  | static const char * const ascii_only_prefix = "PyInit"; | 
					
						
							|  |  |  | static const char * const nonascii_prefix = "PyInitU"; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Get the variable part of a module's export symbol name.
 | 
					
						
							|  |  |  |  * Returns a bytes instance. For non-ASCII-named modules, the name is | 
					
						
							|  |  |  |  * encoded as per PEP 489. | 
					
						
							|  |  |  |  * The hook_prefix pointer is set to either ascii_only_prefix or | 
					
						
							|  |  |  |  * nonascii_prefix, as appropriate. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | get_encoded_name(PyObject *name, const char **hook_prefix) { | 
					
						
							|  |  |  |     PyObject *tmp; | 
					
						
							|  |  |  |     PyObject *encoded = NULL; | 
					
						
							| 
									
										
										
										
											2015-05-29 17:10:30 -05:00
										 |  |  |     PyObject *modname = NULL; | 
					
						
							|  |  |  |     Py_ssize_t name_len, lastdot; | 
					
						
							|  |  |  |     _Py_IDENTIFIER(replace); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Get the short name (substring after last dot) */ | 
					
						
							|  |  |  |     name_len = PyUnicode_GetLength(name); | 
					
						
							|  |  |  |     lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1); | 
					
						
							|  |  |  |     if (lastdot < -1) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } else if (lastdot >= 0) { | 
					
						
							| 
									
										
										
										
											2015-05-26 21:48:17 +10:00
										 |  |  |         tmp = PyUnicode_Substring(name, lastdot + 1, name_len); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         if (tmp == NULL) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         name = tmp; | 
					
						
							|  |  |  |         /* "name" now holds a new reference to the substring */ | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         Py_INCREF(name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Encode to ASCII or Punycode, as needed */ | 
					
						
							|  |  |  |     encoded = PyUnicode_AsEncodedString(name, "ascii", NULL); | 
					
						
							|  |  |  |     if (encoded != NULL) { | 
					
						
							|  |  |  |         *hook_prefix = ascii_only_prefix; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { | 
					
						
							|  |  |  |             PyErr_Clear(); | 
					
						
							|  |  |  |             encoded = PyUnicode_AsEncodedString(name, "punycode", NULL); | 
					
						
							|  |  |  |             if (encoded == NULL) { | 
					
						
							|  |  |  |                 goto error; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             *hook_prefix = nonascii_prefix; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 17:10:30 -05:00
										 |  |  |     /* Replace '-' by '_' */ | 
					
						
							|  |  |  |     modname = _PyObject_CallMethodId(encoded, &PyId_replace, "cc", '-', '_'); | 
					
						
							|  |  |  |     if (modname == NULL) | 
					
						
							|  |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Py_DECREF(name); | 
					
						
							| 
									
										
										
										
											2015-05-29 17:10:30 -05:00
										 |  |  |     Py_DECREF(encoded); | 
					
						
							|  |  |  |     return modname; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | error: | 
					
						
							|  |  |  |     Py_DECREF(name); | 
					
						
							|  |  |  |     Py_XDECREF(encoded); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 20:08:16 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp) | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     PyObject *pathbytes = NULL; | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL; | 
					
						
							|  |  |  |     const char *name_buf, *hook_prefix; | 
					
						
							| 
									
										
										
										
											2016-11-21 10:25:54 +02:00
										 |  |  |     const char *oldcontext; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     dl_funcptr exportfunc; | 
					
						
							|  |  |  |     PyModuleDef *def; | 
					
						
							|  |  |  |     PyObject *(*p0)(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     name_unicode = PyObject_GetAttrString(spec, "name"); | 
					
						
							|  |  |  |     if (name_unicode == NULL) { | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-19 14:39:47 +03:00
										 |  |  |     if (!PyUnicode_Check(name_unicode)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                         "spec.name must be a string"); | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     name = get_encoded_name(name_unicode, &hook_prefix); | 
					
						
							|  |  |  |     if (name == NULL) { | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     name_buf = PyBytes_AS_STRING(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     path = PyObject_GetAttrString(spec, "origin"); | 
					
						
							|  |  |  |     if (path == NULL) | 
					
						
							|  |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											1995-07-18 14:40:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |     if (PySys_Audit("import", "OOOOO", name_unicode, path, | 
					
						
							|  |  |  |                     Py_None, Py_None, Py_None) < 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf, | 
					
						
							|  |  |  |                                                     path, fp); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     pathbytes = PyUnicode_EncodeFSDefault(path); | 
					
						
							|  |  |  |     if (pathbytes == NULL) | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf, | 
					
						
							|  |  |  |                                              PyBytes_AS_STRING(pathbytes), | 
					
						
							|  |  |  |                                              fp); | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     Py_DECREF(pathbytes); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (exportfunc == NULL) { | 
					
						
							|  |  |  |         if (!PyErr_Occurred()) { | 
					
						
							|  |  |  |             PyObject *msg; | 
					
						
							|  |  |  |             msg = PyUnicode_FromFormat( | 
					
						
							|  |  |  |                 "dynamic module does not define " | 
					
						
							|  |  |  |                 "module export function (%s_%s)", | 
					
						
							|  |  |  |                 hook_prefix, name_buf); | 
					
						
							|  |  |  |             if (msg == NULL) | 
					
						
							|  |  |  |                 goto error; | 
					
						
							|  |  |  |             PyErr_SetImportError(msg, name_unicode, path); | 
					
						
							|  |  |  |             Py_DECREF(msg); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     p0 = (PyObject *(*)(void))exportfunc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Package context is needed for single-phase init */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     oldcontext = _Py_PackageContext; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     _Py_PackageContext = PyUnicode_AsUTF8(name_unicode); | 
					
						
							| 
									
										
										
										
											2016-11-20 08:47:21 +02:00
										 |  |  |     if (_Py_PackageContext == NULL) { | 
					
						
							|  |  |  |         _Py_PackageContext = oldcontext; | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     m = p0(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     _Py_PackageContext = oldcontext; | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     if (m == NULL) { | 
					
						
							|  |  |  |         if (!PyErr_Occurred()) { | 
					
						
							|  |  |  |             PyErr_Format( | 
					
						
							|  |  |  |                 PyExc_SystemError, | 
					
						
							|  |  |  |                 "initialization of %s failed without raising an exception", | 
					
						
							|  |  |  |                 name_buf); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } else if (PyErr_Occurred()) { | 
					
						
							|  |  |  |         PyErr_Clear(); | 
					
						
							|  |  |  |         PyErr_Format( | 
					
						
							|  |  |  |             PyExc_SystemError, | 
					
						
							|  |  |  |             "initialization of %s raised unreported exception", | 
					
						
							|  |  |  |             name_buf); | 
					
						
							|  |  |  |         m = NULL; | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (Py_TYPE(m) == NULL) { | 
					
						
							|  |  |  |         /* This can happen when a PyModuleDef is returned without calling
 | 
					
						
							|  |  |  |          * PyModuleDef_Init on it | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         PyErr_Format(PyExc_SystemError, | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |                      "init function of %s returned uninitialized object", | 
					
						
							|  |  |  |                      name_buf); | 
					
						
							|  |  |  |         m = NULL; /* prevent segfault in DECREF */ | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PyObject_TypeCheck(m, &PyModuleDef_Type)) { | 
					
						
							|  |  |  |         Py_DECREF(name_unicode); | 
					
						
							|  |  |  |         Py_DECREF(name); | 
					
						
							|  |  |  |         Py_DECREF(path); | 
					
						
							|  |  |  |         return PyModule_FromDefAndSpec((PyModuleDef*)m, spec); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Fall back to single-phase init mechanism */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (hook_prefix == nonascii_prefix) { | 
					
						
							|  |  |  |         /* don't allow legacy init for non-ASCII module names */ | 
					
						
							|  |  |  |         PyErr_Format( | 
					
						
							|  |  |  |             PyExc_SystemError, | 
					
						
							|  |  |  |             "initialization of * did not return PyModuleDef", | 
					
						
							|  |  |  |             name_buf); | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* Remember pointer to module init function. */ | 
					
						
							|  |  |  |     def = PyModule_GetDef(m); | 
					
						
							| 
									
										
										
										
											2013-07-11 13:02:30 +02:00
										 |  |  |     if (def == NULL) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_SystemError, | 
					
						
							|  |  |  |                      "initialization of %s did not return an extension " | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |                      "module", name_buf); | 
					
						
							| 
									
										
										
										
											2013-07-11 11:22:21 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2013-07-11 13:02:30 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     def->m_base.m_init = p0; | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* Remember the filename as the __file__ attribute */ | 
					
						
							|  |  |  |     if (PyModule_AddObject(m, "__file__", path) < 0) | 
					
						
							|  |  |  |         PyErr_Clear(); /* Not important enough to report */ | 
					
						
							| 
									
										
										
										
											2010-10-17 01:24:53 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |         Py_INCREF(path); | 
					
						
							| 
									
										
										
										
											2003-09-04 18:45:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 12:18:12 -06:00
										 |  |  |     PyObject *modules = PyImport_GetModuleDict(); | 
					
						
							|  |  |  |     if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0) | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Py_DECREF(name_unicode); | 
					
						
							|  |  |  |     Py_DECREF(name); | 
					
						
							|  |  |  |     Py_DECREF(path); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     return m; | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     Py_DECREF(name_unicode); | 
					
						
							|  |  |  |     Py_XDECREF(name); | 
					
						
							|  |  |  |     Py_XDECREF(path); | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |     Py_XDECREF(m); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											1998-08-04 22:46:29 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif /* HAVE_DYNAMIC_LOADING */
 |