/src/cpython3/Python/importdl.c
Line | Count | Source |
1 | | |
2 | | /* Support for dynamic loading of extension modules */ |
3 | | |
4 | | #include "Python.h" |
5 | | #include "pycore_call.h" // _PyObject_CallMethod() |
6 | | #include "pycore_import.h" // _PyImport_SwapPackageContext() |
7 | | #include "pycore_importdl.h" |
8 | | #include "pycore_moduleobject.h" // _PyModule_GetDefOrNull() |
9 | | #include "pycore_pyerrors.h" // _PyErr_FormatFromCause() |
10 | | #include "pycore_runtime.h" // _Py_ID() |
11 | | |
12 | | |
13 | | /***********************************/ |
14 | | /* module info to use when loading */ |
15 | | /***********************************/ |
16 | | |
17 | | static const struct hook_prefixes ascii_only_prefixes = { |
18 | | "PyInit", "PyModExport"}; |
19 | | static const struct hook_prefixes nonascii_prefixes = { |
20 | | "PyInitU", "PyModExportU"}; |
21 | | |
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 | 28 | get_encoded_name(PyObject *name, const struct hook_prefixes **hook_prefixes) { |
30 | 28 | PyObject *tmp; |
31 | 28 | PyObject *encoded = NULL; |
32 | 28 | PyObject *modname = NULL; |
33 | 28 | Py_ssize_t name_len, lastdot; |
34 | | |
35 | | /* Get the short name (substring after last dot) */ |
36 | 28 | name_len = PyUnicode_GetLength(name); |
37 | 28 | if (name_len < 0) { |
38 | 0 | return NULL; |
39 | 0 | } |
40 | 28 | lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1); |
41 | 28 | if (lastdot < -1) { |
42 | 0 | return NULL; |
43 | 28 | } else if (lastdot >= 0) { |
44 | 0 | tmp = PyUnicode_Substring(name, lastdot + 1, name_len); |
45 | 0 | if (tmp == NULL) |
46 | 0 | return NULL; |
47 | 0 | name = tmp; |
48 | | /* "name" now holds a new reference to the substring */ |
49 | 28 | } else { |
50 | 28 | Py_INCREF(name); |
51 | 28 | } |
52 | | |
53 | | /* Encode to ASCII or Punycode, as needed */ |
54 | 28 | encoded = PyUnicode_AsEncodedString(name, "ascii", NULL); |
55 | 28 | if (encoded != NULL) { |
56 | 28 | *hook_prefixes = &ascii_only_prefixes; |
57 | 28 | } else { |
58 | 0 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
59 | 0 | PyErr_Clear(); |
60 | 0 | encoded = PyUnicode_AsEncodedString(name, "punycode", NULL); |
61 | 0 | if (encoded == NULL) { |
62 | 0 | goto error; |
63 | 0 | } |
64 | 0 | *hook_prefixes = &nonascii_prefixes; |
65 | 0 | } else { |
66 | 0 | goto error; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | /* Replace '-' by '_' */ |
71 | 28 | modname = _PyObject_CallMethod(encoded, &_Py_ID(replace), "cc", '-', '_'); |
72 | 28 | if (modname == NULL) |
73 | 0 | goto error; |
74 | | |
75 | 28 | Py_DECREF(name); |
76 | 28 | Py_DECREF(encoded); |
77 | 28 | return modname; |
78 | 0 | error: |
79 | 0 | Py_DECREF(name); |
80 | 0 | Py_XDECREF(encoded); |
81 | 0 | return NULL; |
82 | 28 | } |
83 | | |
84 | | void |
85 | | _Py_ext_module_loader_info_clear(struct _Py_ext_module_loader_info *info) |
86 | 348 | { |
87 | 348 | Py_CLEAR(info->filename); |
88 | 348 | #ifndef MS_WINDOWS |
89 | 348 | Py_CLEAR(info->filename_encoded); |
90 | 348 | #endif |
91 | 348 | Py_CLEAR(info->name); |
92 | 348 | Py_CLEAR(info->name_encoded); |
93 | 348 | } |
94 | | |
95 | | int |
96 | | _Py_ext_module_loader_info_init(struct _Py_ext_module_loader_info *p_info, |
97 | | PyObject *name, PyObject *filename, |
98 | | _Py_ext_module_origin origin) |
99 | 28 | { |
100 | 28 | struct _Py_ext_module_loader_info info = { |
101 | 28 | .origin=origin, |
102 | 28 | }; |
103 | | |
104 | 28 | assert(name != NULL); |
105 | 28 | if (!PyUnicode_Check(name)) { |
106 | 0 | PyErr_SetString(PyExc_TypeError, |
107 | 0 | "module name must be a string"); |
108 | 0 | _Py_ext_module_loader_info_clear(&info); |
109 | 0 | return -1; |
110 | 0 | } |
111 | 28 | assert(PyUnicode_GetLength(name) > 0); |
112 | 28 | info.name = Py_NewRef(name); |
113 | | |
114 | 28 | info.name_encoded = get_encoded_name(info.name, &info.hook_prefixes); |
115 | 28 | if (info.name_encoded == NULL) { |
116 | 0 | _Py_ext_module_loader_info_clear(&info); |
117 | 0 | return -1; |
118 | 0 | } |
119 | | |
120 | 28 | info.newcontext = PyUnicode_AsUTF8(info.name); |
121 | 28 | if (info.newcontext == NULL) { |
122 | 0 | _Py_ext_module_loader_info_clear(&info); |
123 | 0 | return -1; |
124 | 0 | } |
125 | | |
126 | 28 | if (filename != NULL) { |
127 | 28 | if (!PyUnicode_Check(filename)) { |
128 | 0 | PyErr_SetString(PyExc_TypeError, |
129 | 0 | "module filename must be a string"); |
130 | 0 | _Py_ext_module_loader_info_clear(&info); |
131 | 0 | return -1; |
132 | 0 | } |
133 | 28 | info.filename = Py_NewRef(filename); |
134 | | |
135 | 28 | #ifndef MS_WINDOWS |
136 | 28 | info.filename_encoded = PyUnicode_EncodeFSDefault(info.filename); |
137 | 28 | if (info.filename_encoded == NULL) { |
138 | 0 | _Py_ext_module_loader_info_clear(&info); |
139 | 0 | return -1; |
140 | 0 | } |
141 | 28 | #endif |
142 | | |
143 | 28 | info.path = info.filename; |
144 | 28 | } |
145 | 0 | else { |
146 | 0 | info.path = info.name; |
147 | 0 | } |
148 | | |
149 | 28 | *p_info = info; |
150 | 28 | return 0; |
151 | 28 | } |
152 | | |
153 | | int |
154 | | _Py_ext_module_loader_info_init_for_builtin( |
155 | | struct _Py_ext_module_loader_info *info, |
156 | | PyObject *name) |
157 | 320 | { |
158 | 320 | assert(PyUnicode_Check(name)); |
159 | 320 | assert(PyUnicode_FindChar(name, '.', 0, PyUnicode_GetLength(name), -1) == -1); |
160 | 320 | assert(PyUnicode_GetLength(name) > 0); |
161 | | |
162 | 320 | PyObject *name_encoded = PyUnicode_AsEncodedString(name, "ascii", NULL); |
163 | 320 | if (name_encoded == NULL) { |
164 | 0 | return -1; |
165 | 0 | } |
166 | | |
167 | 320 | *info = (struct _Py_ext_module_loader_info){ |
168 | 320 | .name=Py_NewRef(name), |
169 | 320 | .name_encoded=name_encoded, |
170 | | /* We won't need filename. */ |
171 | 320 | .path=name, |
172 | 320 | .origin=_Py_ext_module_origin_BUILTIN, |
173 | 320 | .hook_prefixes=&ascii_only_prefixes, |
174 | 320 | .newcontext=NULL, |
175 | 320 | }; |
176 | 320 | return 0; |
177 | 320 | } |
178 | | |
179 | | int |
180 | | _Py_ext_module_loader_info_init_for_core( |
181 | | struct _Py_ext_module_loader_info *info, |
182 | | PyObject *name) |
183 | 0 | { |
184 | 0 | if (_Py_ext_module_loader_info_init_for_builtin(info, name) < 0) { |
185 | 0 | return -1; |
186 | 0 | } |
187 | 0 | info->origin = _Py_ext_module_origin_CORE; |
188 | 0 | return 0; |
189 | 0 | } |
190 | | |
191 | | #ifdef HAVE_DYNAMIC_LOADING |
192 | | int |
193 | | _Py_ext_module_loader_info_init_from_spec( |
194 | | struct _Py_ext_module_loader_info *p_info, |
195 | | PyObject *spec) |
196 | 28 | { |
197 | 28 | PyObject *name = PyObject_GetAttrString(spec, "name"); |
198 | 28 | if (name == NULL) { |
199 | 0 | return -1; |
200 | 0 | } |
201 | 28 | PyObject *filename = PyObject_GetAttrString(spec, "origin"); |
202 | 28 | if (filename == NULL) { |
203 | 0 | Py_DECREF(name); |
204 | 0 | return -1; |
205 | 0 | } |
206 | | /* We could also accommodate builtin modules here without much trouble. */ |
207 | 28 | _Py_ext_module_origin origin = _Py_ext_module_origin_DYNAMIC; |
208 | 28 | int err = _Py_ext_module_loader_info_init(p_info, name, filename, origin); |
209 | 28 | Py_DECREF(name); |
210 | 28 | Py_DECREF(filename); |
211 | 28 | return err; |
212 | 28 | } |
213 | | #endif /* HAVE_DYNAMIC_LOADING */ |
214 | | |
215 | | |
216 | | /********************************/ |
217 | | /* module init function results */ |
218 | | /********************************/ |
219 | | |
220 | | void |
221 | | _Py_ext_module_loader_result_clear(struct _Py_ext_module_loader_result *res) |
222 | 348 | { |
223 | | /* Instead, the caller should have called |
224 | | * _Py_ext_module_loader_result_apply_error(). */ |
225 | 348 | assert(res->err == NULL); |
226 | 348 | *res = (struct _Py_ext_module_loader_result){0}; |
227 | 348 | } |
228 | | |
229 | | static void |
230 | | _Py_ext_module_loader_result_set_error( |
231 | | struct _Py_ext_module_loader_result *res, |
232 | | enum _Py_ext_module_loader_result_error_kind kind) |
233 | 0 | { |
234 | 0 | #ifndef NDEBUG |
235 | 0 | switch (kind) { |
236 | 0 | case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH; |
237 | 0 | case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: |
238 | 0 | assert(PyErr_Occurred()); |
239 | 0 | break; |
240 | 0 | case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH; |
241 | 0 | case _Py_ext_module_loader_result_ERR_UNINITIALIZED: _Py_FALLTHROUGH; |
242 | 0 | case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH; |
243 | 0 | case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH; |
244 | 0 | case _Py_ext_module_loader_result_ERR_MISSING_DEF: |
245 | 0 | assert(!PyErr_Occurred()); |
246 | 0 | break; |
247 | 0 | default: |
248 | | /* We added a new error kind but forgot to add it to this switch. */ |
249 | 0 | assert(0); |
250 | 0 | } |
251 | 0 | #endif |
252 | | |
253 | 0 | assert(res->err == NULL && res->_err.exc == NULL); |
254 | 0 | res->err = &res->_err; |
255 | 0 | *res->err = (struct _Py_ext_module_loader_result_error){ |
256 | 0 | .kind=kind, |
257 | 0 | .exc=PyErr_GetRaisedException(), |
258 | 0 | }; |
259 | | |
260 | | /* For some kinds, we also set/check res->kind. */ |
261 | 0 | switch (kind) { |
262 | 0 | case _Py_ext_module_loader_result_ERR_UNINITIALIZED: |
263 | 0 | assert(res->kind == _Py_ext_module_kind_UNKNOWN); |
264 | 0 | res->kind = _Py_ext_module_kind_INVALID; |
265 | 0 | break; |
266 | | /* None of the rest affect the result kind. */ |
267 | 0 | case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH; |
268 | 0 | case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH; |
269 | 0 | case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: _Py_FALLTHROUGH; |
270 | 0 | case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH; |
271 | 0 | case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH; |
272 | 0 | case _Py_ext_module_loader_result_ERR_MISSING_DEF: |
273 | 0 | break; |
274 | 0 | default: |
275 | | /* We added a new error kind but forgot to add it to this switch. */ |
276 | 0 | assert(0); |
277 | 0 | } |
278 | 0 | } |
279 | | |
280 | | void |
281 | | _Py_ext_module_loader_result_apply_error( |
282 | | struct _Py_ext_module_loader_result *res, |
283 | | const char *name) |
284 | 0 | { |
285 | 0 | assert(!PyErr_Occurred()); |
286 | 0 | assert(res->err != NULL && res->err == &res->_err); |
287 | 0 | struct _Py_ext_module_loader_result_error err = *res->err; |
288 | 0 | res->err = NULL; |
289 | | |
290 | | /* We're otherwise done with the result at this point. */ |
291 | 0 | _Py_ext_module_loader_result_clear(res); |
292 | |
|
293 | 0 | #ifndef NDEBUG |
294 | 0 | switch (err.kind) { |
295 | 0 | case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH; |
296 | 0 | case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: |
297 | 0 | assert(err.exc != NULL); |
298 | 0 | break; |
299 | 0 | case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH; |
300 | 0 | case _Py_ext_module_loader_result_ERR_UNINITIALIZED: _Py_FALLTHROUGH; |
301 | 0 | case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH; |
302 | 0 | case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH; |
303 | 0 | case _Py_ext_module_loader_result_ERR_MISSING_DEF: |
304 | 0 | assert(err.exc == NULL); |
305 | 0 | break; |
306 | 0 | default: |
307 | | /* We added a new error kind but forgot to add it to this switch. */ |
308 | 0 | assert(0); |
309 | 0 | } |
310 | 0 | #endif |
311 | | |
312 | 0 | const char *msg = NULL; |
313 | 0 | switch (err.kind) { |
314 | 0 | case _Py_ext_module_loader_result_EXCEPTION: |
315 | 0 | break; |
316 | 0 | case _Py_ext_module_loader_result_ERR_MISSING: |
317 | 0 | msg = "initialization of %s failed without raising an exception"; |
318 | 0 | break; |
319 | 0 | case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: |
320 | 0 | msg = "initialization of %s raised unreported exception"; |
321 | 0 | break; |
322 | 0 | case _Py_ext_module_loader_result_ERR_UNINITIALIZED: |
323 | 0 | msg = "init function of %s returned uninitialized object"; |
324 | 0 | break; |
325 | 0 | case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: |
326 | 0 | msg = "initialization of %s did not return PyModuleDef"; |
327 | 0 | break; |
328 | 0 | case _Py_ext_module_loader_result_ERR_NOT_MODULE: |
329 | 0 | msg = "initialization of %s did not return an extension module"; |
330 | 0 | break; |
331 | 0 | case _Py_ext_module_loader_result_ERR_MISSING_DEF: |
332 | 0 | msg = "initialization of %s did not return a valid extension module"; |
333 | 0 | break; |
334 | 0 | default: |
335 | | /* We added a new error kind but forgot to add it to this switch. */ |
336 | 0 | assert(0); |
337 | 0 | PyErr_Format(PyExc_SystemError, |
338 | 0 | "loading %s failed due to init function", name); |
339 | 0 | return; |
340 | 0 | } |
341 | | |
342 | 0 | if (err.exc != NULL) { |
343 | 0 | PyErr_SetRaisedException(err.exc); |
344 | 0 | err.exc = NULL; /* PyErr_SetRaisedException() stole our reference. */ |
345 | 0 | if (msg != NULL) { |
346 | 0 | _PyErr_FormatFromCause(PyExc_SystemError, msg, name); |
347 | 0 | } |
348 | 0 | } |
349 | 0 | else { |
350 | 0 | assert(msg != NULL); |
351 | 0 | PyErr_Format(PyExc_SystemError, msg, name); |
352 | 0 | } |
353 | 0 | } |
354 | | |
355 | | |
356 | | /********************************************/ |
357 | | /* getting/running the module init function */ |
358 | | /********************************************/ |
359 | | |
360 | | #ifdef HAVE_DYNAMIC_LOADING |
361 | | static dl_funcptr |
362 | | findfuncptr(const char *prefix, const char *name_buf, |
363 | | struct _Py_ext_module_loader_info *info, |
364 | | FILE *fp) |
365 | 56 | { |
366 | | #ifdef MS_WINDOWS |
367 | | return _PyImport_FindSharedFuncptrWindows( |
368 | | prefix, name_buf, info->filename, fp); |
369 | | #else |
370 | 56 | const char *path_buf = PyBytes_AS_STRING(info->filename_encoded); |
371 | 56 | return _PyImport_FindSharedFuncptr( |
372 | 56 | prefix, name_buf, path_buf, fp); |
373 | 56 | #endif |
374 | 56 | } |
375 | | |
376 | | int |
377 | | _PyImport_GetModuleExportHooks( |
378 | | struct _Py_ext_module_loader_info *info, |
379 | | FILE *fp, |
380 | | PyModInitFunction *modinit, |
381 | | PyModExportFunction *modexport) |
382 | 28 | { |
383 | 28 | *modinit = NULL; |
384 | 28 | *modexport = NULL; |
385 | | |
386 | 28 | const char *name_buf = PyBytes_AS_STRING(info->name_encoded); |
387 | 28 | dl_funcptr exportfunc; |
388 | | |
389 | 28 | exportfunc = findfuncptr( |
390 | 28 | info->hook_prefixes->export_prefix, |
391 | 28 | name_buf, info, fp); |
392 | 28 | if (exportfunc) { |
393 | 0 | *modexport = (PyModExportFunction)exportfunc; |
394 | 0 | return 2; |
395 | 0 | } |
396 | 28 | if (PyErr_Occurred()) { |
397 | 0 | return -1; |
398 | 0 | } |
399 | | |
400 | 28 | exportfunc = findfuncptr( |
401 | 28 | info->hook_prefixes->init_prefix, |
402 | 28 | name_buf, info, fp); |
403 | 28 | if (exportfunc) { |
404 | 28 | *modinit = (PyModInitFunction)exportfunc; |
405 | 28 | return 1; |
406 | 28 | } |
407 | | |
408 | 0 | if (!PyErr_Occurred()) { |
409 | 0 | PyObject *msg; |
410 | 0 | msg = PyUnicode_FromFormat( |
411 | 0 | "dynamic module does not define " |
412 | 0 | "module export function (%s_%s or %s_%s)", |
413 | 0 | info->hook_prefixes->export_prefix, name_buf, |
414 | 0 | info->hook_prefixes->init_prefix, name_buf); |
415 | 0 | if (msg != NULL) { |
416 | 0 | PyErr_SetImportError(msg, info->name, info->filename); |
417 | 0 | Py_DECREF(msg); |
418 | 0 | } |
419 | 0 | } |
420 | 0 | return -1; |
421 | 28 | } |
422 | | #endif /* HAVE_DYNAMIC_LOADING */ |
423 | | |
424 | | int |
425 | | _PyImport_RunModInitFunc(PyModInitFunction p0, |
426 | | struct _Py_ext_module_loader_info *info, |
427 | | struct _Py_ext_module_loader_result *p_res) |
428 | 348 | { |
429 | 348 | struct _Py_ext_module_loader_result res = { |
430 | 348 | .kind=_Py_ext_module_kind_UNKNOWN, |
431 | 348 | }; |
432 | | |
433 | | /* Call the module init function. */ |
434 | | |
435 | | /* Package context is needed for single-phase init */ |
436 | 348 | const char *oldcontext = _PyImport_SwapPackageContext(info->newcontext); |
437 | 348 | PyObject *m = p0(); |
438 | 348 | _PyImport_SwapPackageContext(oldcontext); |
439 | | |
440 | | /* Validate the result (and populate "res". */ |
441 | | |
442 | 348 | if (m == NULL) { |
443 | | /* The init func for multi-phase init modules is expected |
444 | | * to return a PyModuleDef after calling PyModuleDef_Init(). |
445 | | * That function never raises an exception nor returns NULL, |
446 | | * so at this point it must be a single-phase init modules. */ |
447 | 0 | res.kind = _Py_ext_module_kind_SINGLEPHASE; |
448 | 0 | if (PyErr_Occurred()) { |
449 | 0 | _Py_ext_module_loader_result_set_error( |
450 | 0 | &res, _Py_ext_module_loader_result_EXCEPTION); |
451 | 0 | } |
452 | 0 | else { |
453 | 0 | _Py_ext_module_loader_result_set_error( |
454 | 0 | &res, _Py_ext_module_loader_result_ERR_MISSING); |
455 | 0 | } |
456 | 0 | goto error; |
457 | 348 | } else if (PyErr_Occurred()) { |
458 | | /* Likewise, we infer that this is a single-phase init module. */ |
459 | 0 | res.kind = _Py_ext_module_kind_SINGLEPHASE; |
460 | 0 | _Py_ext_module_loader_result_set_error( |
461 | 0 | &res, _Py_ext_module_loader_result_ERR_UNREPORTED_EXC); |
462 | | /* We would probably be correct to decref m here, |
463 | | * but we weren't doing so before, |
464 | | * so we stick with doing nothing. */ |
465 | 0 | m = NULL; |
466 | 0 | goto error; |
467 | 0 | } |
468 | | |
469 | 348 | if (Py_IS_TYPE(m, NULL)) { |
470 | | /* This can happen when a PyModuleDef is returned without calling |
471 | | * PyModuleDef_Init on it |
472 | | */ |
473 | 0 | _Py_ext_module_loader_result_set_error( |
474 | 0 | &res, _Py_ext_module_loader_result_ERR_UNINITIALIZED); |
475 | | /* Likewise, decref'ing here makes sense. However, the original |
476 | | * code has a note about "prevent segfault in DECREF", |
477 | | * so we play it safe and leave it alone. */ |
478 | 0 | m = NULL; /* prevent segfault in DECREF */ |
479 | 0 | goto error; |
480 | 0 | } |
481 | | |
482 | 348 | if (PyObject_TypeCheck(m, &PyModuleDef_Type)) { |
483 | | /* multi-phase init */ |
484 | 348 | res.kind = _Py_ext_module_kind_MULTIPHASE; |
485 | 348 | res.def = (PyModuleDef *)m; |
486 | | /* Run PyModule_FromDefAndSpec() to finish loading the module. */ |
487 | 348 | } |
488 | 0 | else if (info->hook_prefixes == &nonascii_prefixes) { |
489 | | /* Non-ASCII is only supported for multi-phase init. */ |
490 | 0 | res.kind = _Py_ext_module_kind_MULTIPHASE; |
491 | | /* Don't allow legacy init for non-ASCII module names. */ |
492 | 0 | _Py_ext_module_loader_result_set_error( |
493 | 0 | &res, _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE); |
494 | 0 | goto error; |
495 | 0 | } |
496 | 0 | else { |
497 | | /* single-phase init (legacy) */ |
498 | 0 | res.kind = _Py_ext_module_kind_SINGLEPHASE; |
499 | 0 | res.module = m; |
500 | |
|
501 | 0 | if (!PyModule_Check(m)) { |
502 | 0 | _Py_ext_module_loader_result_set_error( |
503 | 0 | &res, _Py_ext_module_loader_result_ERR_NOT_MODULE); |
504 | 0 | goto error; |
505 | 0 | } |
506 | | |
507 | 0 | res.def = _PyModule_GetDefOrNull(m); |
508 | 0 | if (res.def == NULL) { |
509 | 0 | PyErr_Clear(); |
510 | 0 | _Py_ext_module_loader_result_set_error( |
511 | 0 | &res, _Py_ext_module_loader_result_ERR_MISSING_DEF); |
512 | 0 | goto error; |
513 | 0 | } |
514 | 0 | } |
515 | | |
516 | 348 | assert(!PyErr_Occurred()); |
517 | 348 | assert(res.err == NULL); |
518 | 348 | *p_res = res; |
519 | 348 | return 0; |
520 | | |
521 | 0 | error: |
522 | 0 | assert(!PyErr_Occurred()); |
523 | 0 | assert(res.err != NULL); |
524 | 0 | Py_CLEAR(res.module); |
525 | | res.def = NULL; |
526 | 0 | *p_res = res; |
527 | 0 | p_res->err = &p_res->_err; |
528 | 0 | return -1; |
529 | 0 | } |