Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 410b85a

Browse files
authored
bpo-36900: import.c uses PyInterpreterState.core_config (GH-13278)
Move _PyImportZip_Init() to the internal C API and add an 'interp' parameter.
1 parent 2c10538 commit 410b85a

4 files changed

Lines changed: 48 additions & 29 deletions

File tree

Include/import.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ extern "C" {
88
#endif
99

1010
#ifndef Py_LIMITED_API
11-
PyAPI_FUNC(_PyInitError) _PyImportZip_Init(void);
12-
1311
PyMODINIT_FUNC PyInit__imp(void);
1412
#endif /* !Py_LIMITED_API */
1513
PyAPI_FUNC(long) PyImport_GetMagicNumber(void);

Include/internal/pycore_pylifecycle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ extern int _PyFloat_Init(void);
5454
extern _PyInitError _Py_HashRandomization_Init(const _PyCoreConfig *);
5555

5656
extern _PyInitError _PyTypes_Init(void);
57+
extern _PyInitError _PyImportZip_Init(PyInterpreterState *interp);
58+
5759

5860
/* Various internal finalizers */
5961

Python/import.c

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ _PyImportHooks_Init(void)
9191
}
9292

9393
_PyInitError
94-
_PyImportZip_Init(void)
94+
_PyImportZip_Init(PyInterpreterState *interp)
9595
{
9696
PyObject *path_hooks, *zipimport;
9797
int err = 0;
@@ -102,14 +102,17 @@ _PyImportZip_Init(void)
102102
goto error;
103103
}
104104

105-
if (Py_VerboseFlag)
105+
int verbose = interp->core_config.verbose;
106+
if (verbose) {
106107
PySys_WriteStderr("# installing zipimport hook\n");
108+
}
107109

108110
zipimport = PyImport_ImportModule("zipimport");
109111
if (zipimport == NULL) {
110112
PyErr_Clear(); /* No zip import module -- okay */
111-
if (Py_VerboseFlag)
113+
if (verbose) {
112114
PySys_WriteStderr("# can't import zipimport\n");
115+
}
113116
}
114117
else {
115118
_Py_IDENTIFIER(zipimporter);
@@ -118,9 +121,9 @@ _PyImportZip_Init(void)
118121
Py_DECREF(zipimport);
119122
if (zipimporter == NULL) {
120123
PyErr_Clear(); /* No zipimporter object -- okay */
121-
if (Py_VerboseFlag)
122-
PySys_WriteStderr(
123-
"# can't import zipimport.zipimporter\n");
124+
if (verbose) {
125+
PySys_WriteStderr("# can't import zipimport.zipimporter\n");
126+
}
124127
}
125128
else {
126129
/* sys.path_hooks.insert(0, zipimporter) */
@@ -129,9 +132,9 @@ _PyImportZip_Init(void)
129132
if (err < 0) {
130133
goto error;
131134
}
132-
if (Py_VerboseFlag)
133-
PySys_WriteStderr(
134-
"# installed zipimport hook\n");
135+
if (verbose) {
136+
PySys_WriteStderr("# installed zipimport hook\n");
137+
}
135138
}
136139
}
137140

@@ -415,22 +418,26 @@ PyImport_Cleanup(void)
415418

416419
/* XXX Perhaps these precautions are obsolete. Who knows? */
417420

418-
if (Py_VerboseFlag)
421+
int verbose = interp->core_config.verbose;
422+
if (verbose) {
419423
PySys_WriteStderr("# clear builtins._\n");
424+
}
420425
if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
421426
PyErr_WriteUnraisable(NULL);
422427
}
423428

424429
for (p = sys_deletes; *p != NULL; p++) {
425-
if (Py_VerboseFlag)
430+
if (verbose) {
426431
PySys_WriteStderr("# clear sys.%s\n", *p);
432+
}
427433
if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
428434
PyErr_WriteUnraisable(NULL);
429435
}
430436
}
431437
for (p = sys_files; *p != NULL; p+=2) {
432-
if (Py_VerboseFlag)
438+
if (verbose) {
433439
PySys_WriteStderr("# restore sys.%s\n", *p);
440+
}
434441
value = _PyDict_GetItemStringWithError(interp->sysdict, *(p+1));
435442
if (value == NULL) {
436443
if (PyErr_Occurred()) {
@@ -469,8 +476,9 @@ PyImport_Cleanup(void)
469476
}
470477
#define CLEAR_MODULE(name, mod) \
471478
if (PyModule_Check(mod)) { \
472-
if (Py_VerboseFlag && PyUnicode_Check(name)) \
479+
if (verbose && PyUnicode_Check(name)) { \
473480
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
481+
} \
474482
STORE_MODULE_WEAKREF(name, mod); \
475483
if (PyObject_SetItem(modules, name, Py_None) < 0) { \
476484
PyErr_WriteUnraisable(NULL); \
@@ -563,20 +571,23 @@ PyImport_Cleanup(void)
563571
if (dict == interp->builtins || dict == interp->sysdict)
564572
continue;
565573
Py_INCREF(mod);
566-
if (Py_VerboseFlag && PyUnicode_Check(name))
574+
if (verbose && PyUnicode_Check(name)) {
567575
PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
576+
}
568577
_PyModule_Clear(mod);
569578
Py_DECREF(mod);
570579
}
571580
Py_DECREF(weaklist);
572581
}
573582

574583
/* Next, delete sys and builtins (in that order) */
575-
if (Py_VerboseFlag)
584+
if (verbose) {
576585
PySys_FormatStderr("# cleanup[3] wiping sys\n");
586+
}
577587
_PyModule_ClearDict(interp->sysdict);
578-
if (Py_VerboseFlag)
588+
if (verbose) {
579589
PySys_FormatStderr("# cleanup[3] wiping builtins\n");
590+
}
580591
_PyModule_ClearDict(interp->builtins);
581592

582593
/* Clear and delete the modules directory. Actual modules will
@@ -755,9 +766,11 @@ _PyImport_FindExtensionObjectEx(PyObject *name, PyObject *filename,
755766
PyMapping_DelItem(modules, name);
756767
return NULL;
757768
}
758-
if (Py_VerboseFlag)
769+
int verbose = _PyInterpreterState_Get()->core_config.verbose;
770+
if (verbose) {
759771
PySys_FormatStderr("import %U # previously loaded (%R)\n",
760-
name, filename);
772+
name, filename);
773+
}
761774
return mod;
762775

763776
}
@@ -1427,7 +1440,7 @@ PyImport_ImportModuleNoBlock(const char *name)
14271440
/* Remove importlib frames from the traceback,
14281441
* except in Verbose mode. */
14291442
static void
1430-
remove_importlib_frames(void)
1443+
remove_importlib_frames(PyInterpreterState *interp)
14311444
{
14321445
const char *importlib_filename = "<frozen importlib._bootstrap>";
14331446
const char *external_filename = "<frozen importlib._bootstrap_external>";
@@ -1442,8 +1455,10 @@ remove_importlib_frames(void)
14421455
which end with a call to "_call_with_frames_removed". */
14431456

14441457
PyErr_Fetch(&exception, &value, &base_tb);
1445-
if (!exception || Py_VerboseFlag)
1458+
if (!exception || interp->core_config.verbose) {
14461459
goto done;
1460+
}
1461+
14471462
if (PyType_IsSubtype((PyTypeObject *) exception,
14481463
(PyTypeObject *) PyExc_ImportError))
14491464
always_trim = 1;
@@ -1853,8 +1868,9 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
18531868
Py_XDECREF(abs_name);
18541869
Py_XDECREF(mod);
18551870
Py_XDECREF(package);
1856-
if (final_mod == NULL)
1857-
remove_importlib_frames();
1871+
if (final_mod == NULL) {
1872+
remove_importlib_frames(interp);
1873+
}
18581874
return final_mod;
18591875
}
18601876

@@ -2303,13 +2319,16 @@ PyInit__imp(void)
23032319
PyObject *m, *d;
23042320

23052321
m = PyModule_Create(&impmodule);
2306-
if (m == NULL)
2322+
if (m == NULL) {
23072323
goto failure;
2324+
}
23082325
d = PyModule_GetDict(m);
2309-
if (d == NULL)
2326+
if (d == NULL) {
23102327
goto failure;
2311-
_PyCoreConfig *config = &_PyInterpreterState_Get()->core_config;
2312-
PyObject *pyc_mode = PyUnicode_FromWideChar(config->check_hash_pycs_mode, -1);
2328+
}
2329+
2330+
const wchar_t *mode = _PyInterpreterState_Get()->core_config.check_hash_pycs_mode;
2331+
PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
23132332
if (pyc_mode == NULL) {
23142333
goto failure;
23152334
}

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ init_importlib_external(PyInterpreterState *interp)
205205
return _Py_INIT_ERR("external importer setup failed");
206206
}
207207
Py_DECREF(value);
208-
return _PyImportZip_Init();
208+
return _PyImportZip_Init(interp);
209209
}
210210

211211
/* Helper functions to better handle the legacy C locale

0 commit comments

Comments
 (0)