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
f17a2e4f
Commit
f17a2e4f
authored
Jul 22, 2007
by
Nick Coghlan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add explicit relative import tests for runpy.run_module
parent
7f4f4125
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
5 deletions
+66
-5
Lib/test/test_runpy.py
Lib/test/test_runpy.py
+66
-5
No files found.
Lib/test/test_runpy.py
View file @
f17a2e4f
...
...
@@ -87,9 +87,15 @@ class RunModuleTest(unittest.TestCase):
def
test_library_module
(
self
):
run_module
(
"runpy"
)
def
_add_pkg_dir
(
self
,
pkg_dir
):
os
.
mkdir
(
pkg_dir
)
pkg_fname
=
os
.
path
.
join
(
pkg_dir
,
"__init__"
+
os
.
extsep
+
"py"
)
pkg_file
=
open
(
pkg_fname
,
"w"
)
pkg_file
.
close
()
return
pkg_fname
def
_make_pkg
(
self
,
source
,
depth
):
pkg_name
=
"__runpy_pkg__"
init_fname
=
"__init__"
+
os
.
extsep
+
"py"
test_fname
=
"runpy_test"
+
os
.
extsep
+
"py"
pkg_dir
=
sub_dir
=
tempfile
.
mkdtemp
()
if
verbose
:
print
" Package tree in:"
,
sub_dir
...
...
@@ -97,11 +103,8 @@ class RunModuleTest(unittest.TestCase):
if
verbose
:
print
" Updated sys.path:"
,
sys
.
path
[
0
]
for
i
in
range
(
depth
):
sub_dir
=
os
.
path
.
join
(
sub_dir
,
pkg_name
)
os
.
mk
dir
(
sub_dir
)
pkg_fname
=
self
.
_add_pkg_
dir
(
sub_dir
)
if
verbose
:
print
" Next level in:"
,
sub_dir
pkg_fname
=
os
.
path
.
join
(
sub_dir
,
init_fname
)
pkg_file
=
open
(
pkg_fname
,
"w"
)
pkg_file
.
close
()
if
verbose
:
print
" Created:"
,
pkg_fname
mod_fname
=
os
.
path
.
join
(
sub_dir
,
test_fname
)
mod_file
=
open
(
mod_fname
,
"w"
)
...
...
@@ -146,23 +149,81 @@ class RunModuleTest(unittest.TestCase):
try
:
if
verbose
:
print
"Running from source:"
,
mod_name
d1
=
run_module
(
mod_name
)
# Read from source
self
.
failUnless
(
"x"
in
d1
)
self
.
failUnless
(
d1
[
"x"
]
==
1
)
del
d1
# Ensure __loader__ entry doesn't keep file open
__import__
(
mod_name
)
os
.
remove
(
mod_fname
)
if
verbose
:
print
"Running from compiled:"
,
mod_name
d2
=
run_module
(
mod_name
)
# Read from bytecode
self
.
failUnless
(
"x"
in
d2
)
self
.
failUnless
(
d2
[
"x"
]
==
1
)
del
d2
# Ensure __loader__ entry doesn't keep file open
finally
:
self
.
_del_pkg
(
pkg_dir
,
depth
,
mod_name
)
if
verbose
:
print
"Module executed successfully"
def
_add_relative_modules
(
self
,
base_dir
,
depth
):
if
depth
<=
1
:
raise
ValueError
(
"Relative module test needs depth > 1"
)
pkg_name
=
"__runpy_pkg__"
module_dir
=
base_dir
for
i
in
range
(
depth
):
parent_dir
=
module_dir
module_dir
=
os
.
path
.
join
(
module_dir
,
pkg_name
)
# Add sibling module
sibling_fname
=
os
.
path
.
join
(
module_dir
,
"sibling"
+
os
.
extsep
+
"py"
)
sibling_file
=
open
(
sibling_fname
,
"w"
)
sibling_file
.
close
()
if
verbose
:
print
" Added sibling module:"
,
sibling_fname
# Add nephew module
uncle_dir
=
os
.
path
.
join
(
parent_dir
,
"uncle"
)
self
.
_add_pkg_dir
(
uncle_dir
)
if
verbose
:
print
" Added uncle package:"
,
uncle_dir
cousin_dir
=
os
.
path
.
join
(
uncle_dir
,
"cousin"
)
self
.
_add_pkg_dir
(
cousin_dir
)
if
verbose
:
print
" Added cousin package:"
,
cousin_dir
nephew_fname
=
os
.
path
.
join
(
cousin_dir
,
"nephew"
+
os
.
extsep
+
"py"
)
nephew_file
=
open
(
nephew_fname
,
"w"
)
nephew_file
.
close
()
if
verbose
:
print
" Added nephew module:"
,
nephew_fname
def
_check_relative_imports
(
self
,
depth
,
run_name
=
None
):
contents
=
"""
\
from __future__ import absolute_import
from . import sibling
from ..uncle.cousin import nephew
"""
pkg_dir
,
mod_fname
,
mod_name
=
(
self
.
_make_pkg
(
contents
,
depth
))
try
:
self
.
_add_relative_modules
(
pkg_dir
,
depth
)
if
verbose
:
print
"Running from source:"
,
mod_name
d1
=
run_module
(
mod_name
)
# Read from source
self
.
failUnless
(
"sibling"
in
d1
)
self
.
failUnless
(
"nephew"
in
d1
)
del
d1
# Ensure __loader__ entry doesn't keep file open
__import__
(
mod_name
)
os
.
remove
(
mod_fname
)
if
verbose
:
print
"Running from compiled:"
,
mod_name
d2
=
run_module
(
mod_name
)
# Read from bytecode
self
.
failUnless
(
"sibling"
in
d2
)
self
.
failUnless
(
"nephew"
in
d2
)
del
d2
# Ensure __loader__ entry doesn't keep file open
finally
:
self
.
_del_pkg
(
pkg_dir
,
depth
,
mod_name
)
if
verbose
:
print
"Module executed successfully"
def
test_run_module
(
self
):
for
depth
in
range
(
4
):
if
verbose
:
print
"Testing package depth:"
,
depth
self
.
_check_module
(
depth
)
def
test_explicit_relative_import
(
self
):
for
depth
in
range
(
2
,
5
):
if
verbose
:
print
"Testing relative imports at depth:"
,
depth
self
.
_check_relative_imports
(
depth
)
def
test_main
():
run_unittest
(
RunModuleCodeTest
)
...
...
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