Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
866594a2
Commit
866594a2
authored
May 12, 2014
by
Eric Snow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21226: Set all attrs in PyImport_ExecCodeModuleObject.
parent
646253bc
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
4319 additions
and
4269 deletions
+4319
-4269
Doc/c-api/import.rst
Doc/c-api/import.rst
+7
-1
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+23
-0
Misc/NEWS
Misc/NEWS
+2
-0
Python/import.c
Python/import.c
+12
-20
Python/importlib.h
Python/importlib.h
+4275
-4248
No files found.
Doc/c-api/import.rst
View file @
866594a2
...
...
@@ -132,8 +132,14 @@ Importing Modules
such modules have no way to know that the module object is an unknown (and
probably damaged with respect to the module author's intents) state.
The module's :attr:`__spec__` and :attr:`__loader__` will be set, if
not set already, with the appropriate values. The spec's loader will
be set to the module's ``__loader__`` (if set) and to an instance of
:class:`SourceFileLoader` otherwise.
The module's :attr:`__file__` attribute will be set to the code object's
:c:member:`co_filename`.
:c:member:`co_filename`. If applicable, :attr:`__cached__` will also
be set.
This function will reload the module if it was already imported. See
:c:func:`PyImport_ReloadModule` for the intended way to reload a module.
...
...
Lib/importlib/_bootstrap.py
View file @
866594a2
...
...
@@ -1220,6 +1220,29 @@ class _SpecMethods:
return
self
.
_load_unlocked
()
def
_fix_up_module
(
ns
,
name
,
pathname
,
cpathname
=
None
):
# This function is used by PyImport_ExecCodeModuleObject().
loader
=
ns
.
get
(
'__loader__'
)
spec
=
ns
.
get
(
'__spec__'
)
if
not
loader
:
if
spec
:
loader
=
spec
.
loader
elif
pathname
==
cpathname
:
loader
=
SourcelessFileLoader
(
name
,
pathname
)
else
:
loader
=
SourceFileLoader
(
name
,
pathname
)
if
not
spec
:
spec
=
spec_from_file_location
(
name
,
pathname
,
loader
=
loader
)
try
:
ns
[
'__spec__'
]
=
spec
ns
[
'__loader__'
]
=
loader
ns
[
'__file__'
]
=
pathname
ns
[
'__cached__'
]
=
cpathname
except
Exception
:
# Not important enough to report.
pass
# Loaders #####################################################################
class
BuiltinImporter
:
...
...
Misc/NEWS
View file @
866594a2
...
...
@@ -247,6 +247,8 @@ Extension Modules
-----------------
- Issue #21276: posixmodule: Don'
t
define
USE_XATTRS
on
KFreeBSD
and
the
Hurd
.
-
Issue
#
21226
:
Set
up
modules
properly
in
PyImport_ExecCodeModuleObject
(
and
friends
).
IDLE
----
...
...
Python/import.c
View file @
866594a2
...
...
@@ -856,7 +856,7 @@ module_dict_for_exec(PyObject *name)
}
}
return
d
;
return
d
;
/* Return a borrowed reference. */
}
static
PyObject
*
...
...
@@ -888,33 +888,25 @@ PyObject*
PyImport_ExecCodeModuleObject
(
PyObject
*
name
,
PyObject
*
co
,
PyObject
*
pathname
,
PyObject
*
cpathname
)
{
PyObject
*
d
,
*
v
;
PyObject
*
d
,
*
res
;
PyInterpreterState
*
interp
=
PyThreadState_GET
()
->
interp
;
_Py_IDENTIFIER
(
_fix_up_module
);
d
=
module_dict_for_exec
(
name
);
if
(
d
==
NULL
)
{
return
NULL
;
}
if
(
pathname
!
=
NULL
)
{
v
=
path
name
;
if
(
pathname
=
=
NULL
)
{
pathname
=
((
PyCodeObject
*
)
co
)
->
co_file
name
;
}
else
{
v
=
((
PyCodeObject
*
)
co
)
->
co_filename
;
res
=
_PyObject_CallMethodIdObjArgs
(
interp
->
importlib
,
&
PyId__fix_up_module
,
d
,
name
,
pathname
,
cpathname
,
NULL
);
if
(
res
!=
NULL
)
{
res
=
exec_code_in_module
(
name
,
d
,
co
);
}
Py_INCREF
(
v
);
if
(
PyDict_SetItemString
(
d
,
"__file__"
,
v
)
!=
0
)
PyErr_Clear
();
/* Not important enough to report */
Py_DECREF
(
v
);
/* Remember the pyc path name as the __cached__ attribute. */
if
(
cpathname
!=
NULL
)
v
=
cpathname
;
else
v
=
Py_None
;
if
(
PyDict_SetItemString
(
d
,
"__cached__"
,
v
)
!=
0
)
PyErr_Clear
();
/* Not important enough to report */
return
exec_code_in_module
(
name
,
d
,
co
);
return
res
;
}
...
...
Python/importlib.h
View file @
866594a2
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment