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

Skip to content

Commit 93c92f7

Browse files
bpo-31404: Revert "remove modules from Py_InterpreterState (#1638)" (#3565)
PR #1638, for bpo-28411, causes problems in some (very) edge cases. Until that gets sorted out, we're reverting the merge. PR #3506, a fix on top of #1638, is also getting reverted.
1 parent 13ad3b7 commit 93c92f7

21 files changed

+154
-308
lines changed

Doc/c-api/import.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,6 @@ Importing Modules
204204
Return the dictionary used for the module administration (a.k.a.
205205
``sys.modules``). Note that this is a per-interpreter variable.
206206
207-
.. c:function:: PyObject* PyImport_GetModule(PyObject *name)
208-
209-
Return the already imported module with the given name. If the
210-
module has not been imported yet then returns NULL but does not set
211-
an error. Returns NULL and sets an error if the lookup failed.
212-
213-
.. versionadded:: 3.7
214207
215208
.. c:function:: PyObject* PyImport_GetImporter(PyObject *path)
216209

Doc/whatsnew/3.7.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,6 @@ Changes in the Python API
486486
and module are affected by this change. (Contributed by INADA Naoki and
487487
Eugene Toder in :issue:`29463`.)
488488

489-
* ``PyInterpreterState`` no longer has a ``modules`` field. Instead use
490-
``sys.modules``.
491-
492489
* The *mode* argument of :func:`os.makedirs` no longer affects the file
493490
permission bits of newly-created intermediate-level directories.
494491
To set their file permission bits you can set the umask before invoking

Include/import.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,11 @@ PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleObject(
3838
);
3939
#endif
4040
PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void);
41-
#ifndef Py_LIMITED_API
42-
PyAPI_FUNC(int) _PyImport_IsInitialized(PyInterpreterState *);
43-
#endif
44-
PyAPI_FUNC(PyObject *) PyImport_GetModule(PyObject *name);
45-
#ifndef Py_LIMITED_API
46-
PyAPI_FUNC(PyObject *) _PyImport_GetModule(PyObject *name);
47-
PyAPI_FUNC(PyObject *) _PyImport_GetModuleWithError(PyObject *name);
48-
PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(struct _Py_Identifier *name);
49-
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
50-
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
51-
#endif
5241
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
5342
PyAPI_FUNC(PyObject *) PyImport_AddModuleObject(
5443
PyObject *name
5544
);
5645
#endif
57-
#ifndef Py_LIMITED_API
58-
PyAPI_FUNC(PyObject *) _PyImport_AddModuleObject(PyObject *, PyObject *);
59-
#endif
6046
PyAPI_FUNC(PyObject *) PyImport_AddModule(
6147
const char *name /* UTF-8 encoded string */
6248
);
@@ -106,19 +92,14 @@ PyAPI_FUNC(int) _PyImport_ReleaseLock(void);
10692
PyAPI_FUNC(void) _PyImport_ReInitLock(void);
10793

10894
PyAPI_FUNC(PyObject *) _PyImport_FindBuiltin(
109-
const char *name, /* UTF-8 encoded string */
110-
PyObject *modules
95+
const char *name /* UTF-8 encoded string */
11196
);
11297
PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObject(PyObject *, PyObject *);
113-
PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObjectEx(PyObject *, PyObject *,
114-
PyObject *);
11598
PyAPI_FUNC(int) _PyImport_FixupBuiltin(
11699
PyObject *mod,
117-
const char *name, /* UTF-8 encoded string */
118-
PyObject *modules
100+
const char *name /* UTF-8 encoded string */
119101
);
120-
PyAPI_FUNC(int) _PyImport_FixupExtensionObject(PyObject*, PyObject *,
121-
PyObject *, PyObject *);
102+
PyAPI_FUNC(int) _PyImport_FixupExtensionObject(PyObject*, PyObject *, PyObject *);
122103

123104
struct _inittab {
124105
const char *name; /* ASCII encoded string */

Include/modsupport.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,6 @@ PyAPI_FUNC(int) PyModule_ExecDef(PyObject *module, PyModuleDef *def);
191191

192192
PyAPI_FUNC(PyObject *) PyModule_Create2(struct PyModuleDef*,
193193
int apiver);
194-
#ifndef Py_LIMITED_API
195-
PyAPI_FUNC(PyObject *) _PyModule_CreateInitialized(struct PyModuleDef*,
196-
int apiver);
197-
#endif
198194

199195
#ifdef Py_LIMITED_API
200196
#define PyModule_Create(module) \

Include/object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,13 +727,14 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
727727
/* Py_REF_DEBUG also controls the display of refcounts and memory block
728728
* allocations at the interactive prompt and at interpreter shutdown
729729
*/
730-
PyAPI_FUNC(PyObject *) _PyDebug_XOptionShowRefCount(void);
731730
PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
731+
#define _PY_DEBUG_PRINT_TOTAL_REFS() _PyDebug_PrintTotalRefs()
732732
#else
733733
#define _Py_INC_REFTOTAL
734734
#define _Py_DEC_REFTOTAL
735735
#define _Py_REF_DEBUG_COMMA
736736
#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
737+
#define _PY_DEBUG_PRINT_TOTAL_REFS()
737738
#endif /* Py_REF_DEBUG */
738739

739740
#ifdef COUNT_ALLOCS

Include/pystate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ typedef struct _is {
5353

5454
int64_t id;
5555

56+
PyObject *modules;
5657
PyObject *modules_by_index;
5758
PyObject *sysdict;
5859
PyObject *builtins;
5960
PyObject *importlib;
6061

6162
/* Used in Python/sysmodule.c. */
6263
int check_interval;
64+
PyObject *warnoptions;
65+
PyObject *xoptions;
6366

6467
/* Used in Modules/_threadmodule.c. */
6568
long num_threads;

Misc/NEWS.d/next/Core and Builtins/2017-09-04-10-46-09.bpo-28411.IU9rQL.rst

Lines changed: 0 additions & 4 deletions
This file was deleted.

Modules/_pickle.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6425,7 +6425,9 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self,
64256425
/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
64266426
{
64276427
PyObject *global;
6428+
PyObject *modules_dict;
64286429
PyObject *module;
6430+
_Py_IDENTIFIER(modules);
64296431

64306432
/* Try to map the old names used in Python 2.x to the new ones used in
64316433
Python 3.x. We do this only with old pickle protocols and when the
@@ -6482,19 +6484,25 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self,
64826484
}
64836485
}
64846486

6485-
module = PyImport_GetModule(module_name);
6487+
modules_dict = _PySys_GetObjectId(&PyId_modules);
6488+
if (modules_dict == NULL) {
6489+
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
6490+
return NULL;
6491+
}
6492+
6493+
module = PyDict_GetItemWithError(modules_dict, module_name);
64866494
if (module == NULL) {
64876495
if (PyErr_Occurred())
64886496
return NULL;
64896497
module = PyImport_Import(module_name);
64906498
if (module == NULL)
64916499
return NULL;
64926500
global = getattribute(module, global_name, self->proto >= 4);
6501+
Py_DECREF(module);
64936502
}
64946503
else {
64956504
global = getattribute(module, global_name, self->proto >= 4);
64966505
}
6497-
Py_DECREF(module);
64986506
return global;
64996507
}
65006508

Modules/pyexpat.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,7 @@ MODULE_INITFUNC(void)
16431643
PyObject *errors_module;
16441644
PyObject *modelmod_name;
16451645
PyObject *model_module;
1646+
PyObject *sys_modules;
16461647
PyObject *tmpnum, *tmpstr;
16471648
PyObject *codes_dict;
16481649
PyObject *rev_codes_dict;
@@ -1692,6 +1693,11 @@ MODULE_INITFUNC(void)
16921693
*/
16931694
PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
16941695

1696+
sys_modules = PySys_GetObject("modules");
1697+
if (sys_modules == NULL) {
1698+
Py_DECREF(m);
1699+
return NULL;
1700+
}
16951701
d = PyModule_GetDict(m);
16961702
if (d == NULL) {
16971703
Py_DECREF(m);
@@ -1701,7 +1707,7 @@ MODULE_INITFUNC(void)
17011707
if (errors_module == NULL) {
17021708
errors_module = PyModule_New(MODULE_NAME ".errors");
17031709
if (errors_module != NULL) {
1704-
_PyImport_SetModule(errmod_name, errors_module);
1710+
PyDict_SetItem(sys_modules, errmod_name, errors_module);
17051711
/* gives away the reference to errors_module */
17061712
PyModule_AddObject(m, "errors", errors_module);
17071713
}
@@ -1711,7 +1717,7 @@ MODULE_INITFUNC(void)
17111717
if (model_module == NULL) {
17121718
model_module = PyModule_New(MODULE_NAME ".model");
17131719
if (model_module != NULL) {
1714-
_PyImport_SetModule(modelmod_name, model_module);
1720+
PyDict_SetItem(sys_modules, modelmod_name, model_module);
17151721
/* gives away the reference to model_module */
17161722
PyModule_AddObject(m, "model", model_module);
17171723
}

Objects/moduleobject.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,12 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
161161

162162
PyObject *
163163
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
164-
{
165-
if (!_PyImport_IsInitialized(PyThreadState_GET()->interp))
166-
Py_FatalError("Python import machinery not initialized");
167-
return _PyModule_CreateInitialized(module, module_api_version);
168-
}
169-
170-
PyObject *
171-
_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
172164
{
173165
const char* name;
174166
PyModuleObject *m;
175-
167+
PyInterpreterState *interp = PyThreadState_Get()->interp;
168+
if (interp->modules == NULL)
169+
Py_FatalError("Python import machinery not initialized");
176170
if (!PyModuleDef_Init(module))
177171
return NULL;
178172
name = module->m_name;

Objects/object.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,20 @@ _Py_GetRefTotal(void)
2929
return total;
3030
}
3131

32-
PyObject *
33-
_PyDebug_XOptionShowRefCount(void)
34-
{
35-
PyObject *xoptions = PySys_GetXOptions();
36-
if (xoptions == NULL)
37-
return NULL;
38-
39-
_Py_IDENTIFIER(showrefcount);
40-
return _PyDict_GetItemId(xoptions, &PyId_showrefcount);
41-
}
42-
4332
void
4433
_PyDebug_PrintTotalRefs(void) {
45-
fprintf(stderr,
46-
"[%" PY_FORMAT_SIZE_T "d refs, "
47-
"%" PY_FORMAT_SIZE_T "d blocks]\n",
48-
_Py_GetRefTotal(), _Py_GetAllocatedBlocks());
34+
PyObject *xoptions, *value;
35+
_Py_IDENTIFIER(showrefcount);
36+
37+
xoptions = PySys_GetXOptions();
38+
if (xoptions == NULL)
39+
return;
40+
value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
41+
if (value == Py_True)
42+
fprintf(stderr,
43+
"[%" PY_FORMAT_SIZE_T "d refs, "
44+
"%" PY_FORMAT_SIZE_T "d blocks]\n",
45+
_Py_GetRefTotal(), _Py_GetAllocatedBlocks());
4946
}
5047
#endif /* Py_REF_DEBUG */
5148

Objects/typeobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,7 @@ import_copyreg(void)
39023902
{
39033903
PyObject *copyreg_str;
39043904
PyObject *copyreg_module;
3905+
PyInterpreterState *interp = PyThreadState_GET()->interp;
39053906
_Py_IDENTIFIER(copyreg);
39063907

39073908
copyreg_str = _PyUnicode_FromId(&PyId_copyreg);
@@ -3913,7 +3914,7 @@ import_copyreg(void)
39133914
by storing a reference to the cached module in a static variable, but
39143915
this broke when multiple embedded interpreters were in use (see issue
39153916
#17408 and #19088). */
3916-
copyreg_module = _PyImport_GetModuleWithError(copyreg_str);
3917+
copyreg_module = PyDict_GetItemWithError(interp->modules, copyreg_str);
39173918
if (copyreg_module != NULL) {
39183919
Py_INCREF(copyreg_module);
39193920
return copyreg_module;

Python/_warnings.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static PyObject *
3838
get_warnings_attr(const char *attr, int try_import)
3939
{
4040
static PyObject *warnings_str = NULL;
41+
PyObject *all_modules;
4142
PyObject *warnings_module, *obj;
4243

4344
if (warnings_str == NULL) {
@@ -57,7 +58,9 @@ get_warnings_attr(const char *attr, int try_import)
5758
}
5859
}
5960
else {
60-
warnings_module = _PyImport_GetModule(warnings_str);
61+
all_modules = PyImport_GetModuleDict();
62+
63+
warnings_module = PyDict_GetItem(all_modules, warnings_str);
6164
if (warnings_module == NULL)
6265
return NULL;
6366

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,7 @@ _PyBuiltin_Init(void)
26852685
PyType_Ready(&PyZip_Type) < 0)
26862686
return NULL;
26872687

2688-
mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
2688+
mod = PyModule_Create(&builtinsmodule);
26892689
if (mod == NULL)
26902690
return NULL;
26912691
dict = PyModule_GetDict(mod);

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4935,7 +4935,7 @@ import_from(PyObject *v, PyObject *name)
49354935
Py_DECREF(pkgname);
49364936
return NULL;
49374937
}
4938-
x = _PyImport_GetModule(fullmodname);
4938+
x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
49394939
Py_DECREF(fullmodname);
49404940
if (x == NULL) {
49414941
goto error;

0 commit comments

Comments
 (0)