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
56829d5b
Commit
56829d5b
authored
Jul 06, 2006
by
Nick Coghlan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert the __module_name__ changes made in rev 47142. We'll revisit this in Python 2.6
parent
bf84e540
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
47 deletions
+26
-47
Doc/lib/librunpy.tex
Doc/lib/librunpy.tex
+8
-13
Lib/runpy.py
Lib/runpy.py
+13
-21
Lib/test/test_runpy.py
Lib/test/test_runpy.py
+5
-13
No files found.
Doc/lib/librunpy.tex
View file @
56829d5b
...
@@ -35,19 +35,16 @@ The supplied dictionary will not be modified. If any of the special
...
@@ -35,19 +35,16 @@ The supplied dictionary will not be modified. If any of the special
global variables below are defined in the supplied dictionary, those
global variables below are defined in the supplied dictionary, those
definitions are overridden by the
\code
{
run
_
module
}
function.
definitions are overridden by the
\code
{
run
_
module
}
function.
The special global variables
\code
{__
name
__}
,
\code
{__
module
_
nam
e
__}
,
The special global variables
\code
{__
name
__}
,
\code
{__
fil
e
__}
,
\code
{__
file
__}
,
\code
{__
loader
__}
and
\code
{__
builtins
__}
are
\code
{__
loader
__}
and
\code
{__
builtins
__}
are set in the globals
set in the globals
dictionary before the module code is executed.
dictionary before the module code is executed.
\code
{__
name
__}
is set to
\var
{
run
_
name
}
if this optional argument is
\code
{__
name
__}
is set to
\var
{
run
_
name
}
if this optional argument is
supplied, and the
\var
{
mod
_
name
}
argument otherwise.
supplied, and the
\var
{
mod
_
name
}
argument otherwise.
\code
{__
module
_
name
__}
is always set to
\var
{
mod
_
name
}
(this allows
modules to use imports relative to their package name).
\code
{__
loader
__}
is set to the PEP 302 module loader used to retrieve
\code
{__
loader
__}
is set to the PEP 302 module loader used to retrieve
the code for the module (This
will not be defined if the module was
the code for the module (This
loader may be a wrapper around the
found using the
standard import mechanism).
standard import mechanism).
\code
{__
file
__}
is set to the name provided by the module loader. If
\code
{__
file
__}
is set to the name provided by the module loader. If
the loader does not make filename information available, this
the loader does not make filename information available, this
...
@@ -58,12 +55,10 @@ the top level namespace of the \module{__builtin__} module.
...
@@ -58,12 +55,10 @@ the top level namespace of the \module{__builtin__} module.
If the argument
\var
{
alter
_
sys
}
is supplied and evaluates to
If the argument
\var
{
alter
_
sys
}
is supplied and evaluates to
\code
{
True
}
, then
\code
{
sys.argv[0]
}
is updated with the value of
\code
{
True
}
, then
\code
{
sys.argv[0]
}
is updated with the value of
\code
{__
file
__}
and
\code
{
sys.modules[
mod
_
name
]
}
is updated with a
\code
{__
file
__}
and
\code
{
sys.modules[
__
name
__
]
}
is updated with a
temporary module object for the module being executed. Both
temporary module object for the module being executed. Both
\code
{
sys.argv[0]
}
and
\code
{
sys.modules[mod
_
name]
}
are restored to
\code
{
sys.argv[0]
}
and
\code
{
sys.modules[
__
name
__
]
}
are restored to
their original values before the function returns. If
\var
{
run
_
name
}
their original values before the function returns.
differs from
\var
{
mod
_
name
}
entries are made in
\code
{
sys.modules
}
for both names.
Note that this manipulation of
\module
{
sys
}
is not thread-safe. Other
Note that this manipulation of
\module
{
sys
}
is not thread-safe. Other
threads may see the partially initialised module, as well as the
threads may see the partially initialised module, as well as the
...
...
Lib/runpy.py
View file @
56829d5b
...
@@ -21,19 +21,18 @@ __all__ = [
...
@@ -21,19 +21,18 @@ __all__ = [
]
]
def
_run_code
(
code
,
run_globals
,
init_globals
,
run_name
,
def
_run_code
(
code
,
run_globals
,
init_globals
,
mod_name
,
mod_fname
,
mod_loader
):
mod_name
,
mod_fname
,
mod_loader
):
"""Helper for _run_module_code"""
"""Helper for _run_module_code"""
if
init_globals
is
not
None
:
if
init_globals
is
not
None
:
run_globals
.
update
(
init_globals
)
run_globals
.
update
(
init_globals
)
run_globals
.
update
(
__name__
=
run_name
,
run_globals
.
update
(
__name__
=
mod_name
,
__module_name__
=
mod_name
,
__file__
=
mod_fname
,
__file__
=
mod_fname
,
__loader__
=
mod_loader
)
__loader__
=
mod_loader
)
exec
code
in
run_globals
exec
code
in
run_globals
return
run_globals
return
run_globals
def
_run_module_code
(
code
,
init_globals
=
None
,
run_name
=
None
,
def
_run_module_code
(
code
,
init_globals
=
None
,
mod_name
=
None
,
mod_fname
=
None
,
mod_name
=
None
,
mod_fname
=
None
,
mod_loader
=
None
,
alter_sys
=
False
):
mod_loader
=
None
,
alter_sys
=
False
):
"""Helper for run_module"""
"""Helper for run_module"""
...
@@ -43,33 +42,26 @@ def _run_module_code(code, init_globals=None, run_name=None,
...
@@ -43,33 +42,26 @@ def _run_module_code(code, init_globals=None, run_name=None,
temp_module
=
imp
.
new_module
(
mod_name
)
temp_module
=
imp
.
new_module
(
mod_name
)
mod_globals
=
temp_module
.
__dict__
mod_globals
=
temp_module
.
__dict__
saved_argv0
=
sys
.
argv
[
0
]
saved_argv0
=
sys
.
argv
[
0
]
sentinel
=
object
()
restore_module
=
mod_name
in
sys
.
modules
module_mod_name
=
sys
.
modules
.
get
(
mod_name
,
sentinel
)
if
restore_module
:
module_run_name
=
sys
.
modules
.
get
(
run_name
,
sentinel
)
saved_module
=
sys
.
modules
[
mod_name
]
sys
.
argv
[
0
]
=
mod_fname
sys
.
argv
[
0
]
=
mod_fname
sys
.
modules
[
mod_name
]
=
temp_module
sys
.
modules
[
mod_name
]
=
temp_module
if
run_name
!=
mod_name
:
sys
.
modules
[
run_name
]
=
temp_module
try
:
try
:
_run_code
(
code
,
mod_globals
,
init_globals
,
run_name
,
_run_code
(
code
,
mod_globals
,
init_globals
,
mod_name
,
mod_fname
,
mod_loader
)
mod_name
,
mod_fname
,
mod_loader
)
finally
:
finally
:
sys
.
argv
[
0
]
=
saved_argv0
sys
.
argv
[
0
]
=
saved_argv0
if
module_mod_name
is
not
sentinel
:
if
restore_module
:
sys
.
modules
[
mod_name
]
=
module_mod_name
sys
.
modules
[
mod_name
]
=
saved_module
else
:
else
:
del
sys
.
modules
[
mod_name
]
del
sys
.
modules
[
mod_name
]
if
run_name
!=
mod_name
:
if
module_run_name
is
not
sentinel
:
sys
.
modules
[
run_name
]
=
module_run_name
else
:
del
sys
.
modules
[
run_name
]
# Copy the globals of the temporary module, as they
# Copy the globals of the temporary module, as they
# may be cleared when the temporary module goes away
# may be cleared when the temporary module goes away
return
mod_globals
.
copy
()
return
mod_globals
.
copy
()
else
:
else
:
# Leave the sys module alone
# Leave the sys module alone
return
_run_code
(
code
,
{},
init_globals
,
run_name
,
return
_run_code
(
code
,
{},
init_globals
,
mod_name
,
mod_fname
,
mod_loader
)
mod_name
,
mod_fname
,
mod_loader
)
...
@@ -100,7 +92,7 @@ def run_module(mod_name, init_globals=None,
...
@@ -100,7 +92,7 @@ def run_module(mod_name, init_globals=None,
if
run_name
is
None
:
if
run_name
is
None
:
run_name
=
mod_name
run_name
=
mod_name
return
_run_module_code
(
code
,
init_globals
,
run_name
,
return
_run_module_code
(
code
,
init_globals
,
run_name
,
mod_name
,
filename
,
loader
,
alter_sys
)
filename
,
loader
,
alter_sys
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
Lib/test/test_runpy.py
View file @
56829d5b
...
@@ -23,8 +23,6 @@ class RunModuleCodeTest(unittest.TestCase):
...
@@ -23,8 +23,6 @@ class RunModuleCodeTest(unittest.TestCase):
"run_argv0 = sys.argv[0]
\
n
"
"run_argv0 = sys.argv[0]
\
n
"
"if __name__ in sys.modules:
\
n
"
"if __name__ in sys.modules:
\
n
"
" run_name = sys.modules[__name__].__name__
\
n
"
" run_name = sys.modules[__name__].__name__
\
n
"
"if __module_name__ in sys.modules:
\
n
"
" mod_name = sys.modules[__module_name__].__module_name__
\
n
"
"# Check nested operation
\
n
"
"# Check nested operation
\
n
"
"import runpy
\
n
"
"import runpy
\
n
"
"nested = runpy._run_module_code('x=1
\
\
n', mod_name='<run>',
\
n
"
"nested = runpy._run_module_code('x=1
\
\
n', mod_name='<run>',
\
n
"
...
@@ -34,16 +32,14 @@ class RunModuleCodeTest(unittest.TestCase):
...
@@ -34,16 +32,14 @@ class RunModuleCodeTest(unittest.TestCase):
def
test_run_module_code
(
self
):
def
test_run_module_code
(
self
):
initial
=
object
()
initial
=
object
()
run_name
=
"<Nonsense>"
name
=
"<Nonsense>"
mod_name
=
"<ModuleNonsense>"
file
=
"Some other nonsense"
file
=
"Some other nonsense"
loader
=
"Now you're just being silly"
loader
=
"Now you're just being silly"
d1
=
dict
(
initial
=
initial
)
d1
=
dict
(
initial
=
initial
)
saved_argv0
=
sys
.
argv
[
0
]
saved_argv0
=
sys
.
argv
[
0
]
d2
=
_run_module_code
(
self
.
test_source
,
d2
=
_run_module_code
(
self
.
test_source
,
d1
,
d1
,
run_name
,
name
,
mod_name
,
file
,
file
,
loader
,
loader
,
True
)
True
)
...
@@ -51,23 +47,19 @@ class RunModuleCodeTest(unittest.TestCase):
...
@@ -51,23 +47,19 @@ class RunModuleCodeTest(unittest.TestCase):
self
.
failUnless
(
d2
[
"initial"
]
is
initial
)
self
.
failUnless
(
d2
[
"initial"
]
is
initial
)
self
.
failUnless
(
d2
[
"result"
]
==
self
.
expected_result
)
self
.
failUnless
(
d2
[
"result"
]
==
self
.
expected_result
)
self
.
failUnless
(
d2
[
"nested"
][
"x"
]
==
1
)
self
.
failUnless
(
d2
[
"nested"
][
"x"
]
==
1
)
self
.
failUnless
(
d2
[
"__name__"
]
is
run_name
)
self
.
failUnless
(
d2
[
"__name__"
]
is
name
)
self
.
failUnless
(
d2
[
"run_name"
]
is
run_name
)
self
.
failUnless
(
d2
[
"run_name"
]
is
name
)
self
.
failUnless
(
d2
[
"__module_name__"
]
is
mod_name
)
self
.
failUnless
(
d2
[
"mod_name"
]
is
mod_name
)
self
.
failUnless
(
d2
[
"__file__"
]
is
file
)
self
.
failUnless
(
d2
[
"__file__"
]
is
file
)
self
.
failUnless
(
d2
[
"run_argv0"
]
is
file
)
self
.
failUnless
(
d2
[
"run_argv0"
]
is
file
)
self
.
failUnless
(
d2
[
"__loader__"
]
is
loader
)
self
.
failUnless
(
d2
[
"__loader__"
]
is
loader
)
self
.
failUnless
(
sys
.
argv
[
0
]
is
saved_argv0
)
self
.
failUnless
(
sys
.
argv
[
0
]
is
saved_argv0
)
self
.
failUnless
(
mod_name
not
in
sys
.
modules
)
self
.
failUnless
(
name
not
in
sys
.
modules
)
self
.
failUnless
(
run_name
not
in
sys
.
modules
)
def
test_run_module_code_defaults
(
self
):
def
test_run_module_code_defaults
(
self
):
saved_argv0
=
sys
.
argv
[
0
]
saved_argv0
=
sys
.
argv
[
0
]
d
=
_run_module_code
(
self
.
test_source
)
d
=
_run_module_code
(
self
.
test_source
)
self
.
failUnless
(
d
[
"result"
]
==
self
.
expected_result
)
self
.
failUnless
(
d
[
"result"
]
==
self
.
expected_result
)
self
.
failUnless
(
d
[
"__name__"
]
is
None
)
self
.
failUnless
(
d
[
"__name__"
]
is
None
)
self
.
failUnless
(
d
[
"__module_name__"
]
is
None
)
self
.
failUnless
(
d
[
"__file__"
]
is
None
)
self
.
failUnless
(
d
[
"__file__"
]
is
None
)
self
.
failUnless
(
d
[
"__loader__"
]
is
None
)
self
.
failUnless
(
d
[
"__loader__"
]
is
None
)
self
.
failUnless
(
d
[
"run_argv0"
]
is
saved_argv0
)
self
.
failUnless
(
d
[
"run_argv0"
]
is
saved_argv0
)
...
...
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