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
ef888024
Commit
ef888024
authored
Jun 15, 2013
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #17177: stop using imp in test_importlib
parent
53e600c2
Changes
16
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
78 additions
and
76 deletions
+78
-76
Lib/test/test_importlib/extension/test_case_sensitivity.py
Lib/test/test_importlib/extension/test_case_sensitivity.py
+5
-4
Lib/test/test_importlib/extension/test_path_hook.py
Lib/test/test_importlib/extension/test_path_hook.py
+0
-1
Lib/test/test_importlib/extension/util.py
Lib/test/test_importlib/extension/util.py
+0
-1
Lib/test/test_importlib/frozen/test_loader.py
Lib/test/test_importlib/frozen/test_loader.py
+6
-4
Lib/test/test_importlib/import_/test___loader__.py
Lib/test/test_importlib/import_/test___loader__.py
+3
-3
Lib/test/test_importlib/import_/test_api.py
Lib/test/test_importlib/import_/test_api.py
+3
-2
Lib/test/test_importlib/import_/test_fromlist.py
Lib/test/test_importlib/import_/test_fromlist.py
+0
-1
Lib/test/test_importlib/source/test_case_sensitivity.py
Lib/test/test_importlib/source/test_case_sensitivity.py
+3
-3
Lib/test/test_importlib/source/test_file_loader.py
Lib/test/test_importlib/source/test_file_loader.py
+11
-8
Lib/test/test_importlib/source/test_finder.py
Lib/test/test_importlib/source/test_finder.py
+0
-1
Lib/test/test_importlib/source/test_path_hook.py
Lib/test/test_importlib/source/test_path_hook.py
+0
-1
Lib/test/test_importlib/source/util.py
Lib/test/test_importlib/source/util.py
+0
-1
Lib/test/test_importlib/test_abc.py
Lib/test/test_importlib/test_abc.py
+25
-24
Lib/test/test_importlib/test_api.py
Lib/test/test_importlib/test_api.py
+5
-5
Lib/test/test_importlib/test_util.py
Lib/test/test_importlib/test_util.py
+15
-15
Lib/test/test_importlib/util.py
Lib/test/test_importlib/util.py
+2
-2
No files found.
Lib/test/test_importlib/extension/test_case_sensitivity.py
View file @
ef888024
import
imp
import
sys
import
sys
from
test
import
support
from
test
import
support
import
unittest
import
unittest
from
importlib
import
_bootstrap
from
importlib
import
_bootstrap
from
importlib
import
machinery
from
..
import
util
from
..
import
util
from
.
import
util
as
ext_util
from
.
import
util
as
ext_util
...
@@ -14,9 +15,9 @@ class ExtensionModuleCaseSensitivityTest(unittest.TestCase):
...
@@ -14,9 +15,9 @@ class ExtensionModuleCaseSensitivityTest(unittest.TestCase):
good_name
=
ext_util
.
NAME
good_name
=
ext_util
.
NAME
bad_name
=
good_name
.
upper
()
bad_name
=
good_name
.
upper
()
assert
good_name
!=
bad_name
assert
good_name
!=
bad_name
finder
=
_bootstrap
.
FileFinder
(
ext_util
.
PATH
,
finder
=
machinery
.
FileFinder
(
ext_util
.
PATH
,
(
_bootstrap
.
ExtensionFileLoader
,
(
machinery
.
ExtensionFileLoader
,
_bootstrap
.
EXTENSION_SUFFIXES
))
machinery
.
EXTENSION_SUFFIXES
))
return
finder
.
find_module
(
bad_name
)
return
finder
.
find_module
(
bad_name
)
def
test_case_sensitive
(
self
):
def
test_case_sensitive
(
self
):
...
...
Lib/test/test_importlib/extension/test_path_hook.py
View file @
ef888024
...
@@ -2,7 +2,6 @@ from importlib import machinery
...
@@ -2,7 +2,6 @@ from importlib import machinery
from
.
import
util
from
.
import
util
import
collections
import
collections
import
imp
import
sys
import
sys
import
unittest
import
unittest
...
...
Lib/test/test_importlib/extension/util.py
View file @
ef888024
import
imp
from
importlib
import
machinery
from
importlib
import
machinery
import
os
import
os
import
sys
import
sys
...
...
Lib/test/test_importlib/frozen/test_loader.py
View file @
ef888024
from
importlib
import
machinery
import
imp
import
unittest
from
..
import
abc
from
..
import
abc
from
..
import
util
from
..
import
util
from
importlib
import
machinery
import
unittest
from
test.support
import
captured_stdout
from
test.support
import
captured_stdout
import
types
class
LoaderTests
(
abc
.
LoaderTests
):
class
LoaderTests
(
abc
.
LoaderTests
):
...
@@ -85,7 +87,7 @@ class InspectLoaderTests(unittest.TestCase):
...
@@ -85,7 +87,7 @@ class InspectLoaderTests(unittest.TestCase):
name
=
'__hello__'
name
=
'__hello__'
with
captured_stdout
()
as
stdout
:
with
captured_stdout
()
as
stdout
:
code
=
machinery
.
FrozenImporter
.
get_code
(
name
)
code
=
machinery
.
FrozenImporter
.
get_code
(
name
)
mod
=
imp
.
new_modul
e
(
name
)
mod
=
types
.
ModuleTyp
e
(
name
)
exec
(
code
,
mod
.
__dict__
)
exec
(
code
,
mod
.
__dict__
)
self
.
assertTrue
(
hasattr
(
mod
,
'initialized'
))
self
.
assertTrue
(
hasattr
(
mod
,
'initialized'
))
self
.
assertEqual
(
stdout
.
getvalue
(),
'Hello world!
\
n
'
)
self
.
assertEqual
(
stdout
.
getvalue
(),
'Hello world!
\
n
'
)
...
...
Lib/test/test_importlib/import_/test___loader__.py
View file @
ef888024
import
imp
import
sys
import
sys
import
types
import
unittest
import
unittest
from
..
import
util
from
..
import
util
...
@@ -19,7 +19,7 @@ class LoaderMock:
...
@@ -19,7 +19,7 @@ class LoaderMock:
class
LoaderAttributeTests
(
unittest
.
TestCase
):
class
LoaderAttributeTests
(
unittest
.
TestCase
):
def
test___loader___missing
(
self
):
def
test___loader___missing
(
self
):
module
=
imp
.
new_modul
e
(
'blah'
)
module
=
types
.
ModuleTyp
e
(
'blah'
)
try
:
try
:
del
module
.
__loader__
del
module
.
__loader__
except
AttributeError
:
except
AttributeError
:
...
@@ -31,7 +31,7 @@ class LoaderAttributeTests(unittest.TestCase):
...
@@ -31,7 +31,7 @@ class LoaderAttributeTests(unittest.TestCase):
self
.
assertEqual
(
loader
,
module
.
__loader__
)
self
.
assertEqual
(
loader
,
module
.
__loader__
)
def
test___loader___is_None
(
self
):
def
test___loader___is_None
(
self
):
module
=
imp
.
new_modul
e
(
'blah'
)
module
=
types
.
ModuleTyp
e
(
'blah'
)
module
.
__loader__
=
None
module
.
__loader__
=
None
loader
=
LoaderMock
()
loader
=
LoaderMock
()
loader
.
module
=
module
loader
.
module
=
module
...
...
Lib/test/test_importlib/import_/test_api.py
View file @
ef888024
...
@@ -2,6 +2,7 @@ from .. import util as importlib_test_util
...
@@ -2,6 +2,7 @@ from .. import util as importlib_test_util
from
.
import
util
from
.
import
util
import
imp
import
imp
import
sys
import
sys
import
types
import
unittest
import
unittest
...
@@ -48,7 +49,7 @@ class APITest(unittest.TestCase):
...
@@ -48,7 +49,7 @@ class APITest(unittest.TestCase):
def
test_nonexistent_fromlist_entry
(
self
):
def
test_nonexistent_fromlist_entry
(
self
):
# If something in fromlist doesn't exist, that's okay.
# If something in fromlist doesn't exist, that's okay.
# issue15715
# issue15715
mod
=
imp
.
new_modul
e
(
'fine'
)
mod
=
types
.
ModuleTyp
e
(
'fine'
)
mod
.
__path__
=
[
'XXX'
]
mod
.
__path__
=
[
'XXX'
]
with
importlib_test_util
.
import_state
(
meta_path
=
[
BadLoaderFinder
]):
with
importlib_test_util
.
import_state
(
meta_path
=
[
BadLoaderFinder
]):
with
importlib_test_util
.
uncache
(
'fine'
):
with
importlib_test_util
.
uncache
(
'fine'
):
...
@@ -59,7 +60,7 @@ class APITest(unittest.TestCase):
...
@@ -59,7 +60,7 @@ class APITest(unittest.TestCase):
# If something in fromlist triggers an exception not related to not
# If something in fromlist triggers an exception not related to not
# existing, let that exception propagate.
# existing, let that exception propagate.
# issue15316
# issue15316
mod
=
imp
.
new_modul
e
(
'fine'
)
mod
=
types
.
ModuleTyp
e
(
'fine'
)
mod
.
__path__
=
[
'XXX'
]
mod
.
__path__
=
[
'XXX'
]
with
importlib_test_util
.
import_state
(
meta_path
=
[
BadLoaderFinder
]):
with
importlib_test_util
.
import_state
(
meta_path
=
[
BadLoaderFinder
]):
with
importlib_test_util
.
uncache
(
'fine'
):
with
importlib_test_util
.
uncache
(
'fine'
):
...
...
Lib/test/test_importlib/import_/test_fromlist.py
View file @
ef888024
"""Test that the semantics relating to the 'fromlist' argument are correct."""
"""Test that the semantics relating to the 'fromlist' argument are correct."""
from
..
import
util
from
..
import
util
from
.
import
util
as
import_util
from
.
import
util
as
import_util
import
imp
import
unittest
import
unittest
class
ReturnValue
(
unittest
.
TestCase
):
class
ReturnValue
(
unittest
.
TestCase
):
...
...
Lib/test/test_importlib/source/test_case_sensitivity.py
View file @
ef888024
"""Test case-sensitivity (PEP 235)."""
"""Test case-sensitivity (PEP 235)."""
from
importlib
import
_bootstrap
from
importlib
import
machinery
from
..
import
util
from
..
import
util
from
.
import
util
as
source_util
from
.
import
util
as
source_util
import
imp
from
importlib
import
_bootstrap
from
importlib
import
machinery
import
os
import
os
import
sys
import
sys
from
test
import
support
as
test_support
from
test
import
support
as
test_support
...
...
Lib/test/test_importlib/source/test_file_loader.py
View file @
ef888024
from
importlib
import
machinery
from
importlib
import
machinery
import
importlib
import
importlib
import
importlib.abc
import
importlib.abc
import
importlib.util
from
..
import
abc
from
..
import
abc
from
..
import
util
from
..
import
util
from
.
import
util
as
source_util
from
.
import
util
as
source_util
...
@@ -13,6 +14,7 @@ import py_compile
...
@@ -13,6 +14,7 @@ import py_compile
import
shutil
import
shutil
import
stat
import
stat
import
sys
import
sys
import
types
import
unittest
import
unittest
from
test.support
import
make_legacy_pyc
,
unload
from
test.support
import
make_legacy_pyc
,
unload
...
@@ -112,7 +114,7 @@ class SimpleTest(unittest.TestCase):
...
@@ -112,7 +114,7 @@ class SimpleTest(unittest.TestCase):
value
=
'<test>'
value
=
'<test>'
name
=
'_temp'
name
=
'_temp'
with
source_util
.
create_modules
(
name
)
as
mapping
:
with
source_util
.
create_modules
(
name
)
as
mapping
:
orig_module
=
imp
.
new_modul
e
(
name
)
orig_module
=
types
.
ModuleTyp
e
(
name
)
for
attr
in
attributes
:
for
attr
in
attributes
:
setattr
(
orig_module
,
attr
,
value
)
setattr
(
orig_module
,
attr
,
value
)
with
open
(
mapping
[
name
],
'w'
)
as
file
:
with
open
(
mapping
[
name
],
'w'
)
as
file
:
...
@@ -144,11 +146,11 @@ class SimpleTest(unittest.TestCase):
...
@@ -144,11 +146,11 @@ class SimpleTest(unittest.TestCase):
loader
=
machinery
.
SourceFileLoader
(
'_temp'
,
file_path
)
loader
=
machinery
.
SourceFileLoader
(
'_temp'
,
file_path
)
mod
=
loader
.
load_module
(
'_temp'
)
mod
=
loader
.
load_module
(
'_temp'
)
self
.
assertEqual
(
file_path
,
mod
.
__file__
)
self
.
assertEqual
(
file_path
,
mod
.
__file__
)
self
.
assertEqual
(
imp
.
cache_from_source
(
file_path
),
self
.
assertEqual
(
imp
ortlib
.
util
.
cache_from_source
(
file_path
),
mod
.
__cached__
)
mod
.
__cached__
)
finally
:
finally
:
os
.
unlink
(
file_path
)
os
.
unlink
(
file_path
)
pycache
=
os
.
path
.
dirname
(
imp
.
cache_from_source
(
file_path
))
pycache
=
os
.
path
.
dirname
(
imp
ortlib
.
util
.
cache_from_source
(
file_path
))
if
os
.
path
.
exists
(
pycache
):
if
os
.
path
.
exists
(
pycache
):
shutil
.
rmtree
(
pycache
)
shutil
.
rmtree
(
pycache
)
...
@@ -157,7 +159,7 @@ class SimpleTest(unittest.TestCase):
...
@@ -157,7 +159,7 @@ class SimpleTest(unittest.TestCase):
# truncated rather than raise an OverflowError.
# truncated rather than raise an OverflowError.
with
source_util
.
create_modules
(
'_temp'
)
as
mapping
:
with
source_util
.
create_modules
(
'_temp'
)
as
mapping
:
source
=
mapping
[
'_temp'
]
source
=
mapping
[
'_temp'
]
compiled
=
imp
.
cache_from_source
(
source
)
compiled
=
imp
ortlib
.
util
.
cache_from_source
(
source
)
with
open
(
source
,
'w'
)
as
f
:
with
open
(
source
,
'w'
)
as
f
:
f
.
write
(
"x = 5"
)
f
.
write
(
"x = 5"
)
try
:
try
:
...
@@ -194,7 +196,7 @@ class BadBytecodeTest(unittest.TestCase):
...
@@ -194,7 +196,7 @@ class BadBytecodeTest(unittest.TestCase):
pass
pass
py_compile
.
compile
(
mapping
[
name
])
py_compile
.
compile
(
mapping
[
name
])
if
not
del_source
:
if
not
del_source
:
bytecode_path
=
imp
.
cache_from_source
(
mapping
[
name
])
bytecode_path
=
imp
ortlib
.
util
.
cache_from_source
(
mapping
[
name
])
else
:
else
:
os
.
unlink
(
mapping
[
name
])
os
.
unlink
(
mapping
[
name
])
bytecode_path
=
make_legacy_pyc
(
mapping
[
name
])
bytecode_path
=
make_legacy_pyc
(
mapping
[
name
])
...
@@ -322,7 +324,8 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
...
@@ -322,7 +324,8 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
def
test
(
name
,
mapping
,
bytecode_path
):
def
test
(
name
,
mapping
,
bytecode_path
):
self
.
import_
(
mapping
[
name
],
name
)
self
.
import_
(
mapping
[
name
],
name
)
with
open
(
bytecode_path
,
'rb'
)
as
bytecode_file
:
with
open
(
bytecode_path
,
'rb'
)
as
bytecode_file
:
self
.
assertEqual
(
bytecode_file
.
read
(
4
),
imp
.
get_magic
())
self
.
assertEqual
(
bytecode_file
.
read
(
4
),
importlib
.
util
.
MAGIC_NUMBER
)
self
.
_test_bad_magic
(
test
)
self
.
_test_bad_magic
(
test
)
...
@@ -372,7 +375,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
...
@@ -372,7 +375,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
zeros
=
b'
\
x00
\
x00
\
x00
\
x00
'
zeros
=
b'
\
x00
\
x00
\
x00
\
x00
'
with
source_util
.
create_modules
(
'_temp'
)
as
mapping
:
with
source_util
.
create_modules
(
'_temp'
)
as
mapping
:
py_compile
.
compile
(
mapping
[
'_temp'
])
py_compile
.
compile
(
mapping
[
'_temp'
])
bytecode_path
=
imp
.
cache_from_source
(
mapping
[
'_temp'
])
bytecode_path
=
imp
ortlib
.
util
.
cache_from_source
(
mapping
[
'_temp'
])
with
open
(
bytecode_path
,
'r+b'
)
as
bytecode_file
:
with
open
(
bytecode_path
,
'r+b'
)
as
bytecode_file
:
bytecode_file
.
seek
(
4
)
bytecode_file
.
seek
(
4
)
bytecode_file
.
write
(
zeros
)
bytecode_file
.
write
(
zeros
)
...
@@ -390,7 +393,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
...
@@ -390,7 +393,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
with
source_util
.
create_modules
(
'_temp'
)
as
mapping
:
with
source_util
.
create_modules
(
'_temp'
)
as
mapping
:
# Create bytecode that will need to be re-created.
# Create bytecode that will need to be re-created.
py_compile
.
compile
(
mapping
[
'_temp'
])
py_compile
.
compile
(
mapping
[
'_temp'
])
bytecode_path
=
imp
.
cache_from_source
(
mapping
[
'_temp'
])
bytecode_path
=
imp
ortlib
.
util
.
cache_from_source
(
mapping
[
'_temp'
])
with
open
(
bytecode_path
,
'r+b'
)
as
bytecode_file
:
with
open
(
bytecode_path
,
'r+b'
)
as
bytecode_file
:
bytecode_file
.
seek
(
0
)
bytecode_file
.
seek
(
0
)
bytecode_file
.
write
(
b'
\
x00
\
x00
\
x00
\
x00
'
)
bytecode_file
.
write
(
b'
\
x00
\
x00
\
x00
\
x00
'
)
...
...
Lib/test/test_importlib/source/test_finder.py
View file @
ef888024
...
@@ -3,7 +3,6 @@ from . import util as source_util
...
@@ -3,7 +3,6 @@ from . import util as source_util
from
importlib
import
machinery
from
importlib
import
machinery
import
errno
import
errno
import
imp
import
os
import
os
import
py_compile
import
py_compile
import
stat
import
stat
...
...
Lib/test/test_importlib/source/test_path_hook.py
View file @
ef888024
from
.
import
util
as
source_util
from
.
import
util
as
source_util
from
importlib
import
machinery
from
importlib
import
machinery
import
imp
import
unittest
import
unittest
...
...
Lib/test/test_importlib/source/util.py
View file @
ef888024
...
@@ -2,7 +2,6 @@ from .. import util
...
@@ -2,7 +2,6 @@ from .. import util
import
contextlib
import
contextlib
import
errno
import
errno
import
functools
import
functools
import
imp
import
os
import
os
import
os.path
import
os.path
import
sys
import
sys
...
...
Lib/test/test_importlib/test_abc.py
View file @
ef888024
import
importlib
import
importlib
import
importlib.util
from
importlib
import
abc
from
importlib
import
abc
from
importlib
import
machinery
from
importlib
import
machinery
import
contextlib
import
contextlib
import
imp
import
inspect
import
inspect
import
io
import
io
import
marshal
import
marshal
import
os
import
os
import
sys
import
sys
from
test
import
support
from
test
import
support
import
types
import
unittest
import
unittest
from
unittest
import
mock
from
unittest
import
mock
...
@@ -140,7 +141,7 @@ class LoaderDefaultsTests(unittest.TestCase):
...
@@ -140,7 +141,7 @@ class LoaderDefaultsTests(unittest.TestCase):
self
.
ins
.
load_module
(
'something'
)
self
.
ins
.
load_module
(
'something'
)
def
test_module_repr
(
self
):
def
test_module_repr
(
self
):
mod
=
imp
.
new_modul
e
(
'blah'
)
mod
=
types
.
ModuleTyp
e
(
'blah'
)
with
self
.
assertRaises
(
NotImplementedError
):
with
self
.
assertRaises
(
NotImplementedError
):
self
.
ins
.
module_repr
(
mod
)
self
.
ins
.
module_repr
(
mod
)
original_repr
=
repr
(
mod
)
original_repr
=
repr
(
mod
)
...
@@ -205,7 +206,7 @@ class LoaderConcreteMethodTests(unittest.TestCase):
...
@@ -205,7 +206,7 @@ class LoaderConcreteMethodTests(unittest.TestCase):
def
test_init_module_attrs
(
self
):
def
test_init_module_attrs
(
self
):
loader
=
LoaderSubclass
()
loader
=
LoaderSubclass
()
module
=
imp
.
new_modul
e
(
'blah'
)
module
=
types
.
ModuleTyp
e
(
'blah'
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertEqual
(
module
.
__loader__
,
loader
)
self
.
assertEqual
(
module
.
__loader__
,
loader
)
...
@@ -215,7 +216,7 @@ class InspectLoaderSourceToCodeTests(unittest.TestCase):
...
@@ -215,7 +216,7 @@ class InspectLoaderSourceToCodeTests(unittest.TestCase):
def
source_to_module
(
self
,
data
,
path
=
None
):
def
source_to_module
(
self
,
data
,
path
=
None
):
"""Help with source_to_code() tests."""
"""Help with source_to_code() tests."""
module
=
imp
.
new_modul
e
(
'blah'
)
module
=
types
.
ModuleTyp
e
(
'blah'
)
loader
=
InspectLoaderSubclass
()
loader
=
InspectLoaderSubclass
()
if
path
is
None
:
if
path
is
None
:
code
=
loader
.
source_to_code
(
data
)
code
=
loader
.
source_to_code
(
data
)
...
@@ -257,7 +258,7 @@ class InspectLoaderGetCodeTests(unittest.TestCase):
...
@@ -257,7 +258,7 @@ class InspectLoaderGetCodeTests(unittest.TestCase):
def
test_get_code
(
self
):
def
test_get_code
(
self
):
# Test success.
# Test success.
module
=
imp
.
new_modul
e
(
'blah'
)
module
=
types
.
ModuleTyp
e
(
'blah'
)
with
mock
.
patch
.
object
(
InspectLoaderSubclass
,
'get_source'
)
as
mocked
:
with
mock
.
patch
.
object
(
InspectLoaderSubclass
,
'get_source'
)
as
mocked
:
mocked
.
return_value
=
'attr = 42'
mocked
.
return_value
=
'attr = 42'
loader
=
InspectLoaderSubclass
()
loader
=
InspectLoaderSubclass
()
...
@@ -289,7 +290,7 @@ class InspectLoaderInitModuleTests(unittest.TestCase):
...
@@ -289,7 +290,7 @@ class InspectLoaderInitModuleTests(unittest.TestCase):
def
init_module_attrs
(
self
,
name
):
def
init_module_attrs
(
self
,
name
):
loader
=
InspectLoaderSubclass
()
loader
=
InspectLoaderSubclass
()
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertEqual
(
module
.
__loader__
,
loader
)
self
.
assertEqual
(
module
.
__loader__
,
loader
)
return
module
return
module
...
@@ -390,7 +391,7 @@ class ExecutionLoaderGetCodeTests(unittest.TestCase):
...
@@ -390,7 +391,7 @@ class ExecutionLoaderGetCodeTests(unittest.TestCase):
loader
=
ExecutionLoaderSubclass
()
loader
=
ExecutionLoaderSubclass
()
code
=
loader
.
get_code
(
'blah'
)
code
=
loader
.
get_code
(
'blah'
)
self
.
assertEqual
(
code
.
co_filename
,
path
)
self
.
assertEqual
(
code
.
co_filename
,
path
)
module
=
imp
.
new_modul
e
(
'blah'
)
module
=
types
.
ModuleTyp
e
(
'blah'
)
exec
(
code
,
module
.
__dict__
)
exec
(
code
,
module
.
__dict__
)
self
.
assertEqual
(
module
.
attr
,
42
)
self
.
assertEqual
(
module
.
attr
,
42
)
...
@@ -420,7 +421,7 @@ class ExecutionLoaderGetCodeTests(unittest.TestCase):
...
@@ -420,7 +421,7 @@ class ExecutionLoaderGetCodeTests(unittest.TestCase):
loader
=
ExecutionLoaderSubclass
()
loader
=
ExecutionLoaderSubclass
()
code
=
loader
.
get_code
(
'blah'
)
code
=
loader
.
get_code
(
'blah'
)
self
.
assertEqual
(
code
.
co_filename
,
'<string>'
)
self
.
assertEqual
(
code
.
co_filename
,
'<string>'
)
module
=
imp
.
new_modul
e
(
'blah'
)
module
=
types
.
ModuleTyp
e
(
'blah'
)
exec
(
code
,
module
.
__dict__
)
exec
(
code
,
module
.
__dict__
)
self
.
assertEqual
(
module
.
attr
,
42
)
self
.
assertEqual
(
module
.
attr
,
42
)
...
@@ -444,7 +445,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
...
@@ -444,7 +445,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
path
=
os
.
path
.
join
(
'some'
,
'path'
,
'{}.py'
.
format
(
name
))
path
=
os
.
path
.
join
(
'some'
,
'path'
,
'{}.py'
.
format
(
name
))
with
self
.
mock_methods
(
False
,
path
):
with
self
.
mock_methods
(
False
,
path
):
loader
=
ExecutionLoaderSubclass
()
loader
=
ExecutionLoaderSubclass
()
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertIs
(
module
.
__loader__
,
loader
)
self
.
assertIs
(
module
.
__loader__
,
loader
)
self
.
assertEqual
(
module
.
__file__
,
path
)
self
.
assertEqual
(
module
.
__file__
,
path
)
...
@@ -457,7 +458,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
...
@@ -457,7 +458,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
path
=
os
.
path
.
join
(
'some'
,
'pkg'
,
'__init__.py'
)
path
=
os
.
path
.
join
(
'some'
,
'pkg'
,
'__init__.py'
)
with
self
.
mock_methods
(
True
,
path
):
with
self
.
mock_methods
(
True
,
path
):
loader
=
ExecutionLoaderSubclass
()
loader
=
ExecutionLoaderSubclass
()
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertIs
(
module
.
__loader__
,
loader
)
self
.
assertIs
(
module
.
__loader__
,
loader
)
self
.
assertEqual
(
module
.
__file__
,
path
)
self
.
assertEqual
(
module
.
__file__
,
path
)
...
@@ -471,7 +472,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
...
@@ -471,7 +472,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
path
=
os
.
path
.
join
(
'some'
,
'pkg'
,
'submodule.py'
)
path
=
os
.
path
.
join
(
'some'
,
'pkg'
,
'submodule.py'
)
with
self
.
mock_methods
(
False
,
path
):
with
self
.
mock_methods
(
False
,
path
):
loader
=
ExecutionLoaderSubclass
()
loader
=
ExecutionLoaderSubclass
()
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertEqual
(
module
.
__package__
,
'pkg'
)
self
.
assertEqual
(
module
.
__package__
,
'pkg'
)
self
.
assertEqual
(
module
.
__file__
,
path
)
self
.
assertEqual
(
module
.
__file__
,
path
)
...
@@ -484,7 +485,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
...
@@ -484,7 +485,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
with
self
.
mock_methods
(
False
,
path
)
as
mocked_methods
:
with
self
.
mock_methods
(
False
,
path
)
as
mocked_methods
:
mocked_methods
[
'get_filename'
].
side_effect
=
ImportError
mocked_methods
[
'get_filename'
].
side_effect
=
ImportError
loader
=
ExecutionLoaderSubclass
()
loader
=
ExecutionLoaderSubclass
()
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertFalse
(
hasattr
(
module
,
'__file__'
))
self
.
assertFalse
(
hasattr
(
module
,
'__file__'
))
...
@@ -515,9 +516,9 @@ class SourceLoaderMock(SourceOnlyLoaderMock):
...
@@ -515,9 +516,9 @@ class SourceLoaderMock(SourceOnlyLoaderMock):
source_mtime
=
1
source_mtime
=
1
def
__init__
(
self
,
path
,
magic
=
imp
.
get_magic
()
):
def
__init__
(
self
,
path
,
magic
=
imp
ortlib
.
util
.
MAGIC_NUMBER
):
super
().
__init__
(
path
)
super
().
__init__
(
path
)
self
.
bytecode_path
=
imp
.
cache_from_source
(
self
.
path
)
self
.
bytecode_path
=
imp
ortlib
.
util
.
cache_from_source
(
self
.
path
)
self
.
source_size
=
len
(
self
.
source
)
self
.
source_size
=
len
(
self
.
source
)
data
=
bytearray
(
magic
)
data
=
bytearray
(
magic
)
data
.
extend
(
importlib
.
_w_long
(
self
.
source_mtime
))
data
.
extend
(
importlib
.
_w_long
(
self
.
source_mtime
))
...
@@ -557,7 +558,7 @@ class SourceLoaderTestHarness(unittest.TestCase):
...
@@ -557,7 +558,7 @@ class SourceLoaderTestHarness(unittest.TestCase):
module_name
=
'mod'
module_name
=
'mod'
self
.
path
=
os
.
path
.
join
(
self
.
package
,
'.'
.
join
([
'mod'
,
'py'
]))
self
.
path
=
os
.
path
.
join
(
self
.
package
,
'.'
.
join
([
'mod'
,
'py'
]))
self
.
name
=
'.'
.
join
([
self
.
package
,
module_name
])
self
.
name
=
'.'
.
join
([
self
.
package
,
module_name
])
self
.
cached
=
imp
.
cache_from_source
(
self
.
path
)
self
.
cached
=
imp
ortlib
.
util
.
cache_from_source
(
self
.
path
)
self
.
loader
=
self
.
loader_mock
(
self
.
path
,
**
kwargs
)
self
.
loader
=
self
.
loader_mock
(
self
.
path
,
**
kwargs
)
def
verify_module
(
self
,
module
):
def
verify_module
(
self
,
module
):
...
@@ -574,7 +575,7 @@ class SourceLoaderTestHarness(unittest.TestCase):
...
@@ -574,7 +575,7 @@ class SourceLoaderTestHarness(unittest.TestCase):
self
.
assertEqual
(
values
[
4
],
repr
(
self
.
loader
))
self
.
assertEqual
(
values
[
4
],
repr
(
self
.
loader
))
def
verify_code
(
self
,
code_object
):
def
verify_code
(
self
,
code_object
):
module
=
imp
.
new_modul
e
(
self
.
name
)
module
=
types
.
ModuleTyp
e
(
self
.
name
)
module
.
__file__
=
self
.
path
module
.
__file__
=
self
.
path
module
.
__cached__
=
self
.
cached
module
.
__cached__
=
self
.
cached
module
.
__package__
=
self
.
package
module
.
__package__
=
self
.
package
...
@@ -673,7 +674,7 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
...
@@ -673,7 +674,7 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
super
().
verify_code
(
code_object
)
super
().
verify_code
(
code_object
)
if
bytecode_written
:
if
bytecode_written
:
self
.
assertIn
(
self
.
cached
,
self
.
loader
.
written
)
self
.
assertIn
(
self
.
cached
,
self
.
loader
.
written
)
data
=
bytearray
(
imp
.
get_magic
()
)
data
=
bytearray
(
imp
ortlib
.
util
.
MAGIC_NUMBER
)
data
.
extend
(
importlib
.
_w_long
(
self
.
loader
.
source_mtime
))
data
.
extend
(
importlib
.
_w_long
(
self
.
loader
.
source_mtime
))
data
.
extend
(
importlib
.
_w_long
(
self
.
loader
.
source_size
))
data
.
extend
(
importlib
.
_w_long
(
self
.
loader
.
source_size
))
data
.
extend
(
marshal
.
dumps
(
code_object
))
data
.
extend
(
marshal
.
dumps
(
code_object
))
...
@@ -689,7 +690,7 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
...
@@ -689,7 +690,7 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
self
.
loader
.
bytecode_path
=
"<does not exist>"
self
.
loader
.
bytecode_path
=
"<does not exist>"
# Sanity check
# Sanity check
with
self
.
assertRaises
(
OSError
):
with
self
.
assertRaises
(
OSError
):
bytecode_path
=
imp
.
cache_from_source
(
self
.
path
)
bytecode_path
=
imp
ortlib
.
util
.
cache_from_source
(
self
.
path
)
self
.
loader
.
get_data
(
bytecode_path
)
self
.
loader
.
get_data
(
bytecode_path
)
code_object
=
self
.
loader
.
get_code
(
self
.
name
)
code_object
=
self
.
loader
.
get_code
(
self
.
name
)
self
.
verify_code
(
code_object
,
bytecode_written
=
True
)
self
.
verify_code
(
code_object
,
bytecode_written
=
True
)
...
@@ -787,26 +788,26 @@ class SourceLoaderInitModuleAttrTests(unittest.TestCase):
...
@@ -787,26 +788,26 @@ class SourceLoaderInitModuleAttrTests(unittest.TestCase):
"""Tests for importlib.abc.SourceLoader.init_module_attrs()."""
"""Tests for importlib.abc.SourceLoader.init_module_attrs()."""
def
test_init_module_attrs
(
self
):
def
test_init_module_attrs
(
self
):
# If __file__ set, __cached__ == imp.cached_from_source(__file__).
# If __file__ set, __cached__ == imp
ortlib.util
.cached_from_source(__file__).
name
=
'blah'
name
=
'blah'
path
=
'blah.py'
path
=
'blah.py'
loader
=
SourceOnlyLoaderMock
(
path
)
loader
=
SourceOnlyLoaderMock
(
path
)
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertEqual
(
module
.
__loader__
,
loader
)
self
.
assertEqual
(
module
.
__loader__
,
loader
)
self
.
assertEqual
(
module
.
__package__
,
''
)
self
.
assertEqual
(
module
.
__package__
,
''
)
self
.
assertEqual
(
module
.
__file__
,
path
)
self
.
assertEqual
(
module
.
__file__
,
path
)
self
.
assertEqual
(
module
.
__cached__
,
imp
.
cache_from_source
(
path
))
self
.
assertEqual
(
module
.
__cached__
,
imp
ortlib
.
util
.
cache_from_source
(
path
))
@
mock
.
patch
(
'importlib._bootstrap.cache_from_source'
)
@
mock
.
patch
(
'importlib._bootstrap.cache_from_source'
)
def
test_cache_from_source_NotImplementedError
(
self
,
mock_cache_from_source
):
def
test_cache_from_source_NotImplementedError
(
self
,
mock_cache_from_source
):
# If imp.cache_from_source() raises NotImplementedError don't set
# If imp
ortlib.util
.cache_from_source() raises NotImplementedError don't set
# __cached__.
# __cached__.
mock_cache_from_source
.
side_effect
=
NotImplementedError
mock_cache_from_source
.
side_effect
=
NotImplementedError
name
=
'blah'
name
=
'blah'
path
=
'blah.py'
path
=
'blah.py'
loader
=
SourceOnlyLoaderMock
(
path
)
loader
=
SourceOnlyLoaderMock
(
path
)
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertEqual
(
module
.
__file__
,
path
)
self
.
assertEqual
(
module
.
__file__
,
path
)
self
.
assertFalse
(
hasattr
(
module
,
'__cached__'
))
self
.
assertFalse
(
hasattr
(
module
,
'__cached__'
))
...
@@ -817,7 +818,7 @@ class SourceLoaderInitModuleAttrTests(unittest.TestCase):
...
@@ -817,7 +818,7 @@ class SourceLoaderInitModuleAttrTests(unittest.TestCase):
mocked
.
side_effect
=
ImportError
mocked
.
side_effect
=
ImportError
name
=
'blah'
name
=
'blah'
loader
=
SourceOnlyLoaderMock
(
'blah.py'
)
loader
=
SourceOnlyLoaderMock
(
'blah.py'
)
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
.
init_module_attrs
(
module
)
loader
.
init_module_attrs
(
module
)
self
.
assertFalse
(
hasattr
(
module
,
'__file__'
))
self
.
assertFalse
(
hasattr
(
module
,
'__file__'
))
self
.
assertFalse
(
hasattr
(
module
,
'__cached__'
))
self
.
assertFalse
(
hasattr
(
module
,
'__cached__'
))
...
...
Lib/test/test_importlib/test_api.py
View file @
ef888024
from
.
import
util
from
.
import
util
import
imp
import
importlib
import
importlib
from
importlib
import
_bootstrap
from
importlib
import
_bootstrap
from
importlib
import
machinery
from
importlib
import
machinery
...
@@ -99,7 +99,7 @@ class FindLoaderTests(unittest.TestCase):
...
@@ -99,7 +99,7 @@ class FindLoaderTests(unittest.TestCase):
# If a module with __loader__ is in sys.modules, then return it.
# If a module with __loader__ is in sys.modules, then return it.
name
=
'some_mod'
name
=
'some_mod'
with
util
.
uncache
(
name
):
with
util
.
uncache
(
name
):
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
loader
=
'a loader!'
loader
=
'a loader!'
module
.
__loader__
=
loader
module
.
__loader__
=
loader
sys
.
modules
[
name
]
=
module
sys
.
modules
[
name
]
=
module
...
@@ -110,7 +110,7 @@ class FindLoaderTests(unittest.TestCase):
...
@@ -110,7 +110,7 @@ class FindLoaderTests(unittest.TestCase):
# If sys.modules[name].__loader__ is None, raise ValueError.
# If sys.modules[name].__loader__ is None, raise ValueError.
name
=
'some_mod'
name
=
'some_mod'
with
util
.
uncache
(
name
):
with
util
.
uncache
(
name
):
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
module
.
__loader__
=
None
module
.
__loader__
=
None
sys
.
modules
[
name
]
=
module
sys
.
modules
[
name
]
=
module
with
self
.
assertRaises
(
ValueError
):
with
self
.
assertRaises
(
ValueError
):
...
@@ -121,7 +121,7 @@ class FindLoaderTests(unittest.TestCase):
...
@@ -121,7 +121,7 @@ class FindLoaderTests(unittest.TestCase):
# Issue #17099
# Issue #17099
name
=
'some_mod'
name
=
'some_mod'
with
util
.
uncache
(
name
):
with
util
.
uncache
(
name
):
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
try
:
try
:
del
module
.
__loader__
del
module
.
__loader__
except
AttributeError
:
except
AttributeError
:
...
@@ -189,7 +189,7 @@ class InvalidateCacheTests(unittest.TestCase):
...
@@ -189,7 +189,7 @@ class InvalidateCacheTests(unittest.TestCase):
def
test_method_lacking
(
self
):
def
test_method_lacking
(
self
):
# There should be no issues if the method is not defined.
# There should be no issues if the method is not defined.
key
=
'gobbledeegook'
key
=
'gobbledeegook'
sys
.
path_importer_cache
[
key
]
=
imp
.
NullImporter
(
'abc'
)
sys
.
path_importer_cache
[
key
]
=
None
self
.
addCleanup
(
lambda
:
sys
.
path_importer_cache
.
__delitem__
(
key
))
self
.
addCleanup
(
lambda
:
sys
.
path_importer_cache
.
__delitem__
(
key
))
importlib
.
invalidate_caches
()
# Shouldn't trigger an exception.
importlib
.
invalidate_caches
()
# Shouldn't trigger an exception.
...
...
Lib/test/test_importlib/test_util.py
View file @
ef888024
from
importlib
import
util
from
importlib
import
util
from
.
import
util
as
test_util
from
.
import
util
as
test_util
import
imp
import
os
import
os
import
sys
import
sys
from
test
import
support
from
test
import
support
...
@@ -40,14 +40,14 @@ class ModuleToLoadTests(unittest.TestCase):
...
@@ -40,14 +40,14 @@ class ModuleToLoadTests(unittest.TestCase):
def
test_reload
(
self
):
def
test_reload
(
self
):
# Test that the same module is in sys.modules.
# Test that the same module is in sys.modules.
created_module
=
imp
.
new_modul
e
(
self
.
module_name
)
created_module
=
types
.
ModuleTyp
e
(
self
.
module_name
)
sys
.
modules
[
self
.
module_name
]
=
created_module
sys
.
modules
[
self
.
module_name
]
=
created_module
with
util
.
module_to_load
(
self
.
module_name
)
as
module
:
with
util
.
module_to_load
(
self
.
module_name
)
as
module
:
self
.
assertIs
(
module
,
created_module
)
self
.
assertIs
(
module
,
created_module
)
def
test_reload_failed
(
self
):
def
test_reload_failed
(
self
):
# Test that the module was left in sys.modules.
# Test that the module was left in sys.modules.
created_module
=
imp
.
new_modul
e
(
self
.
module_name
)
created_module
=
types
.
ModuleTyp
e
(
self
.
module_name
)
sys
.
modules
[
self
.
module_name
]
=
created_module
sys
.
modules
[
self
.
module_name
]
=
created_module
try
:
try
:
with
util
.
module_to_load
(
self
.
module_name
)
as
module
:
with
util
.
module_to_load
(
self
.
module_name
)
as
module
:
...
@@ -60,7 +60,7 @@ class ModuleToLoadTests(unittest.TestCase):
...
@@ -60,7 +60,7 @@ class ModuleToLoadTests(unittest.TestCase):
def
test_reset_name
(
self
):
def
test_reset_name
(
self
):
# If reset_name is true then module.__name__ = name, else leave it be.
# If reset_name is true then module.__name__ = name, else leave it be.
odd_name
=
'not your typical name'
odd_name
=
'not your typical name'
created_module
=
imp
.
new_modul
e
(
self
.
module_name
)
created_module
=
types
.
ModuleTyp
e
(
self
.
module_name
)
created_module
.
__name__
=
odd_name
created_module
.
__name__
=
odd_name
sys
.
modules
[
self
.
module_name
]
=
created_module
sys
.
modules
[
self
.
module_name
]
=
created_module
with
util
.
module_to_load
(
self
.
module_name
)
as
module
:
with
util
.
module_to_load
(
self
.
module_name
)
as
module
:
...
@@ -119,7 +119,7 @@ class ModuleForLoaderTests(unittest.TestCase):
...
@@ -119,7 +119,7 @@ class ModuleForLoaderTests(unittest.TestCase):
def
load_module
(
self
,
module
):
def
load_module
(
self
,
module
):
return
module
return
module
name
=
'a.b.c'
name
=
'a.b.c'
module
=
imp
.
new_modul
e
(
'a.b.c'
)
module
=
types
.
ModuleTyp
e
(
'a.b.c'
)
module
.
__loader__
=
42
module
.
__loader__
=
42
module
.
__package__
=
42
module
.
__package__
=
42
with
test_util
.
uncache
(
name
):
with
test_util
.
uncache
(
name
):
...
@@ -141,7 +141,7 @@ class ModuleForLoaderTests(unittest.TestCase):
...
@@ -141,7 +141,7 @@ class ModuleForLoaderTests(unittest.TestCase):
def
test_reload_failure
(
self
):
def
test_reload_failure
(
self
):
# Test that a failure on reload leaves the module in-place.
# Test that a failure on reload leaves the module in-place.
name
=
'a.b.c'
name
=
'a.b.c'
module
=
imp
.
new_modul
e
(
name
)
module
=
types
.
ModuleTyp
e
(
name
)
with
test_util
.
uncache
(
name
):
with
test_util
.
uncache
(
name
):
sys
.
modules
[
name
]
=
module
sys
.
modules
[
name
]
=
module
self
.
raise_exception
(
name
)
self
.
raise_exception
(
name
)
...
@@ -212,26 +212,26 @@ class SetPackageTests(unittest.TestCase):
...
@@ -212,26 +212,26 @@ class SetPackageTests(unittest.TestCase):
def
test_top_level
(
self
):
def
test_top_level
(
self
):
# __package__ should be set to the empty string if a top-level module.
# __package__ should be set to the empty string if a top-level module.
# Implicitly tests when package is set to None.
# Implicitly tests when package is set to None.
module
=
imp
.
new_modul
e
(
'module'
)
module
=
types
.
ModuleTyp
e
(
'module'
)
module
.
__package__
=
None
module
.
__package__
=
None
self
.
verify
(
module
,
''
)
self
.
verify
(
module
,
''
)
def
test_package
(
self
):
def
test_package
(
self
):
# Test setting __package__ for a package.
# Test setting __package__ for a package.
module
=
imp
.
new_modul
e
(
'pkg'
)
module
=
types
.
ModuleTyp
e
(
'pkg'
)
module
.
__path__
=
[
'<path>'
]
module
.
__path__
=
[
'<path>'
]
module
.
__package__
=
None
module
.
__package__
=
None
self
.
verify
(
module
,
'pkg'
)
self
.
verify
(
module
,
'pkg'
)
def
test_submodule
(
self
):
def
test_submodule
(
self
):
# Test __package__ for a module in a package.
# Test __package__ for a module in a package.
module
=
imp
.
new_modul
e
(
'pkg.mod'
)
module
=
types
.
ModuleTyp
e
(
'pkg.mod'
)
module
.
__package__
=
None
module
.
__package__
=
None
self
.
verify
(
module
,
'pkg'
)
self
.
verify
(
module
,
'pkg'
)
def
test_setting_if_missing
(
self
):
def
test_setting_if_missing
(
self
):
# __package__ should be set if it is missing.
# __package__ should be set if it is missing.
module
=
imp
.
new_modul
e
(
'mod'
)
module
=
types
.
ModuleTyp
e
(
'mod'
)
if
hasattr
(
module
,
'__package__'
):
if
hasattr
(
module
,
'__package__'
):
delattr
(
module
,
'__package__'
)
delattr
(
module
,
'__package__'
)
self
.
verify
(
module
,
''
)
self
.
verify
(
module
,
''
)
...
@@ -239,7 +239,7 @@ class SetPackageTests(unittest.TestCase):
...
@@ -239,7 +239,7 @@ class SetPackageTests(unittest.TestCase):
def
test_leaving_alone
(
self
):
def
test_leaving_alone
(
self
):
# If __package__ is set and not None then leave it alone.
# If __package__ is set and not None then leave it alone.
for
value
in
(
True
,
False
):
for
value
in
(
True
,
False
):
module
=
imp
.
new_modul
e
(
'mod'
)
module
=
types
.
ModuleTyp
e
(
'mod'
)
module
.
__package__
=
value
module
.
__package__
=
value
self
.
verify
(
module
,
value
)
self
.
verify
(
module
,
value
)
...
@@ -261,7 +261,7 @@ class SetLoaderTests(unittest.TestCase):
...
@@ -261,7 +261,7 @@ class SetLoaderTests(unittest.TestCase):
def
test_no_attribute
(
self
):
def
test_no_attribute
(
self
):
loader
=
self
.
DummyLoader
()
loader
=
self
.
DummyLoader
()
loader
.
module
=
imp
.
new_modul
e
(
'blah'
)
loader
.
module
=
types
.
ModuleTyp
e
(
'blah'
)
try
:
try
:
del
loader
.
module
.
__loader__
del
loader
.
module
.
__loader__
except
AttributeError
:
except
AttributeError
:
...
@@ -270,13 +270,13 @@ class SetLoaderTests(unittest.TestCase):
...
@@ -270,13 +270,13 @@ class SetLoaderTests(unittest.TestCase):
def
test_attribute_is_None
(
self
):
def
test_attribute_is_None
(
self
):
loader
=
self
.
DummyLoader
()
loader
=
self
.
DummyLoader
()
loader
.
module
=
imp
.
new_modul
e
(
'blah'
)
loader
.
module
=
types
.
ModuleTyp
e
(
'blah'
)
loader
.
module
.
__loader__
=
None
loader
.
module
.
__loader__
=
None
self
.
assertEqual
(
loader
,
loader
.
load_module
(
'blah'
).
__loader__
)
self
.
assertEqual
(
loader
,
loader
.
load_module
(
'blah'
).
__loader__
)
def
test_not_reset
(
self
):
def
test_not_reset
(
self
):
loader
=
self
.
DummyLoader
()
loader
=
self
.
DummyLoader
()
loader
.
module
=
imp
.
new_modul
e
(
'blah'
)
loader
.
module
=
types
.
ModuleTyp
e
(
'blah'
)
loader
.
module
.
__loader__
=
42
loader
.
module
.
__loader__
=
42
self
.
assertEqual
(
42
,
loader
.
load_module
(
'blah'
).
__loader__
)
self
.
assertEqual
(
42
,
loader
.
load_module
(
'blah'
).
__loader__
)
...
@@ -331,7 +331,7 @@ class PEP3147Tests(unittest.TestCase):
...
@@ -331,7 +331,7 @@ class PEP3147Tests(unittest.TestCase):
"""
"""
tag
=
imp
.
get_tag
()
tag
=
sys
.
implementation
.
cache_tag
@
unittest
.
skipUnless
(
sys
.
implementation
.
cache_tag
is
not
None
,
@
unittest
.
skipUnless
(
sys
.
implementation
.
cache_tag
is
not
None
,
'requires sys.implementation.cache_tag not be None'
)
'requires sys.implementation.cache_tag not be None'
)
...
...
Lib/test/test_importlib/util.py
View file @
ef888024
from
contextlib
import
contextmanager
from
contextlib
import
contextmanager
import
imp
import
os.path
import
os.path
from
test
import
support
from
test
import
support
import
unittest
import
unittest
import
sys
import
sys
import
types
CASE_INSENSITIVE_FS
=
True
CASE_INSENSITIVE_FS
=
True
...
@@ -98,7 +98,7 @@ class mock_modules:
...
@@ -98,7 +98,7 @@ class mock_modules:
package
=
name
.
rsplit
(
'.'
,
1
)[
0
]
package
=
name
.
rsplit
(
'.'
,
1
)[
0
]
else
:
else
:
package
=
import_name
package
=
import_name
module
=
imp
.
new_modul
e
(
import_name
)
module
=
types
.
ModuleTyp
e
(
import_name
)
module
.
__loader__
=
self
module
.
__loader__
=
self
module
.
__file__
=
'<mock __file__>'
module
.
__file__
=
'<mock __file__>'
module
.
__package__
=
package
module
.
__package__
=
package
...
...
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