diff -r 395904f70d6a Doc/c-api/import.rst --- a/Doc/c-api/import.rst Fri Mar 21 11:24:40 2014 -0400 +++ b/Doc/c-api/import.rst Fri Mar 21 14:35:27 2014 -0400 @@ -245,6 +245,9 @@ .. versionadded:: 3.3 + .. versionchanged:: 3.5 + The ``__file__`` attribute is no longer set on the module. + .. c:function:: int PyImport_ImportFrozenModule(const char *name) diff -r 395904f70d6a Python/import.c --- a/Python/import.c Fri Mar 21 11:24:40 2014 -0400 +++ b/Python/import.c Fri Mar 21 14:35:27 2014 -0400 @@ -837,12 +837,10 @@ return m; } -PyObject* -PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, - PyObject *cpathname) +static PyObject * +module_dict_for_exec(PyObject *name) { - PyObject *modules = PyImport_GetModuleDict(); - PyObject *m, *d, *v; + PyObject *m, *d = NULL; m = PyImport_AddModuleObject(name); if (m == NULL) @@ -852,9 +850,49 @@ d = PyModule_GetDict(m); if (PyDict_GetItemString(d, "__builtins__") == NULL) { if (PyDict_SetItemString(d, "__builtins__", - PyEval_GetBuiltins()) != 0) - goto error; + PyEval_GetBuiltins()) != 0) { + remove_module(name); + return NULL; + } } + + return d; +} + +static PyObject * +exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object) +{ + PyObject *modules = PyImport_GetModuleDict(); + PyObject *v, *m; + + v = PyEval_EvalCode(code_object, module_dict, module_dict); + if (v == NULL) + return NULL; + Py_DECREF(v); + + if ((m = PyDict_GetItem(modules, name)) == NULL) { + PyErr_Format(PyExc_ImportError, + "Loaded module %R not found in sys.modules", + name); + return NULL; + } + + Py_INCREF(m); + + return m; +} + +PyObject* +PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, + PyObject *cpathname) +{ + PyObject *d, *v; + + d = module_dict_for_exec(name); + if (d == NULL) { + return NULL; + } + if (pathname != NULL) { v = pathname; } @@ -879,16 +917,7 @@ goto error; Py_DECREF(v); - if ((m = PyDict_GetItem(modules, name)) == NULL) { - PyErr_Format(PyExc_ImportError, - "Loaded module %R not found in sys.modules", - name); - return NULL; - } - - Py_INCREF(m); - - return m; + return exec_code_in_module(name, d, co); error: remove_module(name); @@ -1206,7 +1235,7 @@ PyImport_ImportFrozenModuleObject(PyObject *name) { const struct _frozen *p; - PyObject *co, *m, *path; + PyObject *co, *m, *d; int ispackage; int size; @@ -1250,11 +1279,8 @@ if (err != 0) goto err_return; } - path = PyUnicode_FromString(""); - if (path == NULL) - goto err_return; - m = PyImport_ExecCodeModuleObject(name, co, path, NULL); - Py_DECREF(path); + d = module_dict_for_exec(name); + m = exec_code_in_module(name, d, co); if (m == NULL) goto err_return; Py_DECREF(co);