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

Skip to content

Commit 18fc4e7

Browse files
committed
Issue #20942: PyImport_ImportFrozenModuleObject() no longer sets
__file__. This causes _frozen_importlib to no longer have __file__ set as well as any frozen module imported using imp.init_frozen() (which is deprecated).
1 parent 1cd9825 commit 18fc4e7

3 files changed

Lines changed: 63 additions & 32 deletions

File tree

Doc/c-api/import.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ Importing Modules
245245
246246
.. versionadded:: 3.3
247247
248+
.. versionchanged:: 3.4
249+
The ``__file__`` attribute is no longer set on the module.
250+
248251
249252
.. c:function:: int PyImport_ImportFrozenModule(const char *name)
250253

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ IDLE
109109
- Issue #17654: Ensure IDLE menus are customized properly on OS X for
110110
non-framework builds and for all variants of Tk.
111111

112+
C API
113+
-----
114+
- Issue #20942: PyImport_ImportFrozenModuleObject() no longer sets __file__ to
115+
match what importlib does; this affects _frozen_importlib as well as any
116+
module loaded using imp.init_frozen().
117+
112118
Documentation
113119
-------------
114120

Python/import.c

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,10 @@ PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
837837
return m;
838838
}
839839

840-
PyObject*
841-
PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
842-
PyObject *cpathname)
840+
static PyObject *
841+
module_dict_for_exec(PyObject *name)
843842
{
844-
PyObject *modules = PyImport_GetModuleDict();
845-
PyObject *m, *d, *v;
843+
PyObject *m, *d = NULL;
846844

847845
m = PyImport_AddModuleObject(name);
848846
if (m == NULL)
@@ -852,9 +850,51 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
852850
d = PyModule_GetDict(m);
853851
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
854852
if (PyDict_SetItemString(d, "__builtins__",
855-
PyEval_GetBuiltins()) != 0)
856-
goto error;
853+
PyEval_GetBuiltins()) != 0) {
854+
remove_module(name);
855+
return NULL;
856+
}
857+
}
858+
859+
return d;
860+
}
861+
862+
static PyObject *
863+
exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object)
864+
{
865+
PyObject *modules = PyImport_GetModuleDict();
866+
PyObject *v, *m;
867+
868+
v = PyEval_EvalCode(code_object, module_dict, module_dict);
869+
if (v == NULL) {
870+
remove_module(name);
871+
return NULL;
872+
}
873+
Py_DECREF(v);
874+
875+
if ((m = PyDict_GetItem(modules, name)) == NULL) {
876+
PyErr_Format(PyExc_ImportError,
877+
"Loaded module %R not found in sys.modules",
878+
name);
879+
return NULL;
880+
}
881+
882+
Py_INCREF(m);
883+
884+
return m;
885+
}
886+
887+
PyObject*
888+
PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
889+
PyObject *cpathname)
890+
{
891+
PyObject *d, *v;
892+
893+
d = module_dict_for_exec(name);
894+
if (d == NULL) {
895+
return NULL;
857896
}
897+
858898
if (pathname != NULL) {
859899
v = pathname;
860900
}
@@ -874,25 +914,7 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
874914
if (PyDict_SetItemString(d, "__cached__", v) != 0)
875915
PyErr_Clear(); /* Not important enough to report */
876916

877-
v = PyEval_EvalCode(co, d, d);
878-
if (v == NULL)
879-
goto error;
880-
Py_DECREF(v);
881-
882-
if ((m = PyDict_GetItem(modules, name)) == NULL) {
883-
PyErr_Format(PyExc_ImportError,
884-
"Loaded module %R not found in sys.modules",
885-
name);
886-
return NULL;
887-
}
888-
889-
Py_INCREF(m);
890-
891-
return m;
892-
893-
error:
894-
remove_module(name);
895-
return NULL;
917+
return exec_code_in_module(name, d, co);
896918
}
897919

898920

@@ -1206,7 +1228,7 @@ int
12061228
PyImport_ImportFrozenModuleObject(PyObject *name)
12071229
{
12081230
const struct _frozen *p;
1209-
PyObject *co, *m, *path;
1231+
PyObject *co, *m, *d;
12101232
int ispackage;
12111233
int size;
12121234

@@ -1235,7 +1257,7 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
12351257
}
12361258
if (ispackage) {
12371259
/* Set __path__ to the empty list */
1238-
PyObject *d, *l;
1260+
PyObject *l;
12391261
int err;
12401262
m = PyImport_AddModuleObject(name);
12411263
if (m == NULL)
@@ -1250,11 +1272,11 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
12501272
if (err != 0)
12511273
goto err_return;
12521274
}
1253-
path = PyUnicode_FromString("<frozen>");
1254-
if (path == NULL)
1275+
d = module_dict_for_exec(name);
1276+
if (d == NULL) {
12551277
goto err_return;
1256-
m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1257-
Py_DECREF(path);
1278+
}
1279+
m = exec_code_in_module(name, d, co);
12581280
if (m == NULL)
12591281
goto err_return;
12601282
Py_DECREF(co);

0 commit comments

Comments
 (0)