/home/mdboom/Work/builds/cpython/Python/import.c
Line | Count | Source (jump to first uncovered line) |
1 | /* Module definition and import implementation */ |
2 | |
3 | #include "Python.h" |
4 | |
5 | #include "pycore_import.h" // _PyImport_BootstrapImp() |
6 | #include "pycore_initconfig.h" // _PyStatus_OK() |
7 | #include "pycore_interp.h" // _PyInterpreterState_ClearModules() |
8 | #include "pycore_namespace.h" // _PyNamespace_Type |
9 | #include "pycore_pyerrors.h" // _PyErr_SetString() |
10 | #include "pycore_pyhash.h" // _Py_KeyedHash() |
11 | #include "pycore_pylifecycle.h" |
12 | #include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() |
13 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
14 | #include "pycore_sysmodule.h" // _PySys_Audit() |
15 | #include "marshal.h" // PyMarshal_ReadObjectFromString() |
16 | #include "importdl.h" // _PyImport_DynLoadFiletab |
17 | #include "pydtrace.h" // PyDTrace_IMPORT_FIND_LOAD_START_ENABLED() |
18 | #include <stdbool.h> // bool |
19 | |
20 | #ifdef HAVE_FCNTL_H |
21 | #include <fcntl.h> |
22 | #endif |
23 | #ifdef __cplusplus |
24 | extern "C" { |
25 | #endif |
26 | |
27 | /* Forward references */ |
28 | static PyObject *import_add_module(PyThreadState *tstate, PyObject *name); |
29 | |
30 | /* See _PyImport_FixupExtensionObject() below */ |
31 | static PyObject *extensions = NULL; |
32 | |
33 | /* This table is defined in config.c: */ |
34 | extern struct _inittab _PyImport_Inittab[]; |
35 | |
36 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
37 | static struct _inittab *inittab_copy = NULL; |
38 | |
39 | /*[clinic input] |
40 | module _imp |
41 | [clinic start generated code]*/ |
42 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/ |
43 | |
44 | #include "clinic/import.c.h" |
45 | |
46 | /* Initialize things */ |
47 | |
48 | PyStatus |
49 | _PyImportZip_Init(PyThreadState *tstate) |
50 | { |
51 | PyObject *path_hooks; |
52 | int err = 0; |
53 | |
54 | path_hooks = PySys_GetObject("path_hooks"); |
55 | if (path_hooks == NULL) { Branch (55:9): [True: 0, False: 278]
|
56 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
57 | "unable to get sys.path_hooks"); |
58 | goto error; |
59 | } |
60 | |
61 | int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; |
62 | if (verbose) { Branch (62:9): [True: 4, False: 274]
|
63 | PySys_WriteStderr("# installing zipimport hook\n"); |
64 | } |
65 | |
66 | PyObject *zipimporter = _PyImport_GetModuleAttrString("zipimport", "zipimporter"); |
67 | if (zipimporter == NULL) { Branch (67:9): [True: 0, False: 278]
|
68 | _PyErr_Clear(tstate); /* No zipimporter object -- okay */ |
69 | if (verbose) { Branch (69:13): [True: 0, False: 0]
|
70 | PySys_WriteStderr("# can't import zipimport.zipimporter\n"); |
71 | } |
72 | } |
73 | else { |
74 | /* sys.path_hooks.insert(0, zipimporter) */ |
75 | err = PyList_Insert(path_hooks, 0, zipimporter); |
76 | Py_DECREF(zipimporter); |
77 | if (err < 0) { Branch (77:13): [True: 0, False: 278]
|
78 | goto error; |
79 | } |
80 | if (verbose) { Branch (80:13): [True: 4, False: 274]
|
81 | PySys_WriteStderr("# installed zipimport hook\n"); |
82 | } |
83 | } |
84 | |
85 | return _PyStatus_OK(); |
86 | |
87 | error: |
88 | PyErr_Print(); |
89 | return _PyStatus_ERR("initializing zipimport failed"); |
90 | } |
91 | |
92 | /* Locking primitives to prevent parallel imports of the same module |
93 | in different threads to return with a partially loaded module. |
94 | These calls are serialized by the global interpreter lock. */ |
95 | |
96 | static PyThread_type_lock import_lock = NULL; |
97 | static unsigned long import_lock_thread = PYTHREAD_INVALID_THREAD_ID; |
98 | static int import_lock_level = 0; |
99 | |
100 | void |
101 | _PyImport_AcquireLock(void) |
102 | { |
103 | unsigned long me = PyThread_get_thread_ident(); |
104 | if (me == PYTHREAD_INVALID_THREAD_ID) Branch (104:9): [True: 0, False: 59.5k]
|
105 | return; /* Too bad */ |
106 | if (import_lock == NULL) { Branch (106:9): [True: 107, False: 59.4k]
|
107 | import_lock = PyThread_allocate_lock(); |
108 | if (import_lock == NULL) Branch (108:13): [True: 0, False: 107]
|
109 | return; /* Nothing much we can do. */ |
110 | } |
111 | if (import_lock_thread == me) { Branch (111:9): [True: 63, False: 59.5k]
|
112 | import_lock_level++; |
113 | return; |
114 | } |
115 | if (import_lock_thread != PYTHREAD_INVALID_THREAD_ID || Branch (115:9): [True: 321, False: 59.2k]
|
116 | !PyThread_acquire_lock(import_lock, 0)59.2k ) Branch (116:9): [True: 356, False: 58.8k]
|
117 | { |
118 | PyThreadState *tstate = PyEval_SaveThread(); |
119 | PyThread_acquire_lock(import_lock, WAIT_LOCK); |
120 | PyEval_RestoreThread(tstate); |
121 | } |
122 | assert(import_lock_level == 0); |
123 | import_lock_thread = me; |
124 | import_lock_level = 1; |
125 | } |
126 | |
127 | int |
128 | _PyImport_ReleaseLock(void) |
129 | { |
130 | unsigned long me = PyThread_get_thread_ident(); |
131 | if (me == PYTHREAD_INVALID_THREAD_ID || import_lock == NULL) Branch (131:9): [True: 0, False: 59.5k]
Branch (131:45): [True: 0, False: 59.5k]
|
132 | return 0; /* Too bad */ |
133 | if (import_lock_thread != me) Branch (133:9): [True: 1, False: 59.5k]
|
134 | return -1; |
135 | import_lock_level--; |
136 | assert(import_lock_level >= 0); |
137 | if (import_lock_level == 0) { Branch (137:9): [True: 59.5k, False: 63]
|
138 | import_lock_thread = PYTHREAD_INVALID_THREAD_ID; |
139 | PyThread_release_lock(import_lock); |
140 | } |
141 | return 1; |
142 | } |
143 | |
144 | #ifdef HAVE_FORK |
145 | /* This function is called from PyOS_AfterFork_Child() to ensure that newly |
146 | created child processes do not share locks with the parent. |
147 | We now acquire the import lock around fork() calls but on some platforms |
148 | (Solaris 9 and earlier? see isue7242) that still left us with problems. */ |
149 | PyStatus |
150 | _PyImport_ReInitLock(void) |
151 | { |
152 | if (import_lock != NULL) { Branch (152:9): [True: 0, False: 0]
|
153 | if (_PyThread_at_fork_reinit(&import_lock) < 0) { Branch (153:13): [True: 0, False: 0]
|
154 | return _PyStatus_ERR("failed to create a new lock"); |
155 | } |
156 | } |
157 | |
158 | if (import_lock_level > 1) { Branch (158:9): [True: 0, False: 0]
|
159 | /* Forked as a side effect of import */ |
160 | unsigned long me = PyThread_get_thread_ident(); |
161 | PyThread_acquire_lock(import_lock, WAIT_LOCK); |
162 | import_lock_thread = me; |
163 | import_lock_level--; |
164 | } else { |
165 | import_lock_thread = PYTHREAD_INVALID_THREAD_ID; |
166 | import_lock_level = 0; |
167 | } |
168 | return _PyStatus_OK(); |
169 | } |
170 | #endif |
171 | |
172 | /*[clinic input] |
173 | _imp.lock_held |
174 | |
175 | Return True if the import lock is currently held, else False. |
176 | |
177 | On platforms without threads, return False. |
178 | [clinic start generated code]*/ |
179 | |
180 | static PyObject * |
181 | _imp_lock_held_impl(PyObject *module) |
182 | /*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/ |
183 | { |
184 | return PyBool_FromLong(import_lock_thread != PYTHREAD_INVALID_THREAD_ID); |
185 | } |
186 | |
187 | /*[clinic input] |
188 | _imp.acquire_lock |
189 | |
190 | Acquires the interpreter's import lock for the current thread. |
191 | |
192 | This lock should be used by import hooks to ensure thread-safety when importing |
193 | modules. On platforms without threads, this function does nothing. |
194 | [clinic start generated code]*/ |
195 | |
196 | static PyObject * |
197 | _imp_acquire_lock_impl(PyObject *module) |
198 | /*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/ |
199 | { |
200 | _PyImport_AcquireLock(); |
201 | Py_RETURN_NONE; |
202 | } |
203 | |
204 | /*[clinic input] |
205 | _imp.release_lock |
206 | |
207 | Release the interpreter's import lock. |
208 | |
209 | On platforms without threads, this function does nothing. |
210 | [clinic start generated code]*/ |
211 | |
212 | static PyObject * |
213 | _imp_release_lock_impl(PyObject *module) |
214 | /*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/ |
215 | { |
216 | if (_PyImport_ReleaseLock() < 0) { Branch (216:9): [True: 1, False: 58.6k]
|
217 | PyErr_SetString(PyExc_RuntimeError, |
218 | "not holding the import lock"); |
219 | return NULL; |
220 | } |
221 | Py_RETURN_NONE58.6k ; |
222 | } |
223 | |
224 | void |
225 | _PyImport_Fini(void) |
226 | { |
227 | Py_CLEAR(extensions); |
228 | if (import_lock != NULL) { Branch (228:9): [True: 104, False: 0]
|
229 | PyThread_free_lock(import_lock); |
230 | import_lock = NULL; |
231 | } |
232 | } |
233 | |
234 | void |
235 | _PyImport_Fini2(void) |
236 | { |
237 | /* Use the same memory allocator than PyImport_ExtendInittab(). */ |
238 | PyMemAllocatorEx old_alloc; |
239 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
240 | |
241 | // Reset PyImport_Inittab |
242 | PyImport_Inittab = _PyImport_Inittab; |
243 | |
244 | /* Free memory allocated by PyImport_ExtendInittab() */ |
245 | PyMem_RawFree(inittab_copy); |
246 | inittab_copy = NULL; |
247 | |
248 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
249 | } |
250 | |
251 | /* Helper for sys */ |
252 | |
253 | PyObject * |
254 | PyImport_GetModuleDict(void) |
255 | { |
256 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
257 | if (interp->modules == NULL) { Branch (257:9): [True: 0, False: 60]
|
258 | Py_FatalError("interpreter has no modules dictionary"); |
259 | } |
260 | return interp->modules; |
261 | } |
262 | |
263 | /* In some corner cases it is important to be sure that the import |
264 | machinery has been initialized (or not cleaned up yet). For |
265 | example, see issue #4236 and PyModule_Create2(). */ |
266 | |
267 | int |
268 | _PyImport_IsInitialized(PyInterpreterState *interp) |
269 | { |
270 | if (interp->modules == NULL) Branch (270:9): [True: 0, False: 346]
|
271 | return 0; |
272 | return 1; |
273 | } |
274 | |
275 | PyObject * |
276 | _PyImport_GetModuleId(_Py_Identifier *nameid) |
277 | { |
278 | PyObject *name = _PyUnicode_FromId(nameid); /* borrowed */ |
279 | if (name == NULL) { Branch (279:9): [True: 0, False: 73]
|
280 | return NULL; |
281 | } |
282 | return PyImport_GetModule(name); |
283 | } |
284 | |
285 | int |
286 | _PyImport_SetModule(PyObject *name, PyObject *m) |
287 | { |
288 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
289 | PyObject *modules = interp->modules; |
290 | return PyObject_SetItem(modules, name, m); |
291 | } |
292 | |
293 | int |
294 | _PyImport_SetModuleString(const char *name, PyObject *m) |
295 | { |
296 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
297 | PyObject *modules = interp->modules; |
298 | return PyMapping_SetItemString(modules, name, m); |
299 | } |
300 | |
301 | static PyObject * |
302 | import_get_module(PyThreadState *tstate, PyObject *name) |
303 | { |
304 | PyObject *modules = tstate->interp->modules; |
305 | if (modules == NULL) { Branch (305:9): [True: 0, False: 1.09M]
|
306 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
307 | "unable to get sys.modules"); |
308 | return NULL; |
309 | } |
310 | |
311 | PyObject *m; |
312 | Py_INCREF(modules); |
313 | if (PyDict_CheckExact(modules)) { |
314 | m = PyDict_GetItemWithError(modules, name); /* borrowed */ |
315 | Py_XINCREF(m); |
316 | } |
317 | else { |
318 | m = PyObject_GetItem(modules, name); |
319 | if (m == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { Branch (319:13): [True: 0, False: 0]
Branch (319:26): [True: 0, False: 0]
|
320 | _PyErr_Clear(tstate); |
321 | } |
322 | } |
323 | Py_DECREF(modules); |
324 | return m; |
325 | } |
326 | |
327 | |
328 | static int |
329 | import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name) |
330 | { |
331 | PyObject *spec; |
332 | |
333 | /* Optimization: only call _bootstrap._lock_unlock_module() if |
334 | __spec__._initializing is true. |
335 | NOTE: because of this, initializing must be set *before* |
336 | stuffing the new module in sys.modules. |
337 | */ |
338 | spec = PyObject_GetAttr(mod, &_Py_ID(__spec__)); |
339 | int busy = _PyModuleSpec_IsInitializing(spec); |
340 | Py_XDECREF(spec); |
341 | if (busy) { Branch (341:9): [True: 1.94k, False: 740k]
|
342 | /* Wait until module is done importing. */ |
343 | PyObject *value = _PyObject_CallMethodOneArg( |
344 | interp->importlib, &_Py_ID(_lock_unlock_module), name); |
345 | if (value == NULL) { Branch (345:13): [True: 0, False: 1.94k]
|
346 | return -1; |
347 | } |
348 | Py_DECREF(value); |
349 | } |
350 | return 0; |
351 | } |
352 | |
353 | |
354 | /* Helper for pythonrun.c -- return magic number and tag. */ |
355 | |
356 | long |
357 | PyImport_GetMagicNumber(void) |
358 | { |
359 | long res; |
360 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
361 | PyObject *external, *pyc_magic; |
362 |
|
363 | external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); |
364 | if (external == NULL) Branch (364:9): [True: 0, False: 0]
|
365 | return -1; |
366 | pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); |
367 | Py_DECREF(external); |
368 | if (pyc_magic == NULL) Branch (368:9): [True: 0, False: 0]
|
369 | return -1; |
370 | res = PyLong_AsLong(pyc_magic); |
371 | Py_DECREF(pyc_magic); |
372 | return res; |
373 | } |
374 | |
375 | |
376 | extern const char * _PySys_ImplCacheTag; |
377 | |
378 | const char * |
379 | PyImport_GetMagicTag(void) |
380 | { |
381 | return _PySys_ImplCacheTag; |
382 | } |
383 | |
384 | |
385 | /* Magic for extension modules (built-in as well as dynamically |
386 | loaded). To prevent initializing an extension module more than |
387 | once, we keep a static dictionary 'extensions' keyed by the tuple |
388 | (module name, module name) (for built-in modules) or by |
389 | (filename, module name) (for dynamically loaded modules), containing these |
390 | modules. A copy of the module's dictionary is stored by calling |
391 | _PyImport_FixupExtensionObject() immediately after the module initialization |
392 | function succeeds. A copy can be retrieved from there by calling |
393 | import_find_extension(). |
394 | |
395 | Modules which do support multiple initialization set their m_size |
396 | field to a non-negative number (indicating the size of the |
397 | module-specific state). They are still recorded in the extensions |
398 | dictionary, to avoid loading shared libraries twice. |
399 | */ |
400 | |
401 | int |
402 | _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, |
403 | PyObject *filename, PyObject *modules) |
404 | { |
405 | if (mod == NULL || !PyModule_Check(mod)) { Branch (405:9): [True: 0, False: 724]
Branch (405:24): [True: 0, False: 724]
|
406 | PyErr_BadInternalCall(); |
407 | return -1; |
408 | } |
409 | |
410 | struct PyModuleDef *def = PyModule_GetDef(mod); |
411 | if (!def) { Branch (411:9): [True: 0, False: 724]
|
412 | PyErr_BadInternalCall(); |
413 | return -1; |
414 | } |
415 | |
416 | PyThreadState *tstate = _PyThreadState_GET(); |
417 | if (PyObject_SetItem(modules, name, mod) < 0) { Branch (417:9): [True: 0, False: 724]
|
418 | return -1; |
419 | } |
420 | if (_PyState_AddModule(tstate, mod, def) < 0) { Branch (420:9): [True: 0, False: 724]
|
421 | PyMapping_DelItem(modules, name); |
422 | return -1; |
423 | } |
424 | |
425 | // bpo-44050: Extensions and def->m_base.m_copy can be updated |
426 | // when the extension module doesn't support sub-interpreters. |
427 | if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1342 ) { Branch (427:9): [True: 382, False: 342]
Branch (427:50): [True: 342, False: 0]
|
428 | if (def->m_size == -1) { Branch (428:13): [True: 609, False: 115]
|
429 | if (def->m_base.m_copy) { Branch (429:17): [True: 102, False: 507]
|
430 | /* Somebody already imported the module, |
431 | likely under a different name. |
432 | XXX this should really not happen. */ |
433 | Py_CLEAR(def->m_base.m_copy); |
434 | } |
435 | PyObject *dict = PyModule_GetDict(mod); |
436 | if (dict == NULL) { Branch (436:17): [True: 0, False: 609]
|
437 | return -1; |
438 | } |
439 | def->m_base.m_copy = PyDict_Copy(dict); |
440 | if (def->m_base.m_copy == NULL) { Branch (440:17): [True: 0, False: 609]
|
441 | return -1; |
442 | } |
443 | } |
444 | |
445 | if (extensions == NULL) { Branch (445:13): [True: 107, False: 617]
|
446 | extensions = PyDict_New(); |
447 | if (extensions == NULL) { Branch (447:17): [True: 0, False: 107]
|
448 | return -1; |
449 | } |
450 | } |
451 | |
452 | PyObject *key = PyTuple_Pack(2, filename, name); |
453 | if (key == NULL) { Branch (453:13): [True: 0, False: 724]
|
454 | return -1; |
455 | } |
456 | int res = PyDict_SetItem(extensions, key, (PyObject *)def); |
457 | Py_DECREF(key); |
458 | if (res < 0) { Branch (458:13): [True: 0, False: 724]
|
459 | return -1; |
460 | } |
461 | } |
462 | |
463 | return 0; |
464 | } |
465 | |
466 | int |
467 | _PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) |
468 | { |
469 | int res; |
470 | PyObject *nameobj; |
471 | nameobj = PyUnicode_InternFromString(name); |
472 | if (nameobj == NULL) Branch (472:9): [True: 0, False: 556]
|
473 | return -1; |
474 | res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj, modules); |
475 | Py_DECREF(nameobj); |
476 | return res; |
477 | } |
478 | |
479 | static PyObject * |
480 | import_find_extension(PyThreadState *tstate, PyObject *name, |
481 | PyObject *filename) |
482 | { |
483 | if (extensions == NULL) { Branch (483:9): [True: 0, False: 4.00k]
|
484 | return NULL; |
485 | } |
486 | |
487 | PyObject *key = PyTuple_Pack(2, filename, name); |
488 | if (key == NULL) { Branch (488:9): [True: 0, False: 4.00k]
|
489 | return NULL; |
490 | } |
491 | PyModuleDef* def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); |
492 | Py_DECREF(key); |
493 | if (def == NULL) { Branch (493:9): [True: 3.77k, False: 233]
|
494 | return NULL; |
495 | } |
496 | |
497 | PyObject *mod, *mdict; |
498 | PyObject *modules = tstate->interp->modules; |
499 | |
500 | if (def->m_size == -1) { Branch (500:9): [True: 53, False: 180]
|
501 | /* Module does not support repeated initialization */ |
502 | if (def->m_base.m_copy == NULL) Branch (502:13): [True: 0, False: 53]
|
503 | return NULL; |
504 | mod = import_add_module(tstate, name); |
505 | if (mod == NULL) Branch (505:13): [True: 0, False: 53]
|
506 | return NULL; |
507 | mdict = PyModule_GetDict(mod); |
508 | if (mdict == NULL) { Branch (508:13): [True: 0, False: 53]
|
509 | Py_DECREF(mod); |
510 | return NULL; |
511 | } |
512 | if (PyDict_Update(mdict, def->m_base.m_copy)) { Branch (512:13): [True: 0, False: 53]
|
513 | Py_DECREF(mod); |
514 | return NULL; |
515 | } |
516 | } |
517 | else { |
518 | if (def->m_base.m_init == NULL) Branch (518:13): [True: 0, False: 180]
|
519 | return NULL; |
520 | mod = _PyImport_InitFunc_TrampolineCall(def->m_base.m_init); |
521 | if (mod == NULL) Branch (521:13): [True: 0, False: 180]
|
522 | return NULL; |
523 | if (PyObject_SetItem(modules, name, mod) == -1) { Branch (523:13): [True: 0, False: 180]
|
524 | Py_DECREF(mod); |
525 | return NULL; |
526 | } |
527 | } |
528 | if (_PyState_AddModule(tstate, mod, def) < 0) { Branch (528:9): [True: 0, False: 233]
|
529 | PyMapping_DelItem(modules, name); |
530 | Py_DECREF(mod); |
531 | return NULL; |
532 | } |
533 | |
534 | int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; |
535 | if (verbose) { Branch (535:9): [True: 0, False: 233]
|
536 | PySys_FormatStderr("import %U # previously loaded (%R)\n", |
537 | name, filename); |
538 | } |
539 | return mod; |
540 | } |
541 | |
542 | |
543 | /* Get the module object corresponding to a module name. |
544 | First check the modules dictionary if there's one there, |
545 | if not, create a new one and insert it in the modules dictionary. */ |
546 | |
547 | static PyObject * |
548 | import_add_module(PyThreadState *tstate, PyObject *name) |
549 | { |
550 | PyObject *modules = tstate->interp->modules; |
551 | if (modules == NULL) { Branch (551:9): [True: 0, False: 1.03k]
|
552 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
553 | "no import module dictionary"); |
554 | return NULL; |
555 | } |
556 | |
557 | PyObject *m; |
558 | if (PyDict_CheckExact(modules)) { |
559 | m = PyDict_GetItemWithError(modules, name); |
560 | Py_XINCREF(m); |
561 | } |
562 | else { |
563 | m = PyObject_GetItem(modules, name); |
564 | // For backward-compatibility we copy the behavior |
565 | // of PyDict_GetItemWithError(). |
566 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { Branch (566:13): [True: 0, False: 0]
|
567 | _PyErr_Clear(tstate); |
568 | } |
569 | } |
570 | if (_PyErr_Occurred(tstate)) { Branch (570:9): [True: 0, False: 1.03k]
|
571 | return NULL; |
572 | } |
573 | if (m != NULL && PyModule_Check428 (m)) { Branch (573:9): [True: 428, False: 608]
|
574 | return m; |
575 | } |
576 | Py_XDECREF(m); |
577 | m = PyModule_NewObject(name); |
578 | if (m == NULL) Branch (578:9): [True: 0, False: 608]
|
579 | return NULL; |
580 | if (PyObject_SetItem(modules, name, m) != 0) { Branch (580:9): [True: 0, False: 608]
|
581 | Py_DECREF(m); |
582 | return NULL; |
583 | } |
584 | |
585 | return m; |
586 | } |
587 | |
588 | PyObject * |
589 | PyImport_AddModuleObject(PyObject *name) |
590 | { |
591 | PyThreadState *tstate = _PyThreadState_GET(); |
592 | PyObject *mod = import_add_module(tstate, name); |
593 | if (mod) { Branch (593:9): [True: 704, False: 0]
|
594 | PyObject *ref = PyWeakref_NewRef(mod, NULL); |
595 | Py_DECREF(mod); |
596 | if (ref == NULL) { Branch (596:13): [True: 0, False: 704]
|
597 | return NULL; |
598 | } |
599 | mod = PyWeakref_GetObject(ref); |
600 | Py_DECREF(ref); |
601 | } |
602 | return mod; /* borrowed reference */ |
603 | } |
604 | |
605 | |
606 | PyObject * |
607 | PyImport_AddModule(const char *name) |
608 | { |
609 | PyObject *nameobj = PyUnicode_FromString(name); |
610 | if (nameobj == NULL) { Branch (610:9): [True: 0, False: 704]
|
611 | return NULL; |
612 | } |
613 | PyObject *module = PyImport_AddModuleObject(nameobj); |
614 | Py_DECREF(nameobj); |
615 | return module; |
616 | } |
617 | |
618 | |
619 | /* Remove name from sys.modules, if it's there. |
620 | * Can be called with an exception raised. |
621 | * If fail to remove name a new exception will be chained with the old |
622 | * exception, otherwise the old exception is preserved. |
623 | */ |
624 | static void |
625 | remove_module(PyThreadState *tstate, PyObject *name) |
626 | { |
627 | PyObject *type, *value, *traceback; |
628 | _PyErr_Fetch(tstate, &type, &value, &traceback); |
629 |
|
630 | PyObject *modules = tstate->interp->modules; |
631 | if (PyDict_CheckExact(modules)) { |
632 | PyObject *mod = _PyDict_Pop(modules, name, Py_None); |
633 | Py_XDECREF(mod); |
634 | } |
635 | else if (PyMapping_DelItem(modules, name) < 0) { Branch (635:14): [True: 0, False: 0]
|
636 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { Branch (636:13): [True: 0, False: 0]
|
637 | _PyErr_Clear(tstate); |
638 | } |
639 | } |
640 |
|
641 | _PyErr_ChainExceptions(type, value, traceback); |
642 | } |
643 | |
644 | |
645 | /* Execute a code object in a module and return the module object |
646 | * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is |
647 | * removed from sys.modules, to avoid leaving damaged module objects |
648 | * in sys.modules. The caller may wish to restore the original |
649 | * module object (if any) in this case; PyImport_ReloadModule is an |
650 | * example. |
651 | * |
652 | * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer |
653 | * interface. The other two exist primarily for backward compatibility. |
654 | */ |
655 | PyObject * |
656 | PyImport_ExecCodeModule(const char *name, PyObject *co) |
657 | { |
658 | return PyImport_ExecCodeModuleWithPathnames( |
659 | name, co, (char *)NULL, (char *)NULL); |
660 | } |
661 | |
662 | PyObject * |
663 | PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname) |
664 | { |
665 | return PyImport_ExecCodeModuleWithPathnames( |
666 | name, co, pathname, (char *)NULL); |
667 | } |
668 | |
669 | PyObject * |
670 | PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, |
671 | const char *pathname, |
672 | const char *cpathname) |
673 | { |
674 | PyObject *m = NULL; |
675 | PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL; |
676 |
|
677 | nameobj = PyUnicode_FromString(name); |
678 | if (nameobj == NULL) Branch (678:9): [True: 0, False: 0]
|
679 | return NULL; |
680 | |
681 | if (cpathname != NULL) { Branch (681:9): [True: 0, False: 0]
|
682 | cpathobj = PyUnicode_DecodeFSDefault(cpathname); |
683 | if (cpathobj == NULL) Branch (683:13): [True: 0, False: 0]
|
684 | goto error; |
685 | } |
686 | else |
687 | cpathobj = NULL; |
688 | |
689 | if (pathname != NULL) { Branch (689:9): [True: 0, False: 0]
|
690 | pathobj = PyUnicode_DecodeFSDefault(pathname); |
691 | if (pathobj == NULL) Branch (691:13): [True: 0, False: 0]
|
692 | goto error; |
693 | } |
694 | else if (cpathobj != NULL) { Branch (694:14): [True: 0, False: 0]
|
695 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
696 |
|
697 | if (interp == NULL) { Branch (697:13): [True: 0, False: 0]
|
698 | Py_FatalError("no current interpreter"); |
699 | } |
700 | |
701 | external= PyObject_GetAttrString(interp->importlib, |
702 | "_bootstrap_external"); |
703 | if (external != NULL) { Branch (703:13): [True: 0, False: 0]
|
704 | pathobj = _PyObject_CallMethodOneArg( |
705 | external, &_Py_ID(_get_sourcefile), cpathobj); |
706 | Py_DECREF(external); |
707 | } |
708 | if (pathobj == NULL) Branch (708:13): [True: 0, False: 0]
|
709 | PyErr_Clear(); |
710 | } |
711 | else |
712 | pathobj = NULL; |
713 | |
714 | m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); |
715 | error: |
716 | Py_DECREF(nameobj); |
717 | Py_XDECREF(pathobj); |
718 | Py_XDECREF(cpathobj); |
719 | return m; |
720 | } |
721 | |
722 | static PyObject * |
723 | module_dict_for_exec(PyThreadState *tstate, PyObject *name) |
724 | { |
725 | PyObject *m, *d; |
726 | |
727 | m = import_add_module(tstate, name); |
728 | if (m == NULL) Branch (728:9): [True: 0, False: 279]
|
729 | return NULL; |
730 | /* If the module is being reloaded, we get the old module back |
731 | and re-use its dict to exec the new code. */ |
732 | d = PyModule_GetDict(m); |
733 | int r = PyDict_Contains(d, &_Py_ID(__builtins__)); |
734 | if (r == 0) { Branch (734:9): [True: 278, False: 1]
|
735 | r = PyDict_SetItem(d, &_Py_ID(__builtins__), PyEval_GetBuiltins()); |
736 | } |
737 | if (r < 0) { Branch (737:9): [True: 0, False: 279]
|
738 | remove_module(tstate, name); |
739 | Py_DECREF(m); |
740 | return NULL; |
741 | } |
742 | |
743 | Py_INCREF(d); |
744 | Py_DECREF(m); |
745 | return d; |
746 | } |
747 | |
748 | static PyObject * |
749 | exec_code_in_module(PyThreadState *tstate, PyObject *name, |
750 | PyObject *module_dict, PyObject *code_object) |
751 | { |
752 | PyObject *v, *m; |
753 | |
754 | v = PyEval_EvalCode(code_object, module_dict, module_dict); |
755 | if (v == NULL) { Branch (755:9): [True: 0, False: 279]
|
756 | remove_module(tstate, name); |
757 | return NULL; |
758 | } |
759 | Py_DECREF(v); |
760 | |
761 | m = import_get_module(tstate, name); |
762 | if (m == NULL && !_PyErr_Occurred(tstate)0 ) { Branch (762:9): [True: 0, False: 279]
Branch (762:22): [True: 0, False: 0]
|
763 | _PyErr_Format(tstate, PyExc_ImportError, |
764 | "Loaded module %R not found in sys.modules", |
765 | name); |
766 | } |
767 | |
768 | return m; |
769 | } |
770 | |
771 | PyObject* |
772 | PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, |
773 | PyObject *cpathname) |
774 | { |
775 | PyThreadState *tstate = _PyThreadState_GET(); |
776 | PyObject *d, *external, *res; |
777 |
|
778 | d = module_dict_for_exec(tstate, name); |
779 | if (d == NULL) { Branch (779:9): [True: 0, False: 0]
|
780 | return NULL; |
781 | } |
782 | |
783 | if (pathname == NULL) { Branch (783:9): [True: 0, False: 0]
|
784 | pathname = ((PyCodeObject *)co)->co_filename; |
785 | } |
786 | external = PyObject_GetAttrString(tstate->interp->importlib, |
787 | "_bootstrap_external"); |
788 | if (external == NULL) { Branch (788:9): [True: 0, False: 0]
|
789 | Py_DECREF(d); |
790 | return NULL; |
791 | } |
792 | res = PyObject_CallMethodObjArgs(external, &_Py_ID(_fix_up_module), |
793 | d, name, pathname, cpathname, NULL); |
794 | Py_DECREF(external); |
795 | if (res != NULL) { Branch (795:9): [True: 0, False: 0]
|
796 | Py_DECREF(res); |
797 | res = exec_code_in_module(tstate, name, d, co); |
798 | } |
799 | Py_DECREF(d); |
800 | return res; |
801 | } |
802 | |
803 | |
804 | static void |
805 | update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) |
806 | { |
807 | PyObject *constants, *tmp; |
808 | Py_ssize_t i, n; |
809 | |
810 | if (PyUnicode_Compare(co->co_filename, oldname)) Branch (810:9): [True: 0, False: 4]
|
811 | return; |
812 | |
813 | Py_INCREF(newname); |
814 | Py_XSETREF(co->co_filename, newname); |
815 | |
816 | constants = co->co_consts; |
817 | n = PyTuple_GET_SIZE(constants); |
818 | for (i = 0; i < n; i++9 ) { Branch (818:17): [True: 9, False: 4]
|
819 | tmp = PyTuple_GET_ITEM(constants, i); |
820 | if (PyCode_Check(tmp)) |
821 | update_code_filenames((PyCodeObject *)tmp, |
822 | oldname, newname); |
823 | } |
824 | } |
825 | |
826 | static void |
827 | update_compiled_module(PyCodeObject *co, PyObject *newname) |
828 | { |
829 | PyObject *oldname; |
830 | |
831 | if (PyUnicode_Compare(co->co_filename, newname) == 0) Branch (831:9): [True: 4.62k, False: 3]
|
832 | return; |
833 | |
834 | oldname = co->co_filename; |
835 | Py_INCREF(oldname); |
836 | update_code_filenames(co, oldname, newname); |
837 | Py_DECREF(oldname); |
838 | } |
839 | |
840 | /*[clinic input] |
841 | _imp._fix_co_filename |
842 | |
843 | code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") |
844 | Code object to change. |
845 | |
846 | path: unicode |
847 | File path to use. |
848 | / |
849 | |
850 | Changes code.co_filename to specify the passed-in file path. |
851 | [clinic start generated code]*/ |
852 | |
853 | static PyObject * |
854 | _imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code, |
855 | PyObject *path) |
856 | /*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/ |
857 | |
858 | { |
859 | update_compiled_module(code, path); |
860 | |
861 | Py_RETURN_NONE; |
862 | } |
863 | |
864 | |
865 | /* Helper to test for built-in module */ |
866 | |
867 | static int |
868 | is_builtin(PyObject *name) |
869 | { |
870 | int i; |
871 | for (i = 0; PyImport_Inittab[i].name != NULL; i++418k ) { Branch (871:17): [True: 421k, False: 12.0k]
|
872 | if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) { Branch (872:13): [True: 3.48k, False: 418k]
|
873 | if (PyImport_Inittab[i].initfunc == NULL) Branch (873:17): [True: 8, False: 3.48k]
|
874 | return -1; |
875 | else |
876 | return 1; |
877 | } |
878 | } |
879 | return 0; |
880 | } |
881 | |
882 | |
883 | /* Return a finder object for a sys.path/pkg.__path__ item 'p', |
884 | possibly by fetching it from the path_importer_cache dict. If it |
885 | wasn't yet cached, traverse path_hooks until a hook is found |
886 | that can handle the path item. Return None if no hook could; |
887 | this tells our caller that the path based finder could not find |
888 | a finder for this path item. Cache the result in |
889 | path_importer_cache. */ |
890 | |
891 | static PyObject * |
892 | get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, |
893 | PyObject *path_hooks, PyObject *p) |
894 | { |
895 | PyObject *importer; |
896 | Py_ssize_t j, nhooks; |
897 | |
898 | /* These conditions are the caller's responsibility: */ |
899 | assert(PyList_Check(path_hooks)); |
900 | assert(PyDict_Check(path_importer_cache)); |
901 | |
902 | nhooks = PyList_Size(path_hooks); |
903 | if (nhooks < 0) Branch (903:9): [True: 0, False: 1]
|
904 | return NULL; /* Shouldn't happen */ |
905 | |
906 | importer = PyDict_GetItemWithError(path_importer_cache, p); |
907 | if (importer != NULL || _PyErr_Occurred(tstate)) { Branch (907:9): [True: 0, False: 1]
Branch (907:29): [True: 0, False: 1]
|
908 | Py_XINCREF(importer); |
909 | return importer; |
910 | } |
911 | |
912 | /* set path_importer_cache[p] to None to avoid recursion */ |
913 | if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) Branch (913:9): [True: 0, False: 1]
|
914 | return NULL; |
915 | |
916 | for (j = 0; 1 j < nhooks; j++2 ) { Branch (916:17): [True: 2, False: 1]
|
917 | PyObject *hook = PyList_GetItem(path_hooks, j); |
918 | if (hook == NULL) Branch (918:13): [True: 0, False: 2]
|
919 | return NULL; |
920 | importer = PyObject_CallOneArg(hook, p); |
921 | if (importer != NULL) Branch (921:13): [True: 0, False: 2]
|
922 | break; |
923 | |
924 | if (!_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) { Branch (924:13): [True: 0, False: 2]
|
925 | return NULL; |
926 | } |
927 | _PyErr_Clear(tstate); |
928 | } |
929 | if (importer == NULL) { Branch (929:9): [True: 1, False: 0]
|
930 | Py_RETURN_NONE; |
931 | } |
932 | if (PyDict_SetItem(path_importer_cache, p, importer) < 0) { Branch (932:9): [True: 0, False: 0]
|
933 | Py_DECREF(importer); |
934 | return NULL; |
935 | } |
936 | return importer; |
937 | } |
938 | |
939 | PyObject * |
940 | PyImport_GetImporter(PyObject *path) |
941 | { |
942 | PyThreadState *tstate = _PyThreadState_GET(); |
943 | PyObject *path_importer_cache = PySys_GetObject("path_importer_cache"); |
944 | PyObject *path_hooks = PySys_GetObject("path_hooks"); |
945 | if (path_importer_cache == NULL || path_hooks == NULL) { Branch (945:9): [True: 0, False: 1]
Branch (945:40): [True: 0, False: 1]
|
946 | return NULL; |
947 | } |
948 | return get_path_importer(tstate, path_importer_cache, path_hooks, path); |
949 | } |
950 | |
951 | #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) |
952 | #include <emscripten.h> |
953 | EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { |
954 | return wasmTable.get(func)(); |
955 | }); |
956 | #endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE |
957 | |
958 | static PyObject* |
959 | create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) |
960 | { |
961 | PyObject *mod = import_find_extension(tstate, name, name); |
962 | if (mod || _PyErr_Occurred(tstate)3.47k ) { Branch (962:9): [True: 171, False: 3.47k]
Branch (962:16): [True: 0, False: 3.47k]
|
963 | return mod; |
964 | } |
965 | |
966 | PyObject *modules = tstate->interp->modules; |
967 | for (struct _inittab *p = PyImport_Inittab; p->name != NULL; p++48.2k ) { Branch (967:49): [True: 51.7k, False: 0]
|
968 | if (_PyUnicode_EqualToASCIIString(name, p->name)) { Branch (968:13): [True: 3.47k, False: 48.2k]
|
969 | if (p->initfunc == NULL) { Branch (969:17): [True: 0, False: 3.47k]
|
970 | /* Cannot re-init internal module ("sys" or "builtins") */ |
971 | return PyImport_AddModuleObject(name); |
972 | } |
973 | mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); |
974 | if (mod == NULL) { Branch (974:17): [True: 0, False: 3.47k]
|
975 | return NULL; |
976 | } |
977 | |
978 | if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { |
979 | return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); |
980 | } |
981 | else { |
982 | /* Remember pointer to module init function. */ |
983 | PyModuleDef *def = PyModule_GetDef(mod); |
984 | if (def == NULL) { Branch (984:21): [True: 0, False: 108]
|
985 | return NULL; |
986 | } |
987 | |
988 | def->m_base.m_init = p->initfunc; |
989 | if (_PyImport_FixupExtensionObject(mod, name, name, Branch (989:21): [True: 0, False: 108]
|
990 | modules) < 0) { |
991 | return NULL; |
992 | } |
993 | return mod; |
994 | } |
995 | } |
996 | } |
997 | |
998 | // not found |
999 | Py_RETURN_NONE0 ; |
1000 | } |
1001 | |
1002 | |
1003 | |
1004 | /*[clinic input] |
1005 | _imp.create_builtin |
1006 | |
1007 | spec: object |
1008 | / |
1009 | |
1010 | Create an extension module. |
1011 | [clinic start generated code]*/ |
1012 | |
1013 | static PyObject * |
1014 | _imp_create_builtin(PyObject *module, PyObject *spec) |
1015 | /*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/ |
1016 | { |
1017 | PyThreadState *tstate = _PyThreadState_GET(); |
1018 | |
1019 | PyObject *name = PyObject_GetAttrString(spec, "name"); |
1020 | if (name == NULL) { Branch (1020:9): [True: 0, False: 3.36k]
|
1021 | return NULL; |
1022 | } |
1023 | |
1024 | PyObject *mod = create_builtin(tstate, name, spec); |
1025 | Py_DECREF(name); |
1026 | return mod; |
1027 | } |
1028 | |
1029 | |
1030 | /* Return true if the name is an alias. In that case, "alias" is set |
1031 | to the original module name. If it is an alias but the original |
1032 | module isn't known then "alias" is set to NULL while true is returned. */ |
1033 | static bool |
1034 | resolve_module_alias(const char *name, const struct _module_alias *aliases, |
1035 | const char **alias) |
1036 | { |
1037 | const struct _module_alias *entry; |
1038 | for (entry = aliases; ; entry++58.4k ) { |
1039 | if (entry->name == NULL) { Branch (1039:13): [True: 6.42k, False: 59.8k]
|
1040 | /* It isn't an alias. */ |
1041 | return false; |
1042 | } |
1043 | if (strcmp(name, entry->name) == 0) { Branch (1043:13): [True: 1.42k, False: 58.4k]
|
1044 | if (alias != NULL) { Branch (1044:17): [True: 1.42k, False: 0]
|
1045 | *alias = entry->orig; |
1046 | } |
1047 | return true; |
1048 | } |
1049 | } |
1050 | } |
1051 | |
1052 | |
1053 | /* Frozen modules */ |
1054 | |
1055 | static bool |
1056 | use_frozen(void) |
1057 | { |
1058 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
1059 | int override = interp->override_frozen_modules; |
1060 | if (override > 0) { Branch (1060:9): [True: 246, False: 62.2k]
|
1061 | return true; |
1062 | } |
1063 | else if (override < 0) { Branch (1063:14): [True: 45.1k, False: 17.1k]
|
1064 | return false; |
1065 | } |
1066 | else { |
1067 | return interp->config.use_frozen_modules; |
1068 | } |
1069 | } |
1070 | |
1071 | static PyObject * |
1072 | list_frozen_module_names(void) |
1073 | { |
1074 | PyObject *names = PyList_New(0); |
1075 | if (names == NULL) { Branch (1075:9): [True: 0, False: 1]
|
1076 | return NULL; |
1077 | } |
1078 | bool enabled = use_frozen(); |
1079 | const struct _frozen *p; |
1080 | #define ADD_MODULE(name) \ |
1081 | do { \ |
1082 | PyObject *nameobj = PyUnicode_FromString(name); \ |
1083 | if (nameobj == NULL) { \ |
1084 | goto error; \ |
1085 | } \ |
1086 | int res = PyList_Append(names, nameobj); \ |
1087 | Py_DECREF(nameobj); \ |
1088 | if (res != 0) { \ |
1089 | goto error; \ |
1090 | } \ |
1091 | } while(0) |
1092 | // We always use the bootstrap modules. |
1093 | for (p = _PyImport_FrozenBootstrap; ; p++3 ) { |
1094 | if (p->name == NULL) { Branch (1094:13): [True: 1, False: 3]
|
1095 | break; |
1096 | } |
1097 | ADD_MODULE(p->name); |
1098 | } |
1099 | // Frozen stdlib modules may be disabled. |
1100 | for (p = _PyImport_FrozenStdlib; ; 1 p++15 ) { |
1101 | if (p->name == NULL) { Branch (1101:13): [True: 1, False: 15]
|
1102 | break; |
1103 | } |
1104 | if (enabled) { Branch (1104:13): [True: 15, False: 0]
|
1105 | ADD_MODULE(p->name); |
1106 | } |
1107 | } |
1108 | for (p = _PyImport_FrozenTest; ; 1 p++11 ) { |
1109 | if (p->name == NULL) { Branch (1109:13): [True: 1, False: 11]
|
1110 | break; |
1111 | } |
1112 | if (enabled) { Branch (1112:13): [True: 11, False: 0]
|
1113 | ADD_MODULE(p->name); |
1114 | } |
1115 | } |
1116 | #undef ADD_MODULE |
1117 | // Add any custom modules. |
1118 | if (PyImport_FrozenModules != NULL) { Branch (1118:9): [True: 0, False: 1]
|
1119 | for (p = PyImport_FrozenModules; ; p++) { |
1120 | if (p->name == NULL) { Branch (1120:17): [True: 0, False: 0]
|
1121 | break; |
1122 | } |
1123 | PyObject *nameobj = PyUnicode_FromString(p->name); |
1124 | if (nameobj == NULL) { Branch (1124:17): [True: 0, False: 0]
|
1125 | goto error; |
1126 | } |
1127 | int found = PySequence_Contains(names, nameobj); |
1128 | if (found < 0) { Branch (1128:17): [True: 0, False: 0]
|
1129 | Py_DECREF(nameobj); |
1130 | goto error; |
1131 | } |
1132 | else if (found) { Branch (1132:22): [True: 0, False: 0]
|
1133 | Py_DECREF(nameobj); |
1134 | } |
1135 | else { |
1136 | int res = PyList_Append(names, nameobj); |
1137 | Py_DECREF(nameobj); |
1138 | if (res != 0) { Branch (1138:21): [True: 0, False: 0]
|
1139 | goto error; |
1140 | } |
1141 | } |
1142 | } |
1143 | } |
1144 | return names; |
1145 | |
1146 | error: |
1147 | Py_DECREF(names); |
1148 | return NULL; |
1149 | } |
1150 | |
1151 | typedef enum { |
1152 | FROZEN_OKAY, |
1153 | FROZEN_BAD_NAME, // The given module name wasn't valid. |
1154 | FROZEN_NOT_FOUND, // It wasn't in PyImport_FrozenModules. |
1155 | FROZEN_DISABLED, // -X frozen_modules=off (and not essential) |
1156 | FROZEN_EXCLUDED, /* The PyImport_FrozenModules entry has NULL "code" |
1157 | (module is present but marked as unimportable, stops search). */ |
1158 | FROZEN_INVALID, /* The PyImport_FrozenModules entry is bogus |
1159 | (eg. does not contain executable code). */ |
1160 | } frozen_status; |
1161 | |
1162 | static inline void |
1163 | set_frozen_error(frozen_status status, PyObject *modname) |
1164 | { |
1165 | const char *err = NULL; |
1166 | switch (status) { |
1167 | case FROZEN_BAD_NAME: Branch (1167:9): [True: 0, False: 2]
|
1168 | case FROZEN_NOT_FOUND: Branch (1168:9): [True: 2, False: 0]
|
1169 | err = "No such frozen object named %R"; |
1170 | break; |
1171 | case FROZEN_DISABLED: Branch (1171:9): [True: 0, False: 2]
|
1172 | err = "Frozen modules are disabled and the frozen object named %R is not essential"; |
1173 | break; |
1174 | case FROZEN_EXCLUDED: Branch (1174:9): [True: 0, False: 2]
|
1175 | err = "Excluded frozen object named %R"; |
1176 | break; |
1177 | case FROZEN_INVALID: Branch (1177:9): [True: 0, False: 2]
|
1178 | err = "Frozen object named %R is invalid"; |
1179 | break; |
1180 | case FROZEN_OKAY: Branch (1180:9): [True: 0, False: 2]
|
1181 | // There was no error. |
1182 | break; |
1183 | default: Branch (1183:9): [True: 0, False: 2]
|
1184 | Py_UNREACHABLE(); |
1185 | } |
1186 | if (err != NULL) { Branch (1186:9): [True: 2, False: 0]
|
1187 | PyObject *msg = PyUnicode_FromFormat(err, modname); |
1188 | if (msg == NULL) { Branch (1188:13): [True: 0, False: 2]
|
1189 | PyErr_Clear(); |
1190 | } |
1191 | PyErr_SetImportError(msg, modname, NULL); |
1192 | Py_XDECREF(msg); |
1193 | } |
1194 | } |
1195 | |
1196 | static const struct _frozen * |
1197 | look_up_frozen(const char *name) |
1198 | { |
1199 | const struct _frozen *p; |
1200 | // We always use the bootstrap modules. |
1201 | for (p = _PyImport_FrozenBootstrap; ; p++189k ) { |
1202 | if (p->name == NULL) { Branch (1202:13): [True: 62.4k, False: 191k]
|
1203 | // We hit the end-of-list sentinel value. |
1204 | break; |
1205 | } |
1206 | if (strcmp(name, p->name) == 0) { Branch (1206:13): [True: 1.99k, False: 189k]
|
1207 | return p; |
1208 | } |
1209 | } |
1210 | // Prefer custom modules, if any. Frozen stdlib modules can be |
1211 | // disabled here by setting "code" to NULL in the array entry. |
1212 | if (PyImport_FrozenModules != NULL) { Branch (1212:9): [True: 27, False: 62.4k]
|
1213 | for (p = PyImport_FrozenModules; ; p++26 ) { |
1214 | if (p->name == NULL) { Branch (1214:17): [True: 26, False: 27]
|
1215 | break; |
1216 | } |
1217 | if (strcmp(name, p->name) == 0) { Branch (1217:17): [True: 1, False: 26]
|
1218 | return p; |
1219 | } |
1220 | } |
1221 | } |
1222 | // Frozen stdlib modules may be disabled. |
1223 | if (use_frozen()) { Branch (1223:9): [True: 17.3k, False: 45.1k]
|
1224 | for (p = _PyImport_FrozenStdlib; ; p++205k ) { |
1225 | if (p->name == NULL) { Branch (1225:17): [True: 11.7k, False: 210k]
|
1226 | break; |
1227 | } |
1228 | if (strcmp(name, p->name) == 0) { Branch (1228:17): [True: 5.61k, False: 205k]
|
1229 | return p; |
1230 | } |
1231 | } |
1232 | for (p = _PyImport_FrozenTest; ; 11.7k p++127k ) { |
1233 | if (p->name == NULL) { Branch (1233:17): [True: 11.4k, False: 127k]
|
1234 | break; |
1235 | } |
1236 | if (strcmp(name, p->name) == 0) { Branch (1236:17): [True: 233, False: 127k]
|
1237 | return p; |
1238 | } |
1239 | } |
1240 | } |
1241 | return NULL; |
1242 | } |
1243 | |
1244 | struct frozen_info { |
1245 | PyObject *nameobj; |
1246 | const char *data; |
1247 | PyObject *(*get_code)(void); |
1248 | Py_ssize_t size; |
1249 | bool is_package; |
1250 | bool is_alias; |
1251 | const char *origname; |
1252 | }; |
1253 | |
1254 | static frozen_status |
1255 | find_frozen(PyObject *nameobj, struct frozen_info *info) |
1256 | { |
1257 | if (info != NULL) { Branch (1257:9): [True: 64.4k, False: 0]
|
1258 | memset(info, 0, sizeof(*info)); |
1259 | } |
1260 | |
1261 | if (nameobj == NULL || nameobj == Py_None) { Branch (1261:9): [True: 0, False: 64.4k]
Branch (1261:28): [True: 0, False: 64.4k]
|
1262 | return FROZEN_BAD_NAME; |
1263 | } |
1264 | const char *name = PyUnicode_AsUTF8(nameobj); |
1265 | if (name == NULL) { Branch (1265:9): [True: 0, False: 64.4k]
|
1266 | // Note that this function previously used |
1267 | // _PyUnicode_EqualToASCIIString(). We clear the error here |
1268 | // (instead of propagating it) to match the earlier behavior |
1269 | // more closely. |
1270 | PyErr_Clear(); |
1271 | return FROZEN_BAD_NAME; |
1272 | } |
1273 | |
1274 | const struct _frozen *p = look_up_frozen(name); |
1275 | if (p == NULL) { Branch (1275:9): [True: 56.6k, False: 7.84k]
|
1276 | return FROZEN_NOT_FOUND; |
1277 | } |
1278 | if (info != NULL) { Branch (1278:9): [True: 7.84k, False: 0]
|
1279 | info->nameobj = nameobj; // borrowed |
1280 | info->data = (const char *)p->code; |
1281 | info->get_code = p->get_code; |
1282 | info->size = p->size; |
1283 | info->is_package = p->is_package; |
1284 | if (p->size < 0) { Branch (1284:13): [True: 0, False: 7.84k]
|
1285 | // backward compatibility with negative size values |
1286 | info->size = -(p->size); |
1287 | info->is_package = true; |
1288 | } |
1289 | info->origname = name; |
1290 | info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases, |
1291 | &info->origname); |
1292 | } |
1293 | if (p->code == NULL && p->size == 07.84k && p->get_code != NULL7.84k ) { Branch (1293:9): [True: 7.84k, False: 1]
Branch (1293:28): [True: 7.84k, False: 0]
Branch (1293:44): [True: 7.84k, False: 0]
|
1294 | /* It is only deepfrozen. */ |
1295 | return FROZEN_OKAY; |
1296 | } |
1297 | if (p->code == NULL) { Branch (1297:9): [True: 0, False: 1]
|
1298 | /* It is frozen but marked as un-importable. */ |
1299 | return FROZEN_EXCLUDED; |
1300 | } |
1301 | if (p->code[0] == '\0' || p->size == 0) { Branch (1301:9): [True: 0, False: 1]
Branch (1301:31): [True: 0, False: 1]
|
1302 | /* Does not contain executable code. */ |
1303 | return FROZEN_INVALID; |
1304 | } |
1305 | return FROZEN_OKAY; |
1306 | } |
1307 | |
1308 | static PyObject * |
1309 | unmarshal_frozen_code(struct frozen_info *info) |
1310 | { |
1311 | if (info->get_code) { Branch (1311:9): [True: 3.64k, False: 1]
|
1312 | PyObject *code = info->get_code(); |
1313 | assert(code != NULL); |
1314 | return code; |
1315 | } |
1316 | PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size); |
1317 | if (co == NULL) { Branch (1317:9): [True: 0, False: 1]
|
1318 | /* Does not contain executable code. */ |
1319 | set_frozen_error(FROZEN_INVALID, info->nameobj); |
1320 | return NULL; |
1321 | } |
1322 | if (!PyCode_Check(co)) { Branch (1322:9): [True: 0, False: 1]
|
1323 | // We stick with TypeError for backward compatibility. |
1324 | PyErr_Format(PyExc_TypeError, |
1325 | "frozen object %R is not a code object", |
1326 | info->nameobj); |
1327 | Py_DECREF(co); |
1328 | return NULL; |
1329 | } |
1330 | return co; |
1331 | } |
1332 | |
1333 | |
1334 | /* Initialize a frozen module. |
1335 | Return 1 for success, 0 if the module is not found, and -1 with |
1336 | an exception set if the initialization failed. |
1337 | This function is also used from frozenmain.c */ |
1338 | |
1339 | int |
1340 | PyImport_ImportFrozenModuleObject(PyObject *name) |
1341 | { |
1342 | PyThreadState *tstate = _PyThreadState_GET(); |
1343 | PyObject *co, *m, *d = NULL; |
1344 | int err; |
1345 | |
1346 | struct frozen_info info; |
1347 | frozen_status status = find_frozen(name, &info); |
1348 | if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) { Branch (1348:9): [True: 0, False: 279]
Branch (1348:39): [True: 0, False: 279]
|
1349 | return 0; |
1350 | } |
1351 | else if (status == FROZEN_BAD_NAME) { Branch (1351:14): [True: 0, False: 279]
|
1352 | return 0; |
1353 | } |
1354 | else if (status != FROZEN_OKAY) { Branch (1354:14): [True: 0, False: 279]
|
1355 | set_frozen_error(status, name); |
1356 | return -1; |
1357 | } |
1358 | co = unmarshal_frozen_code(&info); |
1359 | if (co == NULL) { Branch (1359:9): [True: 0, False: 279]
|
1360 | return -1; |
1361 | } |
1362 | if (info.is_package) { Branch (1362:9): [True: 0, False: 279]
|
1363 | /* Set __path__ to the empty list */ |
1364 | PyObject *l; |
1365 | m = import_add_module(tstate, name); |
1366 | if (m == NULL) Branch (1366:13): [True: 0, False: 0]
|
1367 | goto err_return; |
1368 | d = PyModule_GetDict(m); |
1369 | l = PyList_New(0); |
1370 | if (l == NULL) { Branch (1370:13): [True: 0, False: 0]
|
1371 | Py_DECREF(m); |
1372 | goto err_return; |
1373 | } |
1374 | err = PyDict_SetItemString(d, "__path__", l); |
1375 | Py_DECREF(l); |
1376 | Py_DECREF(m); |
1377 | if (err != 0) Branch (1377:13): [True: 0, False: 0]
|
1378 | goto err_return; |
1379 | } |
1380 | d = module_dict_for_exec(tstate, name); |
1381 | if (d == NULL) { Branch (1381:9): [True: 0, False: 279]
|
1382 | goto err_return; |
1383 | } |
1384 | m = exec_code_in_module(tstate, name, d, co); |
1385 | if (m == NULL) { Branch (1385:9): [True: 0, False: 279]
|
1386 | goto err_return; |
1387 | } |
1388 | Py_DECREF(m); |
1389 | /* Set __origname__ (consumed in FrozenImporter._setup_module()). */ |
1390 | PyObject *origname; |
1391 | if (info.origname) { Branch (1391:9): [True: 279, False: 0]
|
1392 | origname = PyUnicode_FromString(info.origname); |
1393 | if (origname == NULL) { Branch (1393:13): [True: 0, False: 279]
|
1394 | goto err_return; |
1395 | } |
1396 | } |
1397 | else { |
1398 | Py_INCREF(Py_None); |
1399 | origname = Py_None; |
1400 | } |
1401 | err = PyDict_SetItemString(d, "__origname__", origname); |
1402 | Py_DECREF(origname); |
1403 | if (err != 0) { Branch (1403:9): [True: 0, False: 279]
|
1404 | goto err_return; |
1405 | } |
1406 | Py_DECREF(d); |
1407 | Py_DECREF(co); |
1408 | return 1; |
1409 | |
1410 | err_return: |
1411 | Py_XDECREF(d); |
1412 | Py_DECREF(co); |
1413 | return -1; |
1414 | } |
1415 | |
1416 | int |
1417 | PyImport_ImportFrozenModule(const char *name) |
1418 | { |
1419 | PyObject *nameobj; |
1420 | int ret; |
1421 | nameobj = PyUnicode_InternFromString(name); |
1422 | if (nameobj == NULL) Branch (1422:9): [True: 0, False: 279]
|
1423 | return -1; |
1424 | ret = PyImport_ImportFrozenModuleObject(nameobj); |
1425 | Py_DECREF(nameobj); |
1426 | return ret; |
1427 | } |
1428 | |
1429 | |
1430 | /* Import a module, either built-in, frozen, or external, and return |
1431 | its module object WITH INCREMENTED REFERENCE COUNT */ |
1432 | |
1433 | PyObject * |
1434 | PyImport_ImportModule(const char *name) |
1435 | { |
1436 | PyObject *pname; |
1437 | PyObject *result; |
1438 | |
1439 | pname = PyUnicode_FromString(name); |
1440 | if (pname == NULL) Branch (1440:9): [True: 0, False: 1.97k]
|
1441 | return NULL; |
1442 | result = PyImport_Import(pname); |
1443 | Py_DECREF(pname); |
1444 | return result; |
1445 | } |
1446 | |
1447 | |
1448 | /* Import a module without blocking |
1449 | * |
1450 | * At first it tries to fetch the module from sys.modules. If the module was |
1451 | * never loaded before it loads it with PyImport_ImportModule() unless another |
1452 | * thread holds the import lock. In the latter case the function raises an |
1453 | * ImportError instead of blocking. |
1454 | * |
1455 | * Returns the module object with incremented ref count. |
1456 | */ |
1457 | PyObject * |
1458 | PyImport_ImportModuleNoBlock(const char *name) |
1459 | { |
1460 | return PyImport_ImportModule(name); |
1461 | } |
1462 | |
1463 | |
1464 | /* Remove importlib frames from the traceback, |
1465 | * except in Verbose mode. */ |
1466 | static void |
1467 | remove_importlib_frames(PyThreadState *tstate) |
1468 | { |
1469 | const char *importlib_filename = "<frozen importlib._bootstrap>"; |
1470 | const char *external_filename = "<frozen importlib._bootstrap_external>"; |
1471 | const char *remove_frames = "_call_with_frames_removed"; |
1472 | int always_trim = 0; |
1473 | int in_importlib = 0; |
1474 | PyObject *exception, *value, *base_tb, *tb; |
1475 | PyObject **prev_link, **outer_link = NULL; |
1476 | |
1477 | /* Synopsis: if it's an ImportError, we trim all importlib chunks |
1478 | from the traceback. We always trim chunks |
1479 | which end with a call to "_call_with_frames_removed". */ |
1480 | |
1481 | _PyErr_Fetch(tstate, &exception, &value, &base_tb); |
1482 | if (!exception || _PyInterpreterState_GetConfig(tstate->interp)->verbose) { Branch (1482:9): [True: 0, False: 1.09k]
Branch (1482:23): [True: 2, False: 1.09k]
|
1483 | goto done; |
1484 | } |
1485 | |
1486 | if (PyType_IsSubtype((PyTypeObject *) exception, Branch (1486:9): [True: 994, False: 97]
|
1487 | (PyTypeObject *) PyExc_ImportError)) |
1488 | always_trim = 1; |
1489 | |
1490 | prev_link = &base_tb; |
1491 | tb = base_tb; |
1492 | while (tb != NULL) { Branch (1492:12): [True: 2.38k, False: 1.09k]
|
1493 | PyTracebackObject *traceback = (PyTracebackObject *)tb; |
1494 | PyObject *next = (PyObject *) traceback->tb_next; |
1495 | PyFrameObject *frame = traceback->tb_frame; |
1496 | PyCodeObject *code = PyFrame_GetCode(frame); |
1497 | int now_in_importlib; |
1498 | |
1499 | assert(PyTraceBack_Check(tb)); |
1500 | now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) || Branch (1500:28): [True: 2.18k, False: 199]
|
1501 | _PyUnicode_EqualToASCIIString(code->co_filename, external_filename)199 ; Branch (1501:28): [True: 96, False: 103]
|
1502 | if (now_in_importlib && !in_importlib2.28k ) { Branch (1502:13): [True: 2.28k, False: 103]
Branch (1502:33): [True: 1.06k, False: 1.22k]
|
1503 | /* This is the link to this chunk of importlib tracebacks */ |
1504 | outer_link = prev_link; |
1505 | } |
1506 | in_importlib = now_in_importlib; |
1507 | |
1508 | if (in_importlib && Branch (1508:13): [True: 2.28k, False: 103]
|
1509 | (2.28k always_trim2.28k || Branch (1509:14): [True: 1.93k, False: 354]
|
1510 | _PyUnicode_EqualToASCIIString(code->co_name, remove_frames)354 )) { Branch (1510:14): [True: 69, False: 285]
|
1511 | Py_XINCREF(next); |
1512 | Py_XSETREF(*outer_link, next); |
1513 | prev_link = outer_link; |
1514 | } |
1515 | else { |
1516 | prev_link = (PyObject **) &traceback->tb_next; |
1517 | } |
1518 | Py_DECREF(code); |
1519 | tb = next; |
1520 | } |
1521 | done: |
1522 | _PyErr_Restore(tstate, exception, value, base_tb); |
1523 | } |
1524 | |
1525 | |
1526 | static PyObject * |
1527 | resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level) |
1528 | { |
1529 | PyObject *abs_name; |
1530 | PyObject *package = NULL; |
1531 | PyObject *spec; |
1532 | Py_ssize_t last_dot; |
1533 | PyObject *base; |
1534 | int level_up; |
1535 | |
1536 | if (globals == NULL) { Branch (1536:9): [True: 1, False: 17.1k]
|
1537 | _PyErr_SetString(tstate, PyExc_KeyError, "'__name__' not in globals"); |
1538 | goto error; |
1539 | } |
1540 | if (!PyDict_Check(globals)) { Branch (1540:9): [True: 0, False: 17.1k]
|
1541 | _PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict"); |
1542 | goto error; |
1543 | } |
1544 | package = PyDict_GetItemWithError(globals, &_Py_ID(__package__)); |
1545 | if (package == Py_None) { Branch (1545:9): [True: 3, False: 17.0k]
|
1546 | package = NULL; |
1547 | } |
1548 | else if (package == NULL && _PyErr_Occurred(tstate)14 ) { Branch (1548:14): [True: 14, False: 17.0k]
Branch (1548:33): [True: 0, False: 14]
|
1549 | goto error; |
1550 | } |
1551 | spec = PyDict_GetItemWithError(globals, &_Py_ID(__spec__)); |
1552 | if (spec == NULL && _PyErr_Occurred(tstate)35 ) { Branch (1552:9): [True: 35, False: 17.0k]
Branch (1552:25): [True: 0, False: 35]
|
1553 | goto error; |
1554 | } |
1555 | |
1556 | if (package != NULL) { Branch (1556:9): [True: 17.0k, False: 17]
|
1557 | Py_INCREF(package); |
1558 | if (!PyUnicode_Check(package)) { Branch (1558:13): [True: 3, False: 17.0k]
|
1559 | _PyErr_SetString(tstate, PyExc_TypeError, |
1560 | "package must be a string"); |
1561 | goto error; |
1562 | } |
1563 | else if (spec != NULL && spec != 17.0k Py_None17.0k ) { Branch (1563:18): [True: 17.0k, False: 18]
Branch (1563:34): [True: 17.0k, False: 2]
|
1564 | int equal; |
1565 | PyObject *parent = PyObject_GetAttr(spec, &_Py_ID(parent)); |
1566 | if (parent == NULL) { Branch (1566:17): [True: 0, False: 17.0k]
|
1567 | goto error; |
1568 | } |
1569 | |
1570 | equal = PyObject_RichCompareBool(package, parent, Py_EQ); |
1571 | Py_DECREF(parent); |
1572 | if (equal < 0) { Branch (1572:17): [True: 0, False: 17.0k]
|
1573 | goto error; |
1574 | } |
1575 | else if (equal == 0) { Branch (1575:22): [True: 2, False: 17.0k]
|
1576 | if (PyErr_WarnEx(PyExc_ImportWarning, Branch (1576:21): [True: 0, False: 2]
|
1577 | "__package__ != __spec__.parent", 1) < 0) { |
1578 | goto error; |
1579 | } |
1580 | } |
1581 | } |
1582 | } |
1583 | else if (spec != NULL && spec != 3 Py_None3 ) { Branch (1583:14): [True: 3, False: 14]
Branch (1583:30): [True: 2, False: 1]
|
1584 | package = PyObject_GetAttr(spec, &_Py_ID(parent)); |
1585 | if (package == NULL) { Branch (1585:13): [True: 0, False: 2]
|
1586 | goto error; |
1587 | } |
1588 | else if (!PyUnicode_Check(package)) { Branch (1588:18): [True: 0, False: 2]
|
1589 | _PyErr_SetString(tstate, PyExc_TypeError, |
1590 | "__spec__.parent must be a string"); |
1591 | goto error; |
1592 | } |
1593 | } |
1594 | else { |
1595 | if (PyErr_WarnEx(PyExc_ImportWarning, Branch (1595:13): [True: 0, False: 15]
|
1596 | "can't resolve package from __spec__ or __package__, " |
1597 | "falling back on __name__ and __path__", 1) < 0) { |
1598 | goto error; |
1599 | } |
1600 | |
1601 | package = PyDict_GetItemWithError(globals, &_Py_ID(__name__)); |
1602 | if (package == NULL) { Branch (1602:13): [True: 0, False: 15]
|
1603 | if (!_PyErr_Occurred(tstate)) { Branch (1603:17): [True: 0, False: 0]
|
1604 | _PyErr_SetString(tstate, PyExc_KeyError, |
1605 | "'__name__' not in globals"); |
1606 | } |
1607 | goto error; |
1608 | } |
1609 | |
1610 | Py_INCREF(package); |
1611 | if (!PyUnicode_Check(package)) { Branch (1611:13): [True: 0, False: 15]
|
1612 | _PyErr_SetString(tstate, PyExc_TypeError, |
1613 | "__name__ must be a string"); |
1614 | goto error; |
1615 | } |
1616 | |
1617 | int haspath = PyDict_Contains(globals, &_Py_ID(__path__)); |
1618 | if (haspath < 0) { Branch (1618:13): [True: 0, False: 15]
|
1619 | goto error; |
1620 | } |
1621 | if (!haspath) { Branch (1621:13): [True: 5, False: 10]
|
1622 | Py_ssize_t dot; |
1623 | |
1624 | if (PyUnicode_READY(package) < 0) { Branch (1624:17): [True: 0, False: 5]
|
1625 | goto error; |
1626 | } |
1627 | |
1628 | dot = PyUnicode_FindChar(package, '.', |
1629 | 0, PyUnicode_GET_LENGTH(package), -1); |
1630 | if (dot == -2) { Branch (1630:17): [True: 0, False: 5]
|
1631 | goto error; |
1632 | } |
1633 | else if (dot == -1) { Branch (1633:22): [True: 1, False: 4]
|
1634 | goto no_parent_error; |
1635 | } |
1636 | PyObject *substr = PyUnicode_Substring(package, 0, dot); |
1637 | if (substr == NULL) { Branch (1637:17): [True: 0, False: 4]
|
1638 | goto error; |
1639 | } |
1640 | Py_SETREF(package, substr); |
1641 | } |
1642 | } |
1643 | |
1644 | last_dot = PyUnicode_GET_LENGTH(package); |
1645 | if (last_dot == 0) { Branch (1645:9): [True: 3, False: 17.0k]
|
1646 | goto no_parent_error; |
1647 | } |
1648 | |
1649 | for (level_up = 1; 17.0k level_up < level; level_up += 1204 ) { Branch (1649:24): [True: 208, False: 17.0k]
|
1650 | last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1); |
1651 | if (last_dot == -2) { Branch (1651:13): [True: 0, False: 208]
|
1652 | goto error; |
1653 | } |
1654 | else if (last_dot == -1) { Branch (1654:18): [True: 4, False: 204]
|
1655 | _PyErr_SetString(tstate, PyExc_ImportError, |
1656 | "attempted relative import beyond top-level " |
1657 | "package"); |
1658 | goto error; |
1659 | } |
1660 | } |
1661 | |
1662 | base = PyUnicode_Substring(package, 0, last_dot); |
1663 | Py_DECREF(package); |
1664 | if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) { Branch (1664:9): [True: 0, False: 17.0k]
Branch (1664:25): [True: 1.62k, False: 15.4k]
|
1665 | return base; |
1666 | } |
1667 | |
1668 | abs_name = PyUnicode_FromFormat("%U.%U", base, name); |
1669 | Py_DECREF(base); |
1670 | return abs_name; |
1671 | |
1672 | no_parent_error: |
1673 | _PyErr_SetString(tstate, PyExc_ImportError, |
1674 | "attempted relative import " |
1675 | "with no known parent package"); |
1676 | |
1677 | error: |
1678 | Py_XDECREF(package); |
1679 | return NULL; |
1680 | } |
1681 | |
1682 | static PyObject * |
1683 | import_find_and_load(PyThreadState *tstate, PyObject *abs_name) |
1684 | { |
1685 | PyObject *mod = NULL; |
1686 | PyInterpreterState *interp = tstate->interp; |
1687 | int import_time = _PyInterpreterState_GetConfig(interp)->import_time; |
1688 | static int import_level; |
1689 | static _PyTime_t accumulated; |
1690 | |
1691 | _PyTime_t t1 = 0, accumulated_copy = accumulated; |
1692 | |
1693 | PyObject *sys_path = PySys_GetObject("path"); |
1694 | PyObject *sys_meta_path = PySys_GetObject("meta_path"); |
1695 | PyObject *sys_path_hooks = PySys_GetObject("path_hooks"); |
1696 | if (_PySys_Audit(tstate, "import", "OOOOO", Branch (1696:9): [True: 0, False: 11.8k]
|
1697 | abs_name, Py_None, sys_path ? sys_path : Py_None0 , Branch (1697:41): [True: 11.8k, False: 0]
|
1698 | sys_meta_path ? sys_meta_path : Py_None0 , Branch (1698:22): [True: 11.8k, False: 0]
|
1699 | sys_path_hooks ? sys_path_hooks : Py_None0 ) < 0) { Branch (1699:22): [True: 11.8k, False: 0]
|
1700 | return NULL; |
1701 | } |
1702 | |
1703 | |
1704 | /* XOptions is initialized after first some imports. |
1705 | * So we can't have negative cache before completed initialization. |
1706 | * Anyway, importlib._find_and_load is much slower than |
1707 | * _PyDict_GetItemIdWithError(). |
1708 | */ |
1709 | if (import_time) { Branch (1709:9): [True: 144, False: 11.6k]
|
1710 | static int header = 1; |
1711 | if (header) { Branch (1711:13): [True: 3, False: 141]
|
1712 | fputs("import time: self [us] | cumulative | imported package\n", |
1713 | stderr); |
1714 | header = 0; |
1715 | } |
1716 | |
1717 | import_level++; |
1718 | t1 = _PyTime_GetPerfCounter(); |
1719 | accumulated = 0; |
1720 | } |
1721 | |
1722 | if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()) Branch (1722:9): [True: 0, False: 11.8k]
|
1723 | PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name)); |
1724 | |
1725 | mod = PyObject_CallMethodObjArgs(interp->importlib, &_Py_ID(_find_and_load), |
1726 | abs_name, interp->import_func, NULL); |
1727 | |
1728 | if (PyDTrace_IMPORT_FIND_LOAD_DONE_ENABLED()) Branch (1728:9): [True: 0, False: 11.8k]
|
1729 | PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name), |
1730 | mod != NULL); |
1731 | |
1732 | if (import_time) { Branch (1732:9): [True: 144, False: 11.6k]
|
1733 | _PyTime_t cum = _PyTime_GetPerfCounter() - t1; |
1734 | |
1735 | import_level--; |
1736 | fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n", |
1737 | (long)_PyTime_AsMicroseconds(cum - accumulated, _PyTime_ROUND_CEILING), |
1738 | (long)_PyTime_AsMicroseconds(cum, _PyTime_ROUND_CEILING), |
1739 | import_level*2, "", PyUnicode_AsUTF8(abs_name)); |
1740 | |
1741 | accumulated = accumulated_copy + cum; |
1742 | } |
1743 | |
1744 | return mod; |
1745 | } |
1746 | |
1747 | PyObject * |
1748 | PyImport_GetModule(PyObject *name) |
1749 | { |
1750 | PyThreadState *tstate = _PyThreadState_GET(); |
1751 | PyObject *mod; |
1752 | |
1753 | mod = import_get_module(tstate, name); |
1754 | if (mod != NULL && mod != 95.1k Py_None95.1k ) { Branch (1754:9): [True: 95.1k, False: 591]
Branch (1754:24): [True: 95.1k, False: 0]
|
1755 | if (import_ensure_initialized(tstate->interp, mod, name) < 0) { Branch (1755:13): [True: 0, False: 95.1k]
|
1756 | Py_DECREF(mod); |
1757 | remove_importlib_frames(tstate); |
1758 | return NULL; |
1759 | } |
1760 | } |
1761 | return mod; |
1762 | } |
1763 | |
1764 | PyObject * |
1765 | PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, |
1766 | PyObject *locals, PyObject *fromlist, |
1767 | int level) |
1768 | { |
1769 | PyThreadState *tstate = _PyThreadState_GET(); |
1770 | PyObject *abs_name = NULL; |
1771 | PyObject *final_mod = NULL; |
1772 | PyObject *mod = NULL; |
1773 | PyObject *package = NULL; |
1774 | PyInterpreterState *interp = tstate->interp; |
1775 | int has_from; |
1776 | |
1777 | if (name == NULL) { Branch (1777:9): [True: 0, False: 659k]
|
1778 | _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name"); |
1779 | goto error; |
1780 | } |
1781 | |
1782 | /* The below code is importlib.__import__() & _gcd_import(), ported to C |
1783 | for added performance. */ |
1784 | |
1785 | if (!PyUnicode_Check(name)) { Branch (1785:9): [True: 3, False: 659k]
|
1786 | _PyErr_SetString(tstate, PyExc_TypeError, |
1787 | "module name must be a string"); |
1788 | goto error; |
1789 | } |
1790 | if (PyUnicode_READY(name) < 0) { Branch (1790:9): [True: 0, False: 659k]
|
1791 | goto error; |
1792 | } |
1793 | if (level < 0) { Branch (1793:9): [True: 2, False: 659k]
|
1794 | _PyErr_SetString(tstate, PyExc_ValueError, "level must be >= 0"); |
1795 | goto error; |
1796 | } |
1797 | |
1798 | if (level > 0) { Branch (1798:9): [True: 17.1k, False: 642k]
|
1799 | abs_name = resolve_name(tstate, name, globals, level); |
1800 | if (abs_name == NULL) Branch (1800:13): [True: 12, False: 17.0k]
|
1801 | goto error; |
1802 | } |
1803 | else { /* level == 0 */ |
1804 | if (PyUnicode_GET_LENGTH(name) == 0) { Branch (1804:13): [True: 12, False: 642k]
|
1805 | _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name"); |
1806 | goto error; |
1807 | } |
1808 | abs_name = name; |
1809 | Py_INCREF(abs_name); |
1810 | } |
1811 | |
1812 | mod = import_get_module(tstate, abs_name); |
1813 | if (mod == NULL && _PyErr_Occurred(tstate)11.6k ) { Branch (1813:9): [True: 11.6k, False: 647k]
Branch (1813:24): [True: 0, False: 11.6k]
|
1814 | goto error; |
1815 | } |
1816 | |
1817 | if (mod != NULL && mod != 647k Py_None647k ) { Branch (1817:9): [True: 647k, False: 11.6k]
Branch (1817:24): [True: 647k, False: 124]
|
1818 | if (import_ensure_initialized(tstate->interp, mod, abs_name) < 0) { Branch (1818:13): [True: 0, False: 647k]
|
1819 | goto error; |
1820 | } |
1821 | } |
1822 | else { |
1823 | Py_XDECREF(mod); |
1824 | mod = import_find_and_load(tstate, abs_name); |
1825 | if (mod == NULL) { Branch (1825:13): [True: 1.04k, False: 10.7k]
|
1826 | goto error; |
1827 | } |
1828 | } |
1829 | |
1830 | has_from = 0; |
1831 | if (fromlist != NULL && fromlist != 603k Py_None603k ) { Branch (1831:9): [True: 603k, False: 55.4k]
Branch (1831:29): [True: 424k, False: 178k]
|
1832 | has_from = PyObject_IsTrue(fromlist); |
1833 | if (has_from < 0) Branch (1833:13): [True: 0, False: 424k]
|
1834 | goto error; |
1835 | } |
1836 | if (!has_from) { Branch (1836:9): [True: 577k, False: 81.2k]
|
1837 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
1838 | if (level == 0 || len > 01 ) { Branch (1838:13): [True: 577k, False: 1]
Branch (1838:27): [True: 1, False: 0]
|
1839 | Py_ssize_t dot; |
1840 | |
1841 | dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
1842 | if (dot == -2) { Branch (1842:17): [True: 0, False: 577k]
|
1843 | goto error; |
1844 | } |
1845 | |
1846 | if (dot == -1) { Branch (1846:17): [True: 539k, False: 37.2k]
|
1847 | /* No dot in module name, simple exit */ |
1848 | final_mod = mod; |
1849 | Py_INCREF(mod); |
1850 | goto error; |
1851 | } |
1852 | |
1853 | if (level == 0) { Branch (1853:17): [True: 37.2k, False: 0]
|
1854 | PyObject *front = PyUnicode_Substring(name, 0, dot); |
1855 | if (front == NULL) { Branch (1855:21): [True: 0, False: 37.2k]
|
1856 | goto error; |
1857 | } |
1858 | |
1859 | final_mod = PyImport_ImportModuleLevelObject(front, NULL, NULL, NULL, 0); |
1860 | Py_DECREF(front); |
1861 | } |
1862 | else { |
1863 | Py_ssize_t cut_off = len - dot; |
1864 | Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name); |
1865 | PyObject *to_return = PyUnicode_Substring(abs_name, 0, |
1866 | abs_name_len - cut_off); |
1867 | if (to_return == NULL) { Branch (1867:21): [True: 0, False: 0]
|
1868 | goto error; |
1869 | } |
1870 | |
1871 | final_mod = import_get_module(tstate, to_return); |
1872 | Py_DECREF(to_return); |
1873 | if (final_mod == NULL) { Branch (1873:21): [True: 0, False: 0]
|
1874 | if (!_PyErr_Occurred(tstate)) { Branch (1874:25): [True: 0, False: 0]
|
1875 | _PyErr_Format(tstate, PyExc_KeyError, |
1876 | "%R not in sys.modules as expected", |
1877 | to_return); |
1878 | } |
1879 | goto error; |
1880 | } |
1881 | } |
1882 | } |
1883 | else { |
1884 | final_mod = mod; |
1885 | Py_INCREF(mod); |
1886 | } |
1887 | } |
1888 | else { |
1889 | PyObject *path; |
1890 | if (_PyObject_LookupAttr(mod, &_Py_ID(__path__), &path) < 0) { Branch (1890:13): [True: 0, False: 81.2k]
|
1891 | goto error; |
1892 | } |
1893 | if (path) { Branch (1893:13): [True: 5.58k, False: 75.6k]
|
1894 | Py_DECREF(path); |
1895 | final_mod = PyObject_CallMethodObjArgs( |
1896 | interp->importlib, &_Py_ID(_handle_fromlist), |
1897 | mod, fromlist, interp->import_func, NULL); |
1898 | } |
1899 | else { |
1900 | final_mod = mod; |
1901 | Py_INCREF(mod); |
1902 | } |
1903 | } |
1904 | |
1905 | error: |
1906 | Py_XDECREF(abs_name); |
1907 | Py_XDECREF(mod); |
1908 | Py_XDECREF(package); |
1909 | if (final_mod == NULL) { Branch (1909:9): [True: 1.09k, False: 658k]
|
1910 | remove_importlib_frames(tstate); |
1911 | } |
1912 | return final_mod; |
1913 | } |
1914 | |
1915 | PyObject * |
1916 | PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, |
1917 | PyObject *fromlist, int level) |
1918 | { |
1919 | PyObject *nameobj, *mod; |
1920 | nameobj = PyUnicode_FromString(name); |
1921 | if (nameobj == NULL) Branch (1921:9): [True: 0, False: 2.07k]
|
1922 | return NULL; |
1923 | mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals, |
1924 | fromlist, level); |
1925 | Py_DECREF(nameobj); |
1926 | return mod; |
1927 | } |
1928 | |
1929 | |
1930 | /* Re-import a module of any kind and return its module object, WITH |
1931 | INCREMENTED REFERENCE COUNT */ |
1932 | |
1933 | PyObject * |
1934 | PyImport_ReloadModule(PyObject *m) |
1935 | { |
1936 | PyObject *reloaded_module = NULL; |
1937 | PyObject *importlib = PyImport_GetModule(&_Py_ID(importlib)); |
1938 | if (importlib == NULL) { Branch (1938:9): [True: 0, False: 0]
|
1939 | if (PyErr_Occurred()) { Branch (1939:13): [True: 0, False: 0]
|
1940 | return NULL; |
1941 | } |
1942 | |
1943 | importlib = PyImport_ImportModule("importlib"); |
1944 | if (importlib == NULL) { Branch (1944:13): [True: 0, False: 0]
|
1945 | return NULL; |
1946 | } |
1947 | } |
1948 | |
1949 | reloaded_module = PyObject_CallMethodOneArg(importlib, &_Py_ID(reload), m); |
1950 | Py_DECREF(importlib); |
1951 | return reloaded_module; |
1952 | } |
1953 | |
1954 | |
1955 | /* Higher-level import emulator which emulates the "import" statement |
1956 | more accurately -- it invokes the __import__() function from the |
1957 | builtins of the current globals. This means that the import is |
1958 | done using whatever import hooks are installed in the current |
1959 | environment. |
1960 | A dummy list ["__doc__"] is passed as the 4th argument so that |
1961 | e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) |
1962 | will return <module "gencache"> instead of <module "win32com">. */ |
1963 | |
1964 | PyObject * |
1965 | PyImport_Import(PyObject *module_name) |
1966 | { |
1967 | PyThreadState *tstate = _PyThreadState_GET(); |
1968 | PyObject *globals = NULL; |
1969 | PyObject *import = NULL; |
1970 | PyObject *builtins = NULL; |
1971 | PyObject *r = NULL; |
1972 | |
1973 | PyObject *from_list = PyList_New(0); |
1974 | if (from_list == NULL) { Branch (1974:9): [True: 0, False: 343k]
|
1975 | goto err; |
1976 | } |
1977 | |
1978 | /* Get the builtins from current globals */ |
1979 | globals = PyEval_GetGlobals(); |
1980 | if (globals != NULL) { Branch (1980:9): [True: 340k, False: 2.07k]
|
1981 | Py_INCREF(globals); |
1982 | builtins = PyObject_GetItem(globals, &_Py_ID(__builtins__)); |
1983 | if (builtins == NULL) Branch (1983:13): [True: 0, False: 340k]
|
1984 | goto err; |
1985 | } |
1986 | else { |
1987 | /* No globals -- use standard builtins, and fake globals */ |
1988 | builtins = PyImport_ImportModuleLevel("builtins", |
1989 | NULL, NULL, NULL, 0); |
1990 | if (builtins == NULL) { Branch (1990:13): [True: 0, False: 2.07k]
|
1991 | goto err; |
1992 | } |
1993 | globals = Py_BuildValue("{OO}", &_Py_ID(__builtins__), builtins); |
1994 | if (globals == NULL) Branch (1994:13): [True: 0, False: 2.07k]
|
1995 | goto err; |
1996 | } |
1997 | |
1998 | /* Get the __import__ function from the builtins */ |
1999 | if (PyDict_Check(builtins)) { |
2000 | import = PyObject_GetItem(builtins, &_Py_ID(__import__)); |
2001 | if (import == NULL) { Branch (2001:13): [True: 0, False: 340k]
|
2002 | _PyErr_SetObject(tstate, PyExc_KeyError, &_Py_ID(__import__)); |
2003 | } |
2004 | } |
2005 | else |
2006 | import = PyObject_GetAttr(builtins, &_Py_ID(__import__)); |
2007 | if (import == NULL) Branch (2007:9): [True: 0, False: 343k]
|
2008 | goto err; |
2009 | |
2010 | /* Call the __import__ function with the proper argument list |
2011 | Always use absolute import here. |
2012 | Calling for side-effect of import. */ |
2013 | r = PyObject_CallFunction(import, "OOOOi", module_name, globals, |
2014 | globals, from_list, 0, NULL); |
2015 | if (r == NULL) Branch (2015:9): [True: 13, False: 343k]
|
2016 | goto err; |
2017 | Py_DECREF(r); |
2018 | |
2019 | r = import_get_module(tstate, module_name); |
2020 | if (r == NULL && !_PyErr_Occurred(tstate)0 ) { Branch (2020:9): [True: 0, False: 343k]
Branch (2020:22): [True: 0, False: 0]
|
2021 | _PyErr_SetObject(tstate, PyExc_KeyError, module_name); |
2022 | } |
2023 | |
2024 | err: |
2025 | Py_XDECREF(globals); |
2026 | Py_XDECREF(builtins); |
2027 | Py_XDECREF(import); |
2028 | Py_XDECREF(from_list); |
2029 | |
2030 | return r; |
2031 | } |
2032 | |
2033 | /*[clinic input] |
2034 | _imp.extension_suffixes |
2035 | |
2036 | Returns the list of file suffixes used to identify extension modules. |
2037 | [clinic start generated code]*/ |
2038 | |
2039 | static PyObject * |
2040 | _imp_extension_suffixes_impl(PyObject *module) |
2041 | /*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/ |
2042 | { |
2043 | PyObject *list; |
2044 | |
2045 | list = PyList_New(0); |
2046 | if (list == NULL) Branch (2046:9): [True: 0, False: 677]
|
2047 | return NULL; |
2048 | #ifdef HAVE_DYNAMIC_LOADING |
2049 | const char *suffix; |
2050 | unsigned int index = 0; |
2051 | |
2052 | while ((suffix = _PyImport_DynLoadFiletab[index])) { Branch (2052:12): [True: 2.03k, False: 677]
|
2053 | PyObject *item = PyUnicode_FromString(suffix); |
2054 | if (item == NULL) { Branch (2054:13): [True: 0, False: 2.03k]
|
2055 | Py_DECREF(list); |
2056 | return NULL; |
2057 | } |
2058 | if (PyList_Append(list, item) < 0) { Branch (2058:13): [True: 0, False: 2.03k]
|
2059 | Py_DECREF(list); |
2060 | Py_DECREF(item); |
2061 | return NULL; |
2062 | } |
2063 | Py_DECREF(item); |
2064 | index += 1; |
2065 | } |
2066 | #endif |
2067 | return list; |
2068 | } |
2069 | |
2070 | /*[clinic input] |
2071 | _imp.init_frozen |
2072 | |
2073 | name: unicode |
2074 | / |
2075 | |
2076 | Initializes a frozen module. |
2077 | [clinic start generated code]*/ |
2078 | |
2079 | static PyObject * |
2080 | _imp_init_frozen_impl(PyObject *module, PyObject *name) |
2081 | /*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/ |
2082 | { |
2083 | PyThreadState *tstate = _PyThreadState_GET(); |
2084 | int ret; |
2085 |
|
2086 | ret = PyImport_ImportFrozenModuleObject(name); |
2087 | if (ret < 0) Branch (2087:9): [True: 0, False: 0]
|
2088 | return NULL; |
2089 | if (ret == 0) { Branch (2089:9): [True: 0, False: 0]
|
2090 | Py_RETURN_NONE; |
2091 | } |
2092 | return import_add_module(tstate, name); |
2093 | } |
2094 | |
2095 | /*[clinic input] |
2096 | _imp.find_frozen |
2097 | |
2098 | name: unicode |
2099 | / |
2100 | * |
2101 | withdata: bool = False |
2102 | |
2103 | Return info about the corresponding frozen module (if there is one) or None. |
2104 | |
2105 | The returned info (a 2-tuple): |
2106 | |
2107 | * data the raw marshalled bytes |
2108 | * is_package whether or not it is a package |
2109 | * origname the originally frozen module's name, or None if not |
2110 | a stdlib module (this will usually be the same as |
2111 | the module's current name) |
2112 | [clinic start generated code]*/ |
2113 | |
2114 | static PyObject * |
2115 | _imp_find_frozen_impl(PyObject *module, PyObject *name, int withdata) |
2116 | /*[clinic end generated code: output=8c1c3c7f925397a5 input=22a8847c201542fd]*/ |
2117 | { |
2118 | struct frozen_info info; |
2119 | frozen_status status = find_frozen(name, &info); |
2120 | if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED3.52k ) { Branch (2120:9): [True: 11.8k, False: 3.52k]
Branch (2120:39): [True: 0, False: 3.52k]
|
2121 | Py_RETURN_NONE; |
2122 | } |
2123 | else if (status == FROZEN_BAD_NAME) { Branch (2123:14): [True: 0, False: 3.52k]
|
2124 | Py_RETURN_NONE; |
2125 | } |
2126 | else if (status != FROZEN_OKAY) { Branch (2126:14): [True: 0, False: 3.52k]
|
2127 | set_frozen_error(status, name); |
2128 | return NULL; |
2129 | } |
2130 | |
2131 | PyObject *data = NULL; |
2132 | if (withdata) { Branch (2132:9): [True: 0, False: 3.52k]
|
2133 | data = PyMemoryView_FromMemory((char *)info.data, info.size, PyBUF_READ); |
2134 | if (data == NULL) { Branch (2134:13): [True: 0, False: 0]
|
2135 | return NULL; |
2136 | } |
2137 | } |
2138 | |
2139 | PyObject *origname = NULL; |
2140 | if (info.origname != NULL && info.origname[0] != '\0'3.52k ) { Branch (2140:9): [True: 3.52k, False: 3]
Branch (2140:34): [True: 3.52k, False: 0]
|
2141 | origname = PyUnicode_FromString(info.origname); |
2142 | if (origname == NULL) { Branch (2142:13): [True: 0, False: 3.52k]
|
2143 | Py_DECREF(data); |
2144 | return NULL; |
2145 | } |
2146 | } |
2147 | |
2148 | PyObject *result = PyTuple_Pack(3, data ? data0 : Py_None, Branch (2148:40): [True: 0, False: 3.52k]
|
2149 | info.is_package ? Py_True : Py_False, Branch (2149:37): [True: 41, False: 3.48k]
|
2150 | origname ? origname3.52k : Py_None3 ); Branch (2150:37): [True: 3.52k, False: 3]
|
2151 | Py_XDECREF(origname); |
2152 | Py_XDECREF(data); |
2153 | return result; |
2154 | } |
2155 | |
2156 | /*[clinic input] |
2157 | _imp.get_frozen_object |
2158 | |
2159 | name: unicode |
2160 | data as dataobj: object = None |
2161 | / |
2162 | |
2163 | Create a code object for a frozen module. |
2164 | [clinic start generated code]*/ |
2165 | |
2166 | static PyObject * |
2167 | _imp_get_frozen_object_impl(PyObject *module, PyObject *name, |
2168 | PyObject *dataobj) |
2169 | /*[clinic end generated code: output=54368a673a35e745 input=034bdb88f6460b7b]*/ |
2170 | { |
2171 | struct frozen_info info = {0}; |
2172 | Py_buffer buf = {0}; |
2173 | if (PyObject_CheckBuffer(dataobj)) { Branch (2173:9): [True: 0, False: 3.36k]
|
2174 | if (PyObject_GetBuffer(dataobj, &buf, PyBUF_READ) != 0) { Branch (2174:13): [True: 0, False: 0]
|
2175 | return NULL; |
2176 | } |
2177 | info.data = (const char *)buf.buf; |
2178 | info.size = buf.len; |
2179 | } |
2180 | else if (dataobj != Py_None) { Branch (2180:14): [True: 0, False: 3.36k]
|
2181 | _PyArg_BadArgument("get_frozen_object", "argument 2", "bytes", dataobj); |
2182 | return NULL; |
2183 | } |
2184 | else { |
2185 | frozen_status status = find_frozen(name, &info); |
2186 | if (status != FROZEN_OKAY) { Branch (2186:13): [True: 2, False: 3.36k]
|
2187 | set_frozen_error(status, name); |
2188 | return NULL; |
2189 | } |
2190 | } |
2191 | |
2192 | if (info.nameobj == NULL) { Branch (2192:9): [True: 0, False: 3.36k]
|
2193 | info.nameobj = name; |
2194 | } |
2195 | if (info.size == 0 && info.get_code == NULL) { Branch (2195:9): [True: 3.36k, False: 0]
Branch (2195:27): [True: 0, False: 3.36k]
|
2196 | /* Does not contain executable code. */ |
2197 | set_frozen_error(FROZEN_INVALID, name); |
2198 | return NULL; |
2199 | } |
2200 | |
2201 | PyObject *codeobj = unmarshal_frozen_code(&info); |
2202 | if (dataobj != Py_None) { Branch (2202:9): [True: 0, False: 3.36k]
|
2203 | PyBuffer_Release(&buf); |
2204 | } |
2205 | return codeobj; |
2206 | } |
2207 | |
2208 | /*[clinic input] |
2209 | _imp.is_frozen_package |
2210 | |
2211 | name: unicode |
2212 | / |
2213 | |
2214 | Returns True if the module name is of a frozen package. |
2215 | [clinic start generated code]*/ |
2216 | |
2217 | static PyObject * |
2218 | _imp_is_frozen_package_impl(PyObject *module, PyObject *name) |
2219 | /*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/ |
2220 | { |
2221 | struct frozen_info info; |
2222 | frozen_status status = find_frozen(name, &info); |
2223 | if (status != FROZEN_OKAY && status != FROZEN_EXCLUDED0 ) { Branch (2223:9): [True: 0, False: 318]
Branch (2223:34): [True: 0, False: 0]
|
2224 | set_frozen_error(status, name); |
2225 | return NULL; |
2226 | } |
2227 | return PyBool_FromLong(info.is_package); |
2228 | } |
2229 | |
2230 | /*[clinic input] |
2231 | _imp.is_builtin |
2232 | |
2233 | name: unicode |
2234 | / |
2235 | |
2236 | Returns True if the module name corresponds to a built-in module. |
2237 | [clinic start generated code]*/ |
2238 | |
2239 | static PyObject * |
2240 | _imp_is_builtin_impl(PyObject *module, PyObject *name) |
2241 | /*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/ |
2242 | { |
2243 | return PyLong_FromLong(is_builtin(name)); |
2244 | } |
2245 | |
2246 | /*[clinic input] |
2247 | _imp.is_frozen |
2248 | |
2249 | name: unicode |
2250 | / |
2251 | |
2252 | Returns True if the module name corresponds to a frozen module. |
2253 | [clinic start generated code]*/ |
2254 | |
2255 | static PyObject * |
2256 | _imp_is_frozen_impl(PyObject *module, PyObject *name) |
2257 | /*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/ |
2258 | { |
2259 | struct frozen_info info; |
2260 | frozen_status status = find_frozen(name, &info); |
2261 | if (status != FROZEN_OKAY) { Branch (2261:9): [True: 44.8k, False: 363]
|
2262 | Py_RETURN_FALSE; |
2263 | } |
2264 | Py_RETURN_TRUE363 ; |
2265 | } |
2266 | |
2267 | /*[clinic input] |
2268 | _imp._frozen_module_names |
2269 | |
2270 | Returns the list of available frozen modules. |
2271 | [clinic start generated code]*/ |
2272 | |
2273 | static PyObject * |
2274 | _imp__frozen_module_names_impl(PyObject *module) |
2275 | /*[clinic end generated code: output=80609ef6256310a8 input=76237fbfa94460d2]*/ |
2276 | { |
2277 | return list_frozen_module_names(); |
2278 | } |
2279 | |
2280 | /*[clinic input] |
2281 | _imp._override_frozen_modules_for_tests |
2282 | |
2283 | override: int |
2284 | / |
2285 | |
2286 | (internal-only) Override PyConfig.use_frozen_modules. |
2287 | |
2288 | (-1: "off", 1: "on", 0: no override) |
2289 | See frozen_modules() in Lib/test/support/import_helper.py. |
2290 | [clinic start generated code]*/ |
2291 | |
2292 | static PyObject * |
2293 | _imp__override_frozen_modules_for_tests_impl(PyObject *module, int override) |
2294 | /*[clinic end generated code: output=36d5cb1594160811 input=8f1f95a3ef21aec3]*/ |
2295 | { |
2296 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
2297 | interp->override_frozen_modules = override; |
2298 | Py_RETURN_NONE; |
2299 | } |
2300 | |
2301 | /* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ |
2302 | static int |
2303 | exec_builtin_or_dynamic(PyObject *mod) { |
2304 | PyModuleDef *def; |
2305 | void *state; |
2306 | |
2307 | if (!PyModule_Check(mod)) { Branch (2307:9): [True: 4, False: 3.98k]
|
2308 | return 0; |
2309 | } |
2310 | |
2311 | def = PyModule_GetDef(mod); |
2312 | if (def == NULL) { Branch (2312:9): [True: 54, False: 3.93k]
|
2313 | return 0; |
2314 | } |
2315 | |
2316 | state = PyModule_GetState(mod); |
2317 | if (state) { Branch (2317:9): [True: 303, False: 3.62k]
|
2318 | /* Already initialized; skip reload */ |
2319 | return 0; |
2320 | } |
2321 | |
2322 | return PyModule_ExecDef(mod, def); |
2323 | } |
2324 | |
2325 | #ifdef HAVE_DYNAMIC_LOADING |
2326 | |
2327 | /*[clinic input] |
2328 | _imp.create_dynamic |
2329 | |
2330 | spec: object |
2331 | file: object = NULL |
2332 | / |
2333 | |
2334 | Create an extension module. |
2335 | [clinic start generated code]*/ |
2336 | |
2337 | static PyObject * |
2338 | _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file) |
2339 | /*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/ |
2340 | { |
2341 | PyObject *mod, *name, *path; |
2342 | FILE *fp; |
2343 | |
2344 | name = PyObject_GetAttrString(spec, "name"); |
2345 | if (name == NULL) { Branch (2345:9): [True: 0, False: 362]
|
2346 | return NULL; |
2347 | } |
2348 | |
2349 | path = PyObject_GetAttrString(spec, "origin"); |
2350 | if (path == NULL) { Branch (2350:9): [True: 0, False: 362]
|
2351 | Py_DECREF(name); |
2352 | return NULL; |
2353 | } |
2354 | |
2355 | PyThreadState *tstate = _PyThreadState_GET(); |
2356 | mod = import_find_extension(tstate, name, path); |
2357 | if (mod != NULL || PyErr_Occurred()300 ) { Branch (2357:9): [True: 62, False: 300]
Branch (2357:24): [True: 0, False: 300]
|
2358 | Py_DECREF(name); |
2359 | Py_DECREF(path); |
2360 | return mod; |
2361 | } |
2362 | |
2363 | if (file != NULL) { Branch (2363:9): [True: 0, False: 300]
|
2364 | fp = _Py_fopen_obj(path, "r"); |
2365 | if (fp == NULL) { Branch (2365:13): [True: 0, False: 0]
|
2366 | Py_DECREF(name); |
2367 | Py_DECREF(path); |
2368 | return NULL; |
2369 | } |
2370 | } |
2371 | else |
2372 | fp = NULL; |
2373 | |
2374 | mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp); |
2375 | |
2376 | Py_DECREF(name); |
2377 | Py_DECREF(path); |
2378 | if (fp) Branch (2378:9): [True: 0, False: 300]
|
2379 | fclose(fp); |
2380 | return mod; |
2381 | } |
2382 | |
2383 | /*[clinic input] |
2384 | _imp.exec_dynamic -> int |
2385 | |
2386 | mod: object |
2387 | / |
2388 | |
2389 | Initialize an extension module. |
2390 | [clinic start generated code]*/ |
2391 | |
2392 | static int |
2393 | _imp_exec_dynamic_impl(PyObject *module, PyObject *mod) |
2394 | /*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/ |
2395 | { |
2396 | return exec_builtin_or_dynamic(mod); |
2397 | } |
2398 | |
2399 | |
2400 | #endif /* HAVE_DYNAMIC_LOADING */ |
2401 | |
2402 | /*[clinic input] |
2403 | _imp.exec_builtin -> int |
2404 | |
2405 | mod: object |
2406 | / |
2407 | |
2408 | Initialize a built-in module. |
2409 | [clinic start generated code]*/ |
2410 | |
2411 | static int |
2412 | _imp_exec_builtin_impl(PyObject *module, PyObject *mod) |
2413 | /*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/ |
2414 | { |
2415 | return exec_builtin_or_dynamic(mod); |
2416 | } |
2417 | |
2418 | /*[clinic input] |
2419 | _imp.source_hash |
2420 | |
2421 | key: long |
2422 | source: Py_buffer |
2423 | [clinic start generated code]*/ |
2424 | |
2425 | static PyObject * |
2426 | _imp_source_hash_impl(PyObject *module, long key, Py_buffer *source) |
2427 | /*[clinic end generated code: output=edb292448cf399ea input=9aaad1e590089789]*/ |
2428 | { |
2429 | union { |
2430 | uint64_t x; |
2431 | char data[sizeof(uint64_t)]; |
2432 | } hash; |
2433 | hash.x = _Py_KeyedHash((uint64_t)key, source->buf, source->len); |
2434 | #if !PY_LITTLE_ENDIAN |
2435 | // Force to little-endian. There really ought to be a succinct standard way |
2436 | // to do this. |
2437 | for (size_t i = 0; i < sizeof(hash.data)/2; i++) { |
2438 | char tmp = hash.data[i]; |
2439 | hash.data[i] = hash.data[sizeof(hash.data) - i - 1]; |
2440 | hash.data[sizeof(hash.data) - i - 1] = tmp; |
2441 | } |
2442 | #endif |
2443 | return PyBytes_FromStringAndSize(hash.data, sizeof(hash.data)); |
2444 | } |
2445 | |
2446 | |
2447 | PyDoc_STRVAR(doc_imp, |
2448 | "(Extremely) low-level import machinery bits as used by importlib and imp."); |
2449 | |
2450 | static PyMethodDef imp_methods[] = { |
2451 | _IMP_EXTENSION_SUFFIXES_METHODDEF |
2452 | _IMP_LOCK_HELD_METHODDEF |
2453 | _IMP_ACQUIRE_LOCK_METHODDEF |
2454 | _IMP_RELEASE_LOCK_METHODDEF |
2455 | _IMP_FIND_FROZEN_METHODDEF |
2456 | _IMP_GET_FROZEN_OBJECT_METHODDEF |
2457 | _IMP_IS_FROZEN_PACKAGE_METHODDEF |
2458 | _IMP_CREATE_BUILTIN_METHODDEF |
2459 | _IMP_INIT_FROZEN_METHODDEF |
2460 | _IMP_IS_BUILTIN_METHODDEF |
2461 | _IMP_IS_FROZEN_METHODDEF |
2462 | _IMP__FROZEN_MODULE_NAMES_METHODDEF |
2463 | _IMP__OVERRIDE_FROZEN_MODULES_FOR_TESTS_METHODDEF |
2464 | _IMP_CREATE_DYNAMIC_METHODDEF |
2465 | _IMP_EXEC_DYNAMIC_METHODDEF |
2466 | _IMP_EXEC_BUILTIN_METHODDEF |
2467 | _IMP__FIX_CO_FILENAME_METHODDEF |
2468 | _IMP_SOURCE_HASH_METHODDEF |
2469 | {NULL, NULL} /* sentinel */ |
2470 | }; |
2471 | |
2472 | |
2473 | static int |
2474 | imp_module_exec(PyObject *module) |
2475 | { |
2476 | const wchar_t *mode = _Py_GetConfig()->check_hash_pycs_mode; |
2477 | PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1); |
2478 | if (pyc_mode == NULL) { Branch (2478:9): [True: 0, False: 278]
|
2479 | return -1; |
2480 | } |
2481 | if (PyModule_AddObjectRef(module, "check_hash_based_pycs", pyc_mode) < 0) { Branch (2481:9): [True: 0, False: 278]
|
2482 | Py_DECREF(pyc_mode); |
2483 | return -1; |
2484 | } |
2485 | Py_DECREF(pyc_mode); |
2486 | |
2487 | return 0; |
2488 | } |
2489 | |
2490 | |
2491 | static PyModuleDef_Slot imp_slots[] = { |
2492 | {Py_mod_exec, imp_module_exec}, |
2493 | {0, NULL} |
2494 | }; |
2495 | |
2496 | static struct PyModuleDef imp_module = { |
2497 | PyModuleDef_HEAD_INIT, |
2498 | .m_name = "_imp", |
2499 | .m_doc = doc_imp, |
2500 | .m_size = 0, |
2501 | .m_methods = imp_methods, |
2502 | .m_slots = imp_slots, |
2503 | }; |
2504 | |
2505 | PyMODINIT_FUNC |
2506 | PyInit__imp(void) |
2507 | { |
2508 | return PyModuleDef_Init(&imp_module); |
2509 | } |
2510 | |
2511 | |
2512 | // Import the _imp extension by calling manually _imp.create_builtin() and |
2513 | // _imp.exec_builtin() since importlib is not initialized yet. Initializing |
2514 | // importlib requires the _imp module: this function fix the bootstrap issue. |
2515 | PyObject* |
2516 | _PyImport_BootstrapImp(PyThreadState *tstate) |
2517 | { |
2518 | PyObject *name = PyUnicode_FromString("_imp"); |
2519 | if (name == NULL) { Branch (2519:9): [True: 0, False: 278]
|
2520 | return NULL; |
2521 | } |
2522 | |
2523 | // Mock a ModuleSpec object just good enough for PyModule_FromDefAndSpec(): |
2524 | // an object with just a name attribute. |
2525 | // |
2526 | // _imp.__spec__ is overridden by importlib._bootstrap._instal() anyway. |
2527 | PyObject *attrs = Py_BuildValue("{sO}", "name", name); |
2528 | if (attrs == NULL) { Branch (2528:9): [True: 0, False: 278]
|
2529 | goto error; |
2530 | } |
2531 | PyObject *spec = _PyNamespace_New(attrs); |
2532 | Py_DECREF(attrs); |
2533 | if (spec == NULL) { Branch (2533:9): [True: 0, False: 278]
|
2534 | goto error; |
2535 | } |
2536 | |
2537 | // Create the _imp module from its definition. |
2538 | PyObject *mod = create_builtin(tstate, name, spec); |
2539 | Py_CLEAR(name); |
2540 | Py_DECREF(spec); |
2541 | if (mod == NULL) { Branch (2541:9): [True: 0, False: 278]
|
2542 | goto error; |
2543 | } |
2544 | assert(mod != Py_None); // not found |
2545 | |
2546 | // Execute the _imp module: call imp_module_exec(). |
2547 | if (exec_builtin_or_dynamic(mod) < 0) { Branch (2547:9): [True: 0, False: 278]
|
2548 | Py_DECREF(mod); |
2549 | goto error; |
2550 | } |
2551 | return mod; |
2552 | |
2553 | error: |
2554 | Py_XDECREF(name); |
2555 | return NULL; |
2556 | } |
2557 | |
2558 | |
2559 | /* API for embedding applications that want to add their own entries |
2560 | to the table of built-in modules. This should normally be called |
2561 | *before* Py_Initialize(). When the table resize fails, -1 is |
2562 | returned and the existing table is unchanged. |
2563 | |
2564 | After a similar function by Just van Rossum. */ |
2565 | |
2566 | int |
2567 | PyImport_ExtendInittab(struct _inittab *newtab) |
2568 | { |
2569 | struct _inittab *p; |
2570 | size_t i, n; |
2571 | int res = 0; |
2572 | |
2573 | /* Count the number of entries in both tables */ |
2574 | for (n = 0; newtab[n].name != NULL; n++4 ) Branch (2574:17): [True: 4, False: 4]
|
2575 | ; |
2576 | if (n == 0) Branch (2576:9): [True: 0, False: 4]
|
2577 | return 0; /* Nothing to do */ |
2578 | for (i = 0; 4 PyImport_Inittab[i].name != NULL; i++124 ) Branch (2578:17): [True: 124, False: 4]
|
2579 | ; |
2580 | |
2581 | /* Force default raw memory allocator to get a known allocator to be able |
2582 | to release the memory in _PyImport_Fini2() */ |
2583 | PyMemAllocatorEx old_alloc; |
2584 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
2585 | |
2586 | /* Allocate new memory for the combined table */ |
2587 | p = NULL; |
2588 | if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) { Branch (2588:9): [True: 4, False: 0]
|
2589 | size_t size = sizeof(struct _inittab) * (i + n + 1); |
2590 | p = PyMem_RawRealloc(inittab_copy, size); |
2591 | } |
2592 | if (p == NULL) { Branch (2592:9): [True: 0, False: 4]
|
2593 | res = -1; |
2594 | goto done; |
2595 | } |
2596 | |
2597 | /* Copy the tables into the new memory at the first call |
2598 | to PyImport_ExtendInittab(). */ |
2599 | if (inittab_copy != PyImport_Inittab) { Branch (2599:9): [True: 4, False: 0]
|
2600 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
2601 | } |
2602 | memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab)); |
2603 | PyImport_Inittab = inittab_copy = p; |
2604 | |
2605 | done: |
2606 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
2607 | return res; |
2608 | } |
2609 | |
2610 | /* Shorthand to add a single entry given a name and a function */ |
2611 | |
2612 | int |
2613 | PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) |
2614 | { |
2615 | struct _inittab newtab[2]; |
2616 | |
2617 | memset(newtab, '\0', sizeof newtab); |
2618 | |
2619 | newtab[0].name = name; |
2620 | newtab[0].initfunc = initfunc; |
2621 | |
2622 | return PyImport_ExtendInittab(newtab); |
2623 | } |
2624 | |
2625 | |
2626 | PyObject * |
2627 | _PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname) |
2628 | { |
2629 | PyObject *mod = PyImport_Import(modname); |
2630 | if (mod == NULL) { Branch (2630:9): [True: 0, False: 59.9k]
|
2631 | return NULL; |
2632 | } |
2633 | PyObject *result = PyObject_GetAttr(mod, attrname); |
2634 | Py_DECREF(mod); |
2635 | return result; |
2636 | } |
2637 | |
2638 | PyObject * |
2639 | _PyImport_GetModuleAttrString(const char *modname, const char *attrname) |
2640 | { |
2641 | PyObject *pmodname = PyUnicode_FromString(modname); |
2642 | if (pmodname == NULL) { Branch (2642:9): [True: 0, False: 59.9k]
|
2643 | return NULL; |
2644 | } |
2645 | PyObject *pattrname = PyUnicode_FromString(attrname); |
2646 | if (pattrname == NULL) { Branch (2646:9): [True: 0, False: 59.9k]
|
2647 | Py_DECREF(pmodname); |
2648 | return NULL; |
2649 | } |
2650 | PyObject *result = _PyImport_GetModuleAttr(pmodname, pattrname); |
2651 | Py_DECREF(pattrname); |
2652 | Py_DECREF(pmodname); |
2653 | return result; |
2654 | } |
2655 | |
2656 | #ifdef __cplusplus |
2657 | } |
2658 | #endif |