| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  | #include "pycore_call.h"
 | 
					
						
							| 
									
										
										
										
											2023-02-15 18:16:00 -07:00
										 |  |  | #include "pycore_import.h"
 | 
					
						
							| 
									
										
										
										
											2023-07-03 12:48:50 +02:00
										 |  |  | #include "pycore_pyerrors.h"      // _PyErr_FormatFromCause()
 | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  | #include "pycore_pystate.h"
 | 
					
						
							|  |  |  | #include "pycore_runtime.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 22:50:29 +00:00
										 |  |  | #include "pycore_importdl.h"
 | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | /***********************************/ | 
					
						
							|  |  |  | /* module info to use when loading */ | 
					
						
							|  |  |  | /***********************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Get the short name (substring after last dot) */ | 
					
						
							|  |  |  |     name_len = PyUnicode_GetLength(name); | 
					
						
							| 
									
										
										
										
											2021-10-11 20:08:38 +09:00
										 |  |  |     if (name_len < 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     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 '_' */ | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |     modname = _PyObject_CallMethod(encoded, &_Py_ID(replace), "cc", '-', '_'); | 
					
						
							| 
									
										
										
										
											2015-05-29 17:10:30 -05:00
										 |  |  |     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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_ext_module_loader_info_clear(struct _Py_ext_module_loader_info *info) | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     Py_CLEAR(info->filename); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     Py_CLEAR(info->filename_encoded); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     Py_CLEAR(info->name); | 
					
						
							|  |  |  |     Py_CLEAR(info->name_encoded); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_ext_module_loader_info_init(struct _Py_ext_module_loader_info *p_info, | 
					
						
							| 
									
										
										
										
											2024-05-04 15:24:02 -06:00
										 |  |  |                                 PyObject *name, PyObject *filename, | 
					
						
							|  |  |  |                                 _Py_ext_module_origin origin) | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-05-04 15:24:02 -06:00
										 |  |  |     struct _Py_ext_module_loader_info info = { | 
					
						
							|  |  |  |         .origin=origin, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert(name != NULL); | 
					
						
							|  |  |  |     if (!PyUnicode_Check(name)) { | 
					
						
							| 
									
										
										
										
											2017-09-19 14:39:47 +03:00
										 |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |                         "module name must be a string"); | 
					
						
							|  |  |  |         _Py_ext_module_loader_info_clear(&info); | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2017-09-19 14:39:47 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-29 12:53:04 -06:00
										 |  |  |     assert(PyUnicode_GetLength(name) > 0); | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     info.name = Py_NewRef(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     info.name_encoded = get_encoded_name(info.name, &info.hook_prefix); | 
					
						
							|  |  |  |     if (info.name_encoded == NULL) { | 
					
						
							|  |  |  |         _Py_ext_module_loader_info_clear(&info); | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2023-02-15 15:32:31 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     info.newcontext = PyUnicode_AsUTF8(info.name); | 
					
						
							|  |  |  |     if (info.newcontext == NULL) { | 
					
						
							|  |  |  |         _Py_ext_module_loader_info_clear(&info); | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     if (filename != NULL) { | 
					
						
							|  |  |  |         if (!PyUnicode_Check(filename)) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                             "module filename must be a string"); | 
					
						
							|  |  |  |             _Py_ext_module_loader_info_clear(&info); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         info.filename = Py_NewRef(filename); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MS_WINDOWS
 | 
					
						
							|  |  |  |         info.filename_encoded = PyUnicode_EncodeFSDefault(info.filename); | 
					
						
							|  |  |  |         if (info.filename_encoded == NULL) { | 
					
						
							|  |  |  |             _Py_ext_module_loader_info_clear(&info); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         info.path = info.filename; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         info.path = info.name; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *p_info = info; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 12:53:04 -06:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_ext_module_loader_info_init_for_builtin( | 
					
						
							|  |  |  |                             struct _Py_ext_module_loader_info *info, | 
					
						
							|  |  |  |                             PyObject *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(PyUnicode_Check(name)); | 
					
						
							|  |  |  |     assert(PyUnicode_FindChar(name, '.', 0, PyUnicode_GetLength(name), -1) == -1); | 
					
						
							|  |  |  |     assert(PyUnicode_GetLength(name) > 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *name_encoded = PyUnicode_AsEncodedString(name, "ascii", NULL); | 
					
						
							|  |  |  |     if (name_encoded == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *info = (struct _Py_ext_module_loader_info){ | 
					
						
							|  |  |  |         .name=Py_NewRef(name), | 
					
						
							|  |  |  |         .name_encoded=name_encoded, | 
					
						
							|  |  |  |         /* We won't need filename. */ | 
					
						
							|  |  |  |         .path=name, | 
					
						
							| 
									
										
										
										
											2024-05-04 15:24:02 -06:00
										 |  |  |         .origin=_Py_ext_module_origin_BUILTIN, | 
					
						
							| 
									
										
										
										
											2024-04-29 12:53:04 -06:00
										 |  |  |         .hook_prefix=ascii_only_prefix, | 
					
						
							|  |  |  |         .newcontext=NULL, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-04 15:24:02 -06:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_ext_module_loader_info_init_for_core( | 
					
						
							|  |  |  |                             struct _Py_ext_module_loader_info *info, | 
					
						
							|  |  |  |                             PyObject *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (_Py_ext_module_loader_info_init_for_builtin(info, name) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     info->origin = _Py_ext_module_origin_CORE; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_ext_module_loader_info_init_from_spec( | 
					
						
							|  |  |  |                             struct _Py_ext_module_loader_info *p_info, | 
					
						
							|  |  |  |                             PyObject *spec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *name = PyObject_GetAttrString(spec, "name"); | 
					
						
							|  |  |  |     if (name == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyObject *filename = PyObject_GetAttrString(spec, "origin"); | 
					
						
							| 
									
										
										
										
											2024-04-23 08:25:50 -06:00
										 |  |  |     if (filename == NULL) { | 
					
						
							| 
									
										
										
										
											2024-04-24 15:23:45 -06:00
										 |  |  |         Py_DECREF(name); | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-05-04 15:24:02 -06:00
										 |  |  |     /* We could also accommodate builtin modules here without much trouble. */ | 
					
						
							|  |  |  |     _Py_ext_module_origin origin = _Py_ext_module_origin_DYNAMIC; | 
					
						
							|  |  |  |     int err = _Py_ext_module_loader_info_init(p_info, name, filename, origin); | 
					
						
							| 
									
										
										
										
											2024-04-24 15:23:45 -06:00
										 |  |  |     Py_DECREF(name); | 
					
						
							|  |  |  |     Py_DECREF(filename); | 
					
						
							|  |  |  |     return err; | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  | /********************************/ | 
					
						
							|  |  |  | /* module init function results */ | 
					
						
							|  |  |  | /********************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_ext_module_loader_result_clear(struct _Py_ext_module_loader_result *res) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Instead, the caller should have called
 | 
					
						
							|  |  |  |      * _Py_ext_module_loader_result_apply_error(). */ | 
					
						
							|  |  |  |     assert(res->err == NULL); | 
					
						
							|  |  |  |     *res = (struct _Py_ext_module_loader_result){0}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                             struct _Py_ext_module_loader_result *res, | 
					
						
							|  |  |  |                             enum _Py_ext_module_loader_result_error_kind kind) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  |     switch (kind) { | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_EXCEPTION:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: | 
					
						
							|  |  |  |         assert(PyErr_Occurred()); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNINITIALIZED:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NOT_MODULE:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING_DEF: | 
					
						
							|  |  |  |         assert(!PyErr_Occurred()); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         /* We added a new error kind but forgot to add it to this switch. */ | 
					
						
							|  |  |  |         assert(0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(res->err == NULL && res->_err.exc == NULL); | 
					
						
							|  |  |  |     res->err = &res->_err; | 
					
						
							|  |  |  |     *res->err = (struct _Py_ext_module_loader_result_error){ | 
					
						
							|  |  |  |         .kind=kind, | 
					
						
							|  |  |  |         .exc=PyErr_GetRaisedException(), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* For some kinds, we also set/check res->kind. */ | 
					
						
							|  |  |  |     switch (kind) { | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNINITIALIZED: | 
					
						
							|  |  |  |         assert(res->kind == _Py_ext_module_kind_UNKNOWN); | 
					
						
							|  |  |  |         res->kind = _Py_ext_module_kind_INVALID; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     /* None of the rest affect the result kind. */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_EXCEPTION:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NOT_MODULE:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING_DEF: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         /* We added a new error kind but forgot to add it to this switch. */ | 
					
						
							|  |  |  |         assert(0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_ext_module_loader_result_apply_error( | 
					
						
							|  |  |  |                             struct _Py_ext_module_loader_result *res, | 
					
						
							|  |  |  |                             const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(!PyErr_Occurred()); | 
					
						
							|  |  |  |     assert(res->err != NULL && res->err == &res->_err); | 
					
						
							|  |  |  |     struct _Py_ext_module_loader_result_error err = *res->err; | 
					
						
							|  |  |  |     res->err = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* We're otherwise done with the result at this point. */ | 
					
						
							|  |  |  |     _Py_ext_module_loader_result_clear(res); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  |     switch (err.kind) { | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_EXCEPTION:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: | 
					
						
							|  |  |  |         assert(err.exc != NULL); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNINITIALIZED:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NOT_MODULE:  /* fall through */ | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING_DEF: | 
					
						
							|  |  |  |         assert(err.exc == NULL); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         /* We added a new error kind but forgot to add it to this switch. */ | 
					
						
							|  |  |  |         assert(0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const char *msg = NULL; | 
					
						
							|  |  |  |     switch (err.kind) { | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_EXCEPTION: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING: | 
					
						
							|  |  |  |         msg = "initialization of %s failed without raising an exception"; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: | 
					
						
							|  |  |  |         msg = "initialization of %s raised unreported exception"; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_UNINITIALIZED: | 
					
						
							|  |  |  |         msg = "init function of %s returned uninitialized object"; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: | 
					
						
							|  |  |  |         msg = "initialization of %s did not return PyModuleDef"; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_NOT_MODULE: | 
					
						
							|  |  |  |         msg = "initialization of %s did not return an extension module"; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case _Py_ext_module_loader_result_ERR_MISSING_DEF: | 
					
						
							|  |  |  |         msg = "initialization of %s did not return a valid extension module"; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         /* We added a new error kind but forgot to add it to this switch. */ | 
					
						
							|  |  |  |         assert(0); | 
					
						
							|  |  |  |         PyErr_Format(PyExc_SystemError, | 
					
						
							|  |  |  |                      "loading %s failed due to init function", name); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (err.exc != NULL) { | 
					
						
							|  |  |  |         PyErr_SetRaisedException(err.exc); | 
					
						
							|  |  |  |         err.exc = NULL;  /* PyErr_SetRaisedException() stole our reference. */ | 
					
						
							|  |  |  |         if (msg != NULL) { | 
					
						
							|  |  |  |             _PyErr_FormatFromCause(PyExc_SystemError, msg, name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         assert(msg != NULL); | 
					
						
							|  |  |  |         PyErr_Format(PyExc_SystemError, msg, name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************/ | 
					
						
							|  |  |  | /* getting/running the module init function */ | 
					
						
							|  |  |  | /********************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  | PyModInitFunction | 
					
						
							|  |  |  | _PyImport_GetModInitFunc(struct _Py_ext_module_loader_info *info, | 
					
						
							|  |  |  |                          FILE *fp) | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     const char *name_buf = PyBytes_AS_STRING(info->name_encoded); | 
					
						
							|  |  |  |     dl_funcptr exportfunc; | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2024-04-23 08:25:50 -06:00
										 |  |  |     exportfunc = _PyImport_FindSharedFuncptrWindows( | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |             info->hook_prefix, name_buf, info->filename, fp); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     { | 
					
						
							|  |  |  |         const char *path_buf = PyBytes_AS_STRING(info->filename_encoded); | 
					
						
							|  |  |  |         exportfunc = _PyImport_FindSharedFuncptr( | 
					
						
							|  |  |  |                         info->hook_prefix, name_buf, path_buf, fp); | 
					
						
							| 
									
										
										
										
											2024-04-23 08:25:50 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											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)", | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |                 info->hook_prefix, name_buf); | 
					
						
							|  |  |  |             if (msg != NULL) { | 
					
						
							|  |  |  |                 PyErr_SetImportError(msg, info->name, info->filename); | 
					
						
							|  |  |  |                 Py_DECREF(msg); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     return (PyModInitFunction)exportfunc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _PyImport_RunModInitFunc(PyModInitFunction p0, | 
					
						
							|  |  |  |                          struct _Py_ext_module_loader_info *info, | 
					
						
							|  |  |  |                          struct _Py_ext_module_loader_result *p_res) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |     struct _Py_ext_module_loader_result res = { | 
					
						
							|  |  |  |         .kind=_Py_ext_module_kind_UNKNOWN, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Call the module init function. */ | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Package context is needed for single-phase init */ | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     const char *oldcontext = _PyImport_SwapPackageContext(info->newcontext); | 
					
						
							|  |  |  |     PyObject *m = p0(); | 
					
						
							| 
									
										
										
										
											2023-02-15 15:32:31 -07:00
										 |  |  |     _PyImport_SwapPackageContext(oldcontext); | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     /* Validate the result (and populate "res". */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     if (m == NULL) { | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |         if (PyErr_Occurred()) { | 
					
						
							|  |  |  |             _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                         &res, _Py_ext_module_loader_result_EXCEPTION); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                         &res, _Py_ext_module_loader_result_ERR_MISSING); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         } | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } else if (PyErr_Occurred()) { | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |         _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                 &res, _Py_ext_module_loader_result_ERR_UNREPORTED_EXC); | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |         /* We would probably be correct to decref m here,
 | 
					
						
							|  |  |  |          * but we weren't doing so before, | 
					
						
							|  |  |  |          * so we stick with doing nothing. */ | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         m = NULL; | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 07:15:20 -06:00
										 |  |  |     if (Py_IS_TYPE(m, NULL)) { | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         /* This can happen when a PyModuleDef is returned without calling
 | 
					
						
							|  |  |  |          * PyModuleDef_Init on it | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |         _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                 &res, _Py_ext_module_loader_result_ERR_UNINITIALIZED); | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |         /* Likewise, decref'ing here makes sense.  However, the original
 | 
					
						
							|  |  |  |          * code has a note about "prevent segfault in DECREF", | 
					
						
							|  |  |  |          * so we play it safe and leave it alone. */ | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         m = NULL; /* prevent segfault in DECREF */ | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     if (PyObject_TypeCheck(m, &PyModuleDef_Type)) { | 
					
						
							|  |  |  |         /* multi-phase init */ | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |         res.kind = _Py_ext_module_kind_MULTIPHASE; | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |         res.def = (PyModuleDef *)m; | 
					
						
							|  |  |  |         /* Run PyModule_FromDefAndSpec() to finish loading the module. */ | 
					
						
							| 
									
										
										
										
											2023-02-15 18:16:00 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     else if (info->hook_prefix == nonascii_prefix) { | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |         /* Non-ASCII is only supported for multi-phase init. */ | 
					
						
							|  |  |  |         res.kind = _Py_ext_module_kind_MULTIPHASE; | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |         /* Don't allow legacy init for non-ASCII module names. */ | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |         _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                 &res, _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE); | 
					
						
							|  |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2024-04-24 09:55:48 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     else { | 
					
						
							|  |  |  |         /* single-phase init (legacy) */ | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |         res.kind = _Py_ext_module_kind_SINGLEPHASE; | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |         res.module = m; | 
					
						
							| 
									
										
										
										
											2024-04-24 09:55:48 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 12:53:04 -06:00
										 |  |  |         if (!PyModule_Check(m)) { | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |             _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                     &res, _Py_ext_module_loader_result_ERR_NOT_MODULE); | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-29 12:53:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         res.def = _PyModule_GetDef(m); | 
					
						
							|  |  |  |         if (res.def == NULL) { | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |             PyErr_Clear(); | 
					
						
							|  |  |  |             _Py_ext_module_loader_result_set_error( | 
					
						
							|  |  |  |                     &res, _Py_ext_module_loader_result_ERR_MISSING_DEF); | 
					
						
							| 
									
										
										
										
											2024-04-29 12:53:04 -06:00
										 |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-24 11:42:01 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     assert(!PyErr_Occurred()); | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |     assert(res.err == NULL); | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     *p_res = res; | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |     assert(!PyErr_Occurred()); | 
					
						
							|  |  |  |     assert(res.err != NULL); | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     Py_CLEAR(res.module); | 
					
						
							|  |  |  |     res.def = NULL; | 
					
						
							|  |  |  |     *p_res = res; | 
					
						
							| 
									
										
										
										
											2024-05-01 17:40:28 -06:00
										 |  |  |     p_res->err = &p_res->_err; | 
					
						
							| 
									
										
										
										
											2024-04-29 09:29:07 -06:00
										 |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											1998-08-04 22:46:29 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif /* HAVE_DYNAMIC_LOADING */
 |