Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-28411: Remove "modules" field from Py_InterpreterState. #1638

Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
70f6e19
Add _PyImport_GetModuleDict().
ericsnowcurrently Oct 7, 2016
10f7443
Import the sys module before any other imports.
ericsnowcurrently Oct 10, 2016
5ea0e3b
Pass the modules dict to _PyImport_FindBuiltin().
ericsnowcurrently Oct 10, 2016
29780ec
Replace usage of interp->modules with PyImport_GetModuleDict().
ericsnowcurrently Oct 7, 2016
e6de215
Add _PyImport_EnsureInitialized().
ericsnowcurrently Oct 10, 2016
29f6b7c
Add _PyModule_Create2().
ericsnowcurrently Oct 10, 2016
b77d73f
Use _PyModule_Create2() for key builtin modules.
ericsnowcurrently Oct 10, 2016
8275633
Check sys.modules in _PyImport_EnsureInitialized().
ericsnowcurrently Oct 10, 2016
7c62114
Drop PyInterpreterState.modules.
ericsnowcurrently Oct 7, 2016
428d223
Pass the modules dict to _PyImport_FixupBuiltin().
ericsnowcurrently Oct 10, 2016
833b00b
Pass PyInterpreterState to _PyImport_GetModuleDict().
ericsnowcurrently Oct 11, 2016
e0e51d2
Drop _PyImport_GetModuleDict().
ericsnowcurrently Oct 11, 2016
88aadeb
_PyModule_Create2 -> _PyModule_CreateInitialized
ericsnowcurrently May 17, 2017
79320bd
_PyImport_EnsureInitialized() -> _PyImport_IsInitialized()
ericsnowcurrently May 18, 2017
6a0215c
Add _PyImport_GetModule*.
ericsnowcurrently May 18, 2017
dabd187
Allow sys.modules to be any mapping.
ericsnowcurrently May 18, 2017
58d9be7
Add _PyImport_SetModule*.
ericsnowcurrently May 18, 2017
e3b0f0c
Add PyImport_GetModule().
ericsnowcurrently May 18, 2017
d9c6b79
Fix ref counts.
ericsnowcurrently May 18, 2017
e3565f9
Look up "new" modules in the given modules dict.
ericsnowcurrently May 18, 2017
9946341
Fix error checking.
ericsnowcurrently May 20, 2017
08e556e
Use PyImport_GetModuleDict() in PyImport_GetModule().
ericsnowcurrently May 23, 2017
f043e47
Decref the module when done.
ericsnowcurrently May 25, 2017
b2edd25
Add a missing incref.
ericsnowcurrently May 25, 2017
0eced28
Fix post-rebase.
ericsnowcurrently Sep 4, 2017
a85a11f
Add a Misc/NEWS entry.
ericsnowcurrently Sep 4, 2017
9ba4c43
Make the docs for PyImport_GetModule() more clear.
ericsnowcurrently Sep 4, 2017
f837a61
Fix style (bracket placement).
ericsnowcurrently Sep 4, 2017
101aa31
Drop _PyImport_GetModuleString().
ericsnowcurrently Sep 4, 2017
8f8a7a2
Use PyDict_CheckExact() for sys.modules in fast case.
ericsnowcurrently Sep 4, 2017
3a56648
Do not use PyMapping_HasKey() with an error set.
ericsnowcurrently Sep 4, 2017
619b0a0
Revert a code order change.
ericsnowcurrently Sep 4, 2017
57eeb5b
Drop _PyImport_FixupExtensionObjectEx().
ericsnowcurrently Sep 4, 2017
6544fa2
Switch the code order back.
ericsnowcurrently Sep 4, 2017
8e86c53
Add a whatsnew entry for porting.
ericsnowcurrently Sep 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add _PyImport_SetModule*.
ericsnowcurrently committed Sep 4, 2017
commit 58d9be77a0ae6ab7d80ac740158a17db86f70477
4 changes: 4 additions & 0 deletions Include/import.h
Original file line number Diff line number Diff line change
@@ -40,9 +40,13 @@ PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleObject(
PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void);
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyImport_IsInitialized(PyInterpreterState *);
#endif
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyImport_GetModule(PyObject *name);
PyAPI_FUNC(PyObject *) _PyImport_GetModuleString(const char *name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't tink that it's worth it to add _PyImport_GetModuleString(): it's only used 2 times. Please use _PyImport_GetModuleId() with_Py_IDENTIFIER, and remove _PyImport_GetModuleString(). I dislike having N versions of the same function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll drop it.

PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(struct _Py_Identifier *name);
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyImport_AddModuleObject(
10 changes: 2 additions & 8 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
@@ -1643,7 +1643,6 @@ MODULE_INITFUNC(void)
PyObject *errors_module;
PyObject *modelmod_name;
PyObject *model_module;
PyObject *sys_modules;
PyObject *tmpnum, *tmpstr;
PyObject *codes_dict;
PyObject *rev_codes_dict;
@@ -1693,11 +1692,6 @@ MODULE_INITFUNC(void)
*/
PyModule_AddStringConstant(m, "native_encoding", "UTF-8");

sys_modules = PySys_GetObject("modules");
if (sys_modules == NULL) {
Py_DECREF(m);
return NULL;
}
d = PyModule_GetDict(m);
if (d == NULL) {
Py_DECREF(m);
@@ -1707,7 +1701,7 @@ MODULE_INITFUNC(void)
if (errors_module == NULL) {
errors_module = PyModule_New(MODULE_NAME ".errors");
if (errors_module != NULL) {
PyObject_SetItem(sys_modules, errmod_name, errors_module);
_PyImport_SetModule(errmod_name, errors_module);
/* gives away the reference to errors_module */
PyModule_AddObject(m, "errors", errors_module);
}
@@ -1717,7 +1711,7 @@ MODULE_INITFUNC(void)
if (model_module == NULL) {
model_module = PyModule_New(MODULE_NAME ".model");
if (model_module != NULL) {
PyObject_SetItem(sys_modules, modelmod_name, model_module);
_PyImport_SetModule(modelmod_name, model_module);
/* gives away the reference to model_module */
PyModule_AddObject(m, "model", model_module);
}
12 changes: 12 additions & 0 deletions Python/import.c
Original file line number Diff line number Diff line change
@@ -369,6 +369,18 @@ _PyImport_GetModuleId(struct _Py_Identifier *nameid)
return _PyImport_GetModule(name);
}

int
_PyImport_SetModule(PyObject *name, PyObject *m) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put { on a new line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

PyObject *modules = PyImport_GetModuleDict();
return PyObject_SetItem(modules, name, m);
}

int
_PyImport_SetModuleString(const char *name, PyObject *m) {
PyObject *modules = PyImport_GetModuleDict();
return PyMapping_SetItemString(modules, name, m);
}


/* List of names to clear in sys */
static const char * const sys_deletes[] = {
7 changes: 1 addition & 6 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
@@ -262,7 +262,6 @@ initimport(PyInterpreterState *interp, PyObject *sysmod)
{
PyObject *importlib;
PyObject *impmod;
PyObject *sys_modules;
PyObject *value;

/* Import _importlib through its frozen version, _frozen_importlib. */
@@ -293,11 +292,7 @@ initimport(PyInterpreterState *interp, PyObject *sysmod)
else if (Py_VerboseFlag) {
PySys_FormatStderr("import _imp # builtin\n");
}
sys_modules = PyImport_GetModuleDict();
if (Py_VerboseFlag) {
PySys_FormatStderr("import sys # builtin\n");
}
if (PyMapping_SetItemString(sys_modules, "_imp", impmod) < 0) {
if (_PyImport_SetModuleString("_imp", impmod) < 0) {
Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
}