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
3497c0bf
Commit
3497c0bf
authored
May 16, 2014
by
Eric Snow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21503: Use test_both() consistently in test_importlib.
parent
d50cee20
Changes
30
Hide whitespace changes
Inline
Side-by-side
Showing
30 changed files
with
527 additions
and
429 deletions
+527
-429
Lib/test/test_importlib/builtin/test_finder.py
Lib/test/test_importlib/builtin/test_finder.py
+9
-5
Lib/test/test_importlib/builtin/test_loader.py
Lib/test/test_importlib/builtin/test_loader.py
+8
-6
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_finder.py
Lib/test/test_importlib/extension/test_finder.py
+4
-2
Lib/test/test_importlib/extension/test_loader.py
Lib/test/test_importlib/extension/test_loader.py
+3
-2
Lib/test/test_importlib/extension/test_path_hook.py
Lib/test/test_importlib/extension/test_path_hook.py
+4
-2
Lib/test/test_importlib/frozen/test_finder.py
Lib/test/test_importlib/frozen/test_finder.py
+8
-4
Lib/test/test_importlib/frozen/test_loader.py
Lib/test/test_importlib/frozen/test_loader.py
+11
-6
Lib/test/test_importlib/import_/test___loader__.py
Lib/test/test_importlib/import_/test___loader__.py
+7
-4
Lib/test/test_importlib/import_/test___package__.py
Lib/test/test_importlib/import_/test___package__.py
+11
-5
Lib/test/test_importlib/import_/test_api.py
Lib/test/test_importlib/import_/test_api.py
+8
-4
Lib/test/test_importlib/import_/test_caching.py
Lib/test/test_importlib/import_/test_caching.py
+5
-3
Lib/test/test_importlib/import_/test_fromlist.py
Lib/test/test_importlib/import_/test_fromlist.py
+8
-4
Lib/test/test_importlib/import_/test_meta_path.py
Lib/test/test_importlib/import_/test_meta_path.py
+14
-6
Lib/test/test_importlib/import_/test_packages.py
Lib/test/test_importlib/import_/test_packages.py
+4
-2
Lib/test/test_importlib/import_/test_path.py
Lib/test/test_importlib/import_/test_path.py
+8
-4
Lib/test/test_importlib/import_/test_relative_imports.py
Lib/test/test_importlib/import_/test_relative_imports.py
+4
-2
Lib/test/test_importlib/source/test_case_sensitivity.py
Lib/test/test_importlib/source/test_case_sensitivity.py
+12
-4
Lib/test/test_importlib/source/test_file_loader.py
Lib/test/test_importlib/source/test_file_loader.py
+35
-15
Lib/test/test_importlib/source/test_finder.py
Lib/test/test_importlib/source/test_finder.py
+11
-6
Lib/test/test_importlib/source/test_path_hook.py
Lib/test/test_importlib/source/test_path_hook.py
+4
-1
Lib/test/test_importlib/source/test_source_encoding.py
Lib/test/test_importlib/source/test_source_encoding.py
+20
-8
Lib/test/test_importlib/test_abc.py
Lib/test/test_importlib/test_abc.py
+142
-150
Lib/test/test_importlib/test_api.py
Lib/test/test_importlib/test_api.py
+46
-53
Lib/test/test_importlib/test_locks.py
Lib/test/test_importlib/test_locks.py
+26
-21
Lib/test/test_importlib/test_spec.py
Lib/test/test_importlib/test_spec.py
+29
-43
Lib/test/test_importlib/test_util.py
Lib/test/test_importlib/test_util.py
+44
-41
Lib/test/test_importlib/test_windows.py
Lib/test/test_importlib/test_windows.py
+5
-10
Lib/test/test_importlib/util.py
Lib/test/test_importlib/util.py
+30
-12
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/test/test_importlib/builtin/test_finder.py
View file @
3497c0bf
from
..
import
abc
from
..
import
util
frozen_machinery
,
source_
machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
import
sys
import
unittest
...
...
@@ -44,8 +44,10 @@ class FindSpecTests(abc.FinderTests):
[
'pkg'
])
self
.
assertIsNone
(
spec
)
Frozen_FindSpecTests
,
Source_FindSpecTests
=
util
.
test_both
(
FindSpecTests
,
machinery
=
[
frozen_machinery
,
source_machinery
])
(
Frozen_FindSpecTests
,
Source_FindSpecTests
)
=
util
.
test_both
(
FindSpecTests
,
machinery
=
machinery
)
@
unittest
.
skipIf
(
util
.
BUILTINS
.
good_name
is
None
,
'no reasonable builtin module'
)
...
...
@@ -78,8 +80,10 @@ class FinderTests(abc.FinderTests):
[
'pkg'
])
self
.
assertIsNone
(
loader
)
Frozen_FinderTests
,
Source_FinderTests
=
util
.
test_both
(
FinderTests
,
machinery
=
[
frozen_machinery
,
source_machinery
])
(
Frozen_FinderTests
,
Source_FinderTests
)
=
util
.
test_both
(
FinderTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/builtin/test_loader.py
View file @
3497c0bf
from
..
import
abc
from
..
import
util
frozen_machinery
,
source_
machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
import
sys
import
types
...
...
@@ -65,8 +65,9 @@ class LoaderTests(abc.LoaderTests):
self
.
assertEqual
(
cm
.
exception
.
name
,
module_name
)
Frozen_LoaderTests
,
Source_LoaderTests
=
util
.
test_both
(
LoaderTests
,
machinery
=
[
frozen_machinery
,
source_machinery
])
(
Frozen_LoaderTests
,
Source_LoaderTests
)
=
util
.
test_both
(
LoaderTests
,
machinery
=
machinery
)
@
unittest
.
skipIf
(
util
.
BUILTINS
.
good_name
is
None
,
'no reasonable builtin module'
)
...
...
@@ -98,9 +99,10 @@ class InspectLoaderTests:
method
(
util
.
BUILTINS
.
bad_name
)
self
.
assertRaises
(
util
.
BUILTINS
.
bad_name
)
Frozen_InspectLoaderTests
,
Source_InspectLoaderTests
=
util
.
test_both
(
InspectLoaderTests
,
machinery
=
[
frozen_machinery
,
source_machinery
])
(
Frozen_InspectLoaderTests
,
Source_InspectLoaderTests
)
=
util
.
test_both
(
InspectLoaderTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/extension/test_case_sensitivity.py
View file @
3497c0bf
...
...
@@ -5,7 +5,7 @@ import unittest
from
..
import
util
frozen_machinery
,
source_
machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
# XXX find_spec tests
...
...
@@ -41,9 +41,10 @@ class ExtensionModuleCaseSensitivityTest:
loader
=
self
.
find_module
()
self
.
assertTrue
(
hasattr
(
loader
,
'load_module'
))
Frozen_ExtensionCaseSensitivity
,
Source_ExtensionCaseSensitivity
=
util
.
test_both
(
ExtensionModuleCaseSensitivityTest
,
machinery
=
[
frozen_machinery
,
source_machinery
])
(
Frozen_ExtensionCaseSensitivity
,
Source_ExtensionCaseSensitivity
)
=
util
.
test_both
(
ExtensionModuleCaseSensitivityTest
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/extension/test_finder.py
View file @
3497c0bf
...
...
@@ -35,8 +35,10 @@ class FinderTests(abc.FinderTests):
def
test_failure
(
self
):
self
.
assertIsNone
(
self
.
find_module
(
'asdfjkl;'
))
Frozen_FinderTests
,
Source_FinderTests
=
util
.
test_both
(
FinderTests
,
machinery
=
machinery
)
(
Frozen_FinderTests
,
Source_FinderTests
)
=
util
.
test_both
(
FinderTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/extension/test_loader.py
View file @
3497c0bf
...
...
@@ -76,8 +76,9 @@ class LoaderTests(abc.LoaderTests):
loader
=
self
.
machinery
.
ExtensionFileLoader
(
'pkg'
,
path
)
self
.
assertTrue
(
loader
.
is_package
(
'pkg'
))
Frozen_LoaderTests
,
Source_LoaderTests
=
util
.
test_both
(
LoaderTests
,
machinery
=
machinery
)
(
Frozen_LoaderTests
,
Source_LoaderTests
)
=
util
.
test_both
(
LoaderTests
,
machinery
=
machinery
)
...
...
Lib/test/test_importlib/extension/test_path_hook.py
View file @
3497c0bf
...
...
@@ -23,8 +23,10 @@ class PathHookTests:
# exists.
self
.
assertTrue
(
hasattr
(
self
.
hook
(
util
.
EXTENSIONS
.
path
),
'find_module'
))
Frozen_PathHooksTests
,
Source_PathHooksTests
=
util
.
test_both
(
PathHookTests
,
machinery
=
machinery
)
(
Frozen_PathHooksTests
,
Source_PathHooksTests
)
=
util
.
test_both
(
PathHookTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/frozen/test_finder.py
View file @
3497c0bf
...
...
@@ -37,8 +37,10 @@ class FindSpecTests(abc.FinderTests):
spec
=
self
.
find
(
'<not real>'
)
self
.
assertIsNone
(
spec
)
Frozen_FindSpecTests
,
Source_FindSpecTests
=
util
.
test_both
(
FindSpecTests
,
machinery
=
machinery
)
(
Frozen_FindSpecTests
,
Source_FindSpecTests
)
=
util
.
test_both
(
FindSpecTests
,
machinery
=
machinery
)
class
FinderTests
(
abc
.
FinderTests
):
...
...
@@ -72,8 +74,10 @@ class FinderTests(abc.FinderTests):
loader
=
self
.
find
(
'<not real>'
)
self
.
assertIsNone
(
loader
)
Frozen_FinderTests
,
Source_FinderTests
=
util
.
test_both
(
FinderTests
,
machinery
=
machinery
)
(
Frozen_FinderTests
,
Source_FinderTests
)
=
util
.
test_both
(
FinderTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/frozen/test_loader.py
View file @
3497c0bf
...
...
@@ -85,8 +85,10 @@ class ExecModuleTests(abc.LoaderTests):
self
.
exec_module
(
'_not_real'
)
self
.
assertEqual
(
cm
.
exception
.
name
,
'_not_real'
)
Frozen_ExecModuleTests
,
Source_ExecModuleTests
=
util
.
test_both
(
ExecModuleTests
,
machinery
=
machinery
)
(
Frozen_ExecModuleTests
,
Source_ExecModuleTests
)
=
util
.
test_both
(
ExecModuleTests
,
machinery
=
machinery
)
class
LoaderTests
(
abc
.
LoaderTests
):
...
...
@@ -175,8 +177,10 @@ class LoaderTests(abc.LoaderTests):
self
.
machinery
.
FrozenImporter
.
load_module
(
'_not_real'
)
self
.
assertEqual
(
cm
.
exception
.
name
,
'_not_real'
)
Frozen_LoaderTests
,
Source_LoaderTests
=
util
.
test_both
(
LoaderTests
,
machinery
=
machinery
)
(
Frozen_LoaderTests
,
Source_LoaderTests
)
=
util
.
test_both
(
LoaderTests
,
machinery
=
machinery
)
class
InspectLoaderTests
:
...
...
@@ -214,8 +218,9 @@ class InspectLoaderTests:
method
(
'importlib'
)
self
.
assertEqual
(
cm
.
exception
.
name
,
'importlib'
)
Frozen_ILTests
,
Source_ILTests
=
util
.
test_both
(
InspectLoaderTests
,
machinery
=
machinery
)
(
Frozen_ILTests
,
Source_ILTests
)
=
util
.
test_both
(
InspectLoaderTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/import_/test___loader__.py
View file @
3497c0bf
...
...
@@ -23,8 +23,10 @@ class SpecLoaderAttributeTests:
module
=
self
.
__import__
(
'blah'
)
self
.
assertEqual
(
loader
,
module
.
__loader__
)
Frozen_SpecTests
,
Source_SpecTests
=
util
.
test_both
(
SpecLoaderAttributeTests
,
__import__
=
util
.
__import__
)
(
Frozen_SpecTests
,
Source_SpecTests
)
=
util
.
test_both
(
SpecLoaderAttributeTests
,
__import__
=
util
.
__import__
)
class
LoaderMock
:
...
...
@@ -61,8 +63,9 @@ class LoaderAttributeTests:
self
.
assertEqual
(
loader
,
module
.
__loader__
)
Frozen_Tests
,
Source_Tests
=
util
.
test_both
(
LoaderAttributeTests
,
__import__
=
util
.
__import__
)
(
Frozen_Tests
,
Source_Tests
)
=
util
.
test_both
(
LoaderAttributeTests
,
__import__
=
util
.
__import__
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/import_/test___package__.py
View file @
3497c0bf
...
...
@@ -69,17 +69,23 @@ class Using__package__:
with
self
.
assertRaises
(
TypeError
):
self
.
__import__
(
''
,
globals
,
{},
[
'relimport'
],
1
)
class
Using__package__PEP302
(
Using__package__
):
mock_modules
=
util
.
mock_modules
Frozen_UsingPackagePEP302
,
Source_UsingPackagePEP302
=
util
.
test_both
(
Using__package__PEP302
,
__import__
=
util
.
__import__
)
(
Frozen_UsingPackagePEP302
,
Source_UsingPackagePEP302
)
=
util
.
test_both
(
Using__package__PEP302
,
__import__
=
util
.
__import__
)
class
Using__package__PEP451
(
Using__package__
):
mock_modules
=
util
.
mock_spec
Frozen_UsingPackagePEP451
,
Source_UsingPackagePEP451
=
util
.
test_both
(
Using__package__PEP451
,
__import__
=
util
.
__import__
)
(
Frozen_UsingPackagePEP451
,
Source_UsingPackagePEP451
)
=
util
.
test_both
(
Using__package__PEP451
,
__import__
=
util
.
__import__
)
class
Setting__package__
:
...
...
@@ -94,7 +100,7 @@ class Setting__package__:
"""
__import__
=
util
.
__import__
[
1
]
__import__
=
util
.
__import__
[
'Source'
]
# [top-level]
def
test_top_level
(
self
):
...
...
Lib/test/test_importlib/import_/test_api.py
View file @
3497c0bf
...
...
@@ -78,15 +78,19 @@ class APITest:
class
OldAPITests
(
APITest
):
bad_finder_loader
=
BadLoaderFinder
Frozen_OldAPITests
,
Source_OldAPITests
=
util
.
test_both
(
OldAPITests
,
__import__
=
util
.
__import__
)
(
Frozen_OldAPITests
,
Source_OldAPITests
)
=
util
.
test_both
(
OldAPITests
,
__import__
=
util
.
__import__
)
class
SpecAPITests
(
APITest
):
bad_finder_loader
=
BadSpecFinderLoader
Frozen_SpecAPITests
,
Source_SpecAPITests
=
util
.
test_both
(
SpecAPITests
,
__import__
=
util
.
__import__
)
(
Frozen_SpecAPITests
,
Source_SpecAPITests
)
=
util
.
test_both
(
SpecAPITests
,
__import__
=
util
.
__import__
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/import_/test_caching.py
View file @
3497c0bf
...
...
@@ -38,15 +38,17 @@ class UseCache:
self
.
__import__
(
name
)
self
.
assertEqual
(
cm
.
exception
.
name
,
name
)
Frozen_UseCache
,
Source_UseCache
=
util
.
test_both
(
UseCache
,
__import__
=
util
.
__import__
)
(
Frozen_UseCache
,
Source_UseCache
)
=
util
.
test_both
(
UseCache
,
__import__
=
util
.
__import__
)
class
ImportlibUseCache
(
UseCache
,
unittest
.
TestCase
):
# Pertinent only to PEP 302; exec_module() doesn't return a module.
__import__
=
util
.
__import__
[
1
]
__import__
=
util
.
__import__
[
'Source'
]
def
create_mock
(
self
,
*
names
,
return_
=
None
):
mock
=
util
.
mock_modules
(
*
names
)
...
...
Lib/test/test_importlib/import_/test_fromlist.py
View file @
3497c0bf
...
...
@@ -28,8 +28,10 @@ class ReturnValue:
module
=
self
.
__import__
(
'pkg.module'
,
fromlist
=
[
'attr'
])
self
.
assertEqual
(
module
.
__name__
,
'pkg.module'
)
Frozen_ReturnValue
,
Source_ReturnValue
=
util
.
test_both
(
ReturnValue
,
__import__
=
util
.
__import__
)
(
Frozen_ReturnValue
,
Source_ReturnValue
)
=
util
.
test_both
(
ReturnValue
,
__import__
=
util
.
__import__
)
class
HandlingFromlist
:
...
...
@@ -120,8 +122,10 @@ class HandlingFromlist:
self
.
assertEqual
(
module
.
module1
.
__name__
,
'pkg.module1'
)
self
.
assertEqual
(
module
.
module2
.
__name__
,
'pkg.module2'
)
Frozen_FromList
,
Source_FromList
=
util
.
test_both
(
HandlingFromlist
,
__import__
=
util
.
__import__
)
(
Frozen_FromList
,
Source_FromList
)
=
util
.
test_both
(
HandlingFromlist
,
__import__
=
util
.
__import__
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/import_/test_meta_path.py
View file @
3497c0bf
...
...
@@ -45,8 +45,10 @@ class CallingOrder:
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertTrue
(
issubclass
(
w
[
-
1
].
category
,
ImportWarning
))
Frozen_CallingOrder
,
Source_CallingOrder
=
util
.
test_both
(
CallingOrder
,
__import__
=
util
.
__import__
)
(
Frozen_CallingOrder
,
Source_CallingOrder
)
=
util
.
test_both
(
CallingOrder
,
__import__
=
util
.
__import__
)
class
CallSignature
:
...
...
@@ -99,19 +101,25 @@ class CallSignature:
self
.
assertEqual
(
args
[
0
],
mod_name
)
self
.
assertIs
(
args
[
1
],
path
)
class
CallSignaturePEP302
(
CallSignature
):
mock_modules
=
util
.
mock_modules
finder_name
=
'find_module'
Frozen_CallSignaturePEP302
,
Source_CallSignaturePEP302
=
util
.
test_both
(
CallSignaturePEP302
,
__import__
=
util
.
__import__
)
(
Frozen_CallSignaturePEP302
,
Source_CallSignaturePEP302
)
=
util
.
test_both
(
CallSignaturePEP302
,
__import__
=
util
.
__import__
)
class
CallSignaturePEP451
(
CallSignature
):
mock_modules
=
util
.
mock_spec
finder_name
=
'find_spec'
Frozen_CallSignaturePEP451
,
Source_CallSignaturePEP451
=
util
.
test_both
(
CallSignaturePEP451
,
__import__
=
util
.
__import__
)
(
Frozen_CallSignaturePEP451
,
Source_CallSignaturePEP451
)
=
util
.
test_both
(
CallSignaturePEP451
,
__import__
=
util
.
__import__
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/import_/test_packages.py
View file @
3497c0bf
...
...
@@ -101,8 +101,10 @@ class ParentModuleTests:
finally
:
support
.
unload
(
subname
)
Frozen_ParentTests
,
Source_ParentTests
=
util
.
test_both
(
ParentModuleTests
,
__import__
=
util
.
__import__
)
(
Frozen_ParentTests
,
Source_ParentTests
)
=
util
.
test_both
(
ParentModuleTests
,
__import__
=
util
.
__import__
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/import_/test_path.py
View file @
3497c0bf
...
...
@@ -158,8 +158,10 @@ class FinderTests:
got
=
self
.
machinery
.
PathFinder
.
find_spec
(
'whatever'
,
[
path
])
self
.
assertEqual
(
got
,
success_finder
.
spec
)
Frozen_FinderTests
,
Source_FinderTests
=
util
.
test_both
(
FinderTests
,
importlib
=
importlib
,
machinery
=
machinery
)
(
Frozen_FinderTests
,
Source_FinderTests
)
=
util
.
test_both
(
FinderTests
,
importlib
=
importlib
,
machinery
=
machinery
)
class
PathEntryFinderTests
:
...
...
@@ -182,8 +184,10 @@ class PathEntryFinderTests:
path_hooks
=
[
Finder
]):
self
.
machinery
.
PathFinder
.
find_spec
(
'importlib'
)
Frozen_PEFTests
,
Source_PEFTests
=
util
.
test_both
(
PathEntryFinderTests
,
machinery
=
machinery
)
(
Frozen_PEFTests
,
Source_PEFTests
)
=
util
.
test_both
(
PathEntryFinderTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/import_/test_relative_imports.py
View file @
3497c0bf
...
...
@@ -207,8 +207,10 @@ class RelativeImports:
with
self
.
assertRaises
(
KeyError
):
self
.
__import__
(
'sys'
,
level
=
1
)
Frozen_RelativeImports
,
Source_RelativeImports
=
util
.
test_both
(
RelativeImports
,
__import__
=
util
.
__import__
)
(
Frozen_RelativeImports
,
Source_RelativeImports
)
=
util
.
test_both
(
RelativeImports
,
__import__
=
util
.
__import__
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/source/test_case_sensitivity.py
View file @
3497c0bf
...
...
@@ -62,20 +62,28 @@ class CaseSensitivityTest:
self
.
assertIsNotNone
(
insensitive
)
self
.
assertIn
(
self
.
name
,
insensitive
.
get_filename
(
self
.
name
))
class
CaseSensitivityTestPEP302
(
CaseSensitivityTest
):
def
find
(
self
,
finder
):
return
finder
.
find_module
(
self
.
name
)
Frozen_CaseSensitivityTestPEP302
,
Source_CaseSensitivityTestPEP302
=
util
.
test_both
(
CaseSensitivityTestPEP302
,
importlib
=
importlib
,
machinery
=
machinery
)
(
Frozen_CaseSensitivityTestPEP302
,
Source_CaseSensitivityTestPEP302
)
=
util
.
test_both
(
CaseSensitivityTestPEP302
,
importlib
=
importlib
,
machinery
=
machinery
)
class
CaseSensitivityTestPEP451
(
CaseSensitivityTest
):
def
find
(
self
,
finder
):
found
=
finder
.
find_spec
(
self
.
name
)
return
found
.
loader
if
found
is
not
None
else
found
Frozen_CaseSensitivityTestPEP451
,
Source_CaseSensitivityTestPEP451
=
util
.
test_both
(
CaseSensitivityTestPEP451
,
importlib
=
importlib
,
machinery
=
machinery
)
(
Frozen_CaseSensitivityTestPEP451
,
Source_CaseSensitivityTestPEP451
)
=
util
.
test_both
(
CaseSensitivityTestPEP451
,
importlib
=
importlib
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/source/test_file_loader.py
View file @
3497c0bf
...
...
@@ -235,9 +235,11 @@ class SimpleTest(abc.LoaderTests):
warnings
.
simplefilter
(
'ignore'
,
DeprecationWarning
)
loader
.
load_module
(
'bad name'
)
Frozen_SimpleTest
,
Source_SimpleTest
=
util
.
test_both
(
SimpleTest
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
(
Frozen_SimpleTest
,
Source_SimpleTest
)
=
util
.
test_both
(
SimpleTest
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
class
BadBytecodeTest
:
...
...
@@ -346,6 +348,7 @@ class BadBytecodeTest:
lambda
bc
:
b'
\
x00
\
x00
\
x00
\
x00
'
+
bc
[
4
:])
test
(
'_temp'
,
mapping
,
bc_path
)
class
BadBytecodeTestPEP451
(
BadBytecodeTest
):
def
import_
(
self
,
file
,
module_name
):
...
...
@@ -354,6 +357,7 @@ class BadBytecodeTestPEP451(BadBytecodeTest):
module
.
__spec__
=
self
.
util
.
spec_from_loader
(
module_name
,
loader
)
loader
.
exec_module
(
module
)
class
BadBytecodeTestPEP302
(
BadBytecodeTest
):
def
import_
(
self
,
file
,
module_name
):
...
...
@@ -490,21 +494,29 @@ class SourceLoaderBadBytecodeTest:
# Make writable for eventual clean-up.
os
.
chmod
(
bytecode_path
,
stat
.
S_IWUSR
)
class
SourceLoaderBadBytecodeTestPEP451
(
SourceLoaderBadBytecodeTest
,
BadBytecodeTestPEP451
):
pass
Frozen_SourceBadBytecodePEP451
,
Source_SourceBadBytecodePEP451
=
util
.
test_both
(
SourceLoaderBadBytecodeTestPEP451
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
(
Frozen_SourceBadBytecodePEP451
,
Source_SourceBadBytecodePEP451
)
=
util
.
test_both
(
SourceLoaderBadBytecodeTestPEP451
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
class
SourceLoaderBadBytecodeTestPEP302
(
SourceLoaderBadBytecodeTest
,
BadBytecodeTestPEP302
):
pass
Frozen_SourceBadBytecodePEP302
,
Source_SourceBadBytecodePEP302
=
util
.
test_both
(
SourceLoaderBadBytecodeTestPEP302
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
(
Frozen_SourceBadBytecodePEP302
,
Source_SourceBadBytecodePEP302
)
=
util
.
test_both
(
SourceLoaderBadBytecodeTestPEP302
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
class
SourcelessLoaderBadBytecodeTest
:
...
...
@@ -566,21 +578,29 @@ class SourcelessLoaderBadBytecodeTest:
def
test_non_code_marshal
(
self
):
self
.
_test_non_code_marshal
(
del_source
=
True
)
class
SourcelessLoaderBadBytecodeTestPEP451
(
SourcelessLoaderBadBytecodeTest
,
BadBytecodeTestPEP451
):
pass
Frozen_SourcelessBadBytecodePEP451
,
Source_SourcelessBadBytecodePEP451
=
util
.
test_both
(
SourcelessLoaderBadBytecodeTestPEP451
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
(
Frozen_SourcelessBadBytecodePEP451
,
Source_SourcelessBadBytecodePEP451
)
=
util
.
test_both
(
SourcelessLoaderBadBytecodeTestPEP451
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
class
SourcelessLoaderBadBytecodeTestPEP302
(
SourcelessLoaderBadBytecodeTest
,
BadBytecodeTestPEP302
):
pass
Frozen_SourcelessBadBytecodePEP302
,
Source_SourcelessBadBytecodePEP302
=
util
.
test_both
(
SourcelessLoaderBadBytecodeTestPEP302
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
(
Frozen_SourcelessBadBytecodePEP302
,
Source_SourcelessBadBytecodePEP302
)
=
util
.
test_both
(
SourcelessLoaderBadBytecodeTestPEP302
,
importlib
=
importlib
,
machinery
=
machinery
,
abc
=
importlib_abc
,
util
=
importlib_util
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/source/test_finder.py
View file @
3497c0bf
...
...
@@ -195,8 +195,10 @@ class FinderTestsPEP451(FinderTests):
spec
=
finder
.
find_spec
(
name
)
return
spec
.
loader
if
spec
is
not
None
else
spec
Frozen_FinderTestsPEP451
,
Source_FinderTestsPEP451
=
util
.
test_both
(
FinderTestsPEP451
,
machinery
=
machinery
)
(
Frozen_FinderTestsPEP451
,
Source_FinderTestsPEP451
)
=
util
.
test_both
(
FinderTestsPEP451
,
machinery
=
machinery
)
class
FinderTestsPEP420
(
FinderTests
):
...
...
@@ -209,8 +211,10 @@ class FinderTestsPEP420(FinderTests):
loader_portions
=
finder
.
find_loader
(
name
)
return
loader_portions
[
0
]
if
loader_only
else
loader_portions
Frozen_FinderTestsPEP420
,
Source_FinderTestsPEP420
=
util
.
test_both
(
FinderTestsPEP420
,
machinery
=
machinery
)
(
Frozen_FinderTestsPEP420
,
Source_FinderTestsPEP420
)
=
util
.
test_both
(
FinderTestsPEP420
,
machinery
=
machinery
)
class
FinderTestsPEP302
(
FinderTests
):
...
...
@@ -222,9 +226,10 @@ class FinderTestsPEP302(FinderTests):
warnings
.
simplefilter
(
"ignore"
,
DeprecationWarning
)
return
finder
.
find_module
(
name
)
Frozen_FinderTestsPEP302
,
Source_FinderTestsPEP302
=
util
.
test_both
(
FinderTestsPEP302
,
machinery
=
machinery
)
(
Frozen_FinderTestsPEP302
,
Source_FinderTestsPEP302
)
=
util
.
test_both
(
FinderTestsPEP302
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/source/test_path_hook.py
View file @
3497c0bf
...
...
@@ -22,7 +22,10 @@ class PathHookTest:
# The empty string represents the cwd.
self
.
assertTrue
(
hasattr
(
self
.
path_hook
()(
''
),
'find_module'
))
Frozen_PathHookTest
,
Source_PathHooktest
=
util
.
test_both
(
PathHookTest
,
machinery
=
machinery
)
(
Frozen_PathHookTest
,
Source_PathHooktest
)
=
util
.
test_both
(
PathHookTest
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/source/test_source_encoding.py
View file @
3497c0bf
...
...
@@ -88,6 +88,7 @@ class EncodingTest:
with self.assertRaises(SyntaxError):
self.run_test(source)
class EncodingTestPEP451(EncodingTest):
def load(self, loader):
...
...
@@ -96,8 +97,11 @@ class EncodingTestPEP451(EncodingTest):
loader.exec_module(module)
return module
Frozen_EncodingTestPEP451, Source_EncodingTestPEP451 = util.test_both(
EncodingTestPEP451, machinery=machinery)
(Frozen_EncodingTestPEP451,
Source_EncodingTestPEP451
) = util.test_both(EncodingTestPEP451, machinery=machinery)
class EncodingTestPEP302(EncodingTest):
...
...
@@ -106,8 +110,10 @@ class EncodingTestPEP302(EncodingTest):
warnings.simplefilter('
ignore
', DeprecationWarning)
return loader.load_module(self.module_name)
Frozen_EncodingTestPEP302, Source_EncodingTestPEP302 = util.test_both(
EncodingTestPEP302, machinery=machinery)
(Frozen_EncodingTestPEP302,
Source_EncodingTestPEP302
) = util.test_both(EncodingTestPEP302, machinery=machinery)
class LineEndingTest:
...
...
@@ -138,6 +144,7 @@ class LineEndingTest:
def test_lf(self):
self.run_test(b'
\
n
')
class LineEndingTestPEP451(LineEndingTest):
def load(self, loader, module_name):
...
...
@@ -146,8 +153,11 @@ class LineEndingTestPEP451(LineEndingTest):
loader.exec_module(module)
return module
Frozen_LineEndingTestPEP451, Source_LineEndingTestPEP451 = util.test_both(
LineEndingTestPEP451, machinery=machinery)
(Frozen_LineEndingTestPEP451,
Source_LineEndingTestPEP451
) = util.test_both(LineEndingTestPEP451, machinery=machinery)
class LineEndingTestPEP302(LineEndingTest):
...
...
@@ -156,8 +166,10 @@ class LineEndingTestPEP302(LineEndingTest):
warnings.simplefilter('
ignore
', DeprecationWarning)
return loader.load_module(module_name)
Frozen_LineEndingTestPEP302, Source_LineEndingTestPEP302 = util.test_both(
LineEndingTestPEP302, machinery=machinery)
(Frozen_LineEndingTestPEP302,
Source_LineEndingTestPEP302
) = util.test_both(LineEndingTestPEP302, machinery=machinery)
if __name__ == '
__main__
':
...
...
Lib/test/test_importlib/test_abc.py
View file @
3497c0bf
...
...
@@ -10,12 +10,13 @@ import unittest
from
unittest
import
mock
import
warnings
from
.
import
util
from
.
import
util
as
test_util
init
=
test_util
.
import_importlib
(
'importlib'
)
abc
=
test_util
.
import_importlib
(
'importlib.abc'
)
machinery
=
test_util
.
import_importlib
(
'importlib.machinery'
)
util
=
test_util
.
import_importlib
(
'importlib.util'
)
frozen_init
,
source_init
=
util
.
import_importlib
(
'importlib'
)
frozen_abc
,
source_abc
=
util
.
import_importlib
(
'importlib.abc'
)
machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
frozen_util
,
source_util
=
util
.
import_importlib
(
'importlib.util'
)
##### Inheritance ##############################################################
class
InheritanceTests
:
...
...
@@ -26,8 +27,7 @@ class InheritanceTests:
subclasses
=
[]
superclasses
=
[]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
def
setUp
(
self
):
self
.
superclasses
=
[
getattr
(
self
.
abc
,
class_name
)
for
class_name
in
self
.
superclass_names
]
if
hasattr
(
self
,
'subclass_names'
):
...
...
@@ -36,11 +36,11 @@ class InheritanceTests:
# checking across module boundaries (i.e. the _bootstrap in abc is
# not the same as the one in machinery). That means stealing one of
# the modules from the other to make sure the same instance is used.
self
.
subclasses
=
[
getattr
(
self
.
abc
.
machinery
,
class_name
)
for
class_name
in
self
.
subclass_names
]
machinery
=
self
.
abc
.
machinery
self
.
subclasses
=
[
getattr
(
machinery
,
class_name
)
for
class_name
in
self
.
subclass_names
]
assert
self
.
subclasses
or
self
.
superclasses
,
self
.
__class__
testing
=
self
.
__class__
.
__name__
.
partition
(
'_'
)[
2
]
self
.
__test
=
getattr
(
self
.
abc
,
testing
)
self
.
__test
=
getattr
(
self
.
abc
,
self
.
_NAME
)
def
test_subclasses
(
self
):
# Test that the expected subclasses inherit.
...
...
@@ -54,94 +54,97 @@ class InheritanceTests:
self
.
assertTrue
(
issubclass
(
self
.
__test
,
superclass
),
"{0} is not a superclass of {1}"
.
format
(
superclass
,
self
.
__test
))
def
create_inheritance_tests
(
base_class
):
def
set_frozen
(
ns
):
ns
[
'abc'
]
=
frozen_abc
def
set_source
(
ns
):
ns
[
'abc'
]
=
source_abc
classes
=
[]
for
prefix
,
ns_set
in
[(
'Frozen'
,
set_frozen
),
(
'Source'
,
set_source
)]:
classes
.
append
(
types
.
new_class
(
'_'
.
join
([
prefix
,
base_class
.
__name__
]),
(
base_class
,
unittest
.
TestCase
),
exec_body
=
ns_set
))
return
classes
class
MetaPathFinder
(
InheritanceTests
):
superclass_names
=
[
'Finder'
]
subclass_names
=
[
'BuiltinImporter'
,
'FrozenImporter'
,
'PathFinder'
,
'WindowsRegistryFinder'
]
tests
=
create_inheritance_tests
(
MetaPathFinder
)
Frozen_MetaPathFinderInheritanceTests
,
Source_MetaPathFinderInheritanceTests
=
tests
(
Frozen_MetaPathFinderInheritanceTests
,
Source_MetaPathFinderInheritanceTests
)
=
test_util
.
test_both
(
MetaPathFinder
,
abc
=
abc
)
class
PathEntryFinder
(
InheritanceTests
):
superclass_names
=
[
'Finder'
]
subclass_names
=
[
'FileFinder'
]
tests
=
create_inheritance_tests
(
PathEntryFinder
)
Frozen_PathEntryFinderInheritanceTests
,
Source_PathEntryFinderInheritanceTests
=
tests
(
Frozen_PathEntryFinderInheritanceTests
,
Source_PathEntryFinderInheritanceTests
)
=
test_util
.
test_both
(
PathEntryFinder
,
abc
=
abc
)
class
ResourceLoader
(
InheritanceTests
):
superclass_names
=
[
'Loader'
]
tests
=
create_inheritance_tests
(
ResourceLoader
)
Frozen_ResourceLoaderInheritanceTests
,
Source_ResourceLoaderInheritanceTests
=
tests
(
Frozen_ResourceLoaderInheritanceTests
,
Source_ResourceLoaderInheritanceTests
)
=
test_util
.
test_both
(
ResourceLoader
,
abc
=
abc
)
class
InspectLoader
(
InheritanceTests
):
superclass_names
=
[
'Loader'
]
subclass_names
=
[
'BuiltinImporter'
,
'FrozenImporter'
,
'ExtensionFileLoader'
]
tests
=
create_inheritance_tests
(
InspectLoader
)
Frozen_InspectLoaderInheritanceTests
,
Source_InspectLoaderInheritanceTests
=
tests
(
Frozen_InspectLoaderInheritanceTests
,
Source_InspectLoaderInheritanceTests
)
=
test_util
.
test_both
(
InspectLoader
,
abc
=
abc
)
class
ExecutionLoader
(
InheritanceTests
):
superclass_names
=
[
'InspectLoader'
]
subclass_names
=
[
'ExtensionFileLoader'
]
tests
=
create_inheritance_tests
(
ExecutionLoader
)
Frozen_ExecutionLoaderInheritanceTests
,
Source_ExecutionLoaderInheritanceTests
=
tests
(
Frozen_ExecutionLoaderInheritanceTests
,
Source_ExecutionLoaderInheritanceTests
)
=
test_util
.
test_both
(
ExecutionLoader
,
abc
=
abc
)
class
FileLoader
(
InheritanceTests
):
superclass_names
=
[
'ResourceLoader'
,
'ExecutionLoader'
]
subclass_names
=
[
'SourceFileLoader'
,
'SourcelessFileLoader'
]
tests
=
create_inheritance_tests
(
FileLoader
)
Frozen_FileLoaderInheritanceTests
,
Source_FileLoaderInheritanceTests
=
tests
(
Frozen_FileLoaderInheritanceTests
,
Source_FileLoaderInheritanceTests
)
=
test_util
.
test_both
(
FileLoader
,
abc
=
abc
)
class
SourceLoader
(
InheritanceTests
):
superclass_names
=
[
'ResourceLoader'
,
'ExecutionLoader'
]
subclass_names
=
[
'SourceFileLoader'
]
tests
=
create_inheritance_tests
(
SourceLoader
)
Frozen_SourceLoaderInheritanceTests
,
Source_SourceLoaderInheritanceTests
=
tests
(
Frozen_SourceLoaderInheritanceTests
,
Source_SourceLoaderInheritanceTests
)
=
test_util
.
test_both
(
SourceLoader
,
abc
=
abc
)
##### Default return values ####################################################
def
make_abc_subclasses
(
base_class
):
classes
=
[]
for
kind
,
abc
in
[(
'Frozen'
,
frozen_abc
),
(
'Source'
,
source_abc
)]:
name
=
'_'
.
join
([
kind
,
base_class
.
__name__
])
base_classes
=
base_class
,
getattr
(
abc
,
base_class
.
__name__
)
classes
.
append
(
types
.
new_class
(
name
,
base_classes
))
return
classes
def
make_return_value_tests
(
base_class
,
test_class
):
frozen_class
,
source_class
=
make_abc_subclasses
(
base_class
)
tests
=
[]
for
prefix
,
class_in_test
in
[(
'Frozen'
,
frozen_class
),
(
'Source'
,
source_class
)]:
def
set_ns
(
ns
):
ns
[
'ins'
]
=
class_in_test
()
tests
.
append
(
types
.
new_class
(
'_'
.
join
([
prefix
,
test_class
.
__name__
]),
(
test_class
,
unittest
.
TestCase
),
exec_body
=
set_ns
))
return
tests
def
make_abc_subclasses
(
base_class
,
name
=
None
,
inst
=
False
,
**
kwargs
):
if
name
is
None
:
name
=
base_class
.
__name__
base
=
{
kind
:
getattr
(
splitabc
,
name
)
for
kind
,
splitabc
in
abc
.
items
()}
return
{
cls
.
_KIND
:
cls
()
if
inst
else
cls
for
cls
in
test_util
.
split_frozen
(
base_class
,
base
,
**
kwargs
)}
class
ABCTestHarness
:
@
property
def
ins
(
self
):
# Lazily set ins on the class.
cls
=
self
.
SPLIT
[
self
.
_KIND
]
ins
=
cls
()
self
.
__class__
.
ins
=
ins
return
ins
class
MetaPathFinder
:
...
...
@@ -149,10 +152,10 @@ class MetaPathFinder:
def
find_module
(
self
,
fullname
,
path
):
return
super
().
find_module
(
fullname
,
path
)
Frozen_MPF
,
Source_MPF
=
make_abc_subclasses
(
MetaPathFinder
)
class
MetaPathFinderDefaultsTests
(
ABCTestHarness
):
class
MetaPathFinderDefaultsTests
:
SPLIT
=
make_abc_subclasses
(
MetaPathFinder
)
def
test_find_module
(
self
):
# Default should return None.
...
...
@@ -163,8 +166,9 @@ class MetaPathFinderDefaultsTests:
self
.
ins
.
invalidate_caches
()
tests
=
make_return_value_tests
(
MetaPathFinder
,
MetaPathFinderDefaultsTests
)
Frozen_MPFDefaultTests
,
Source_MPFDefaultTests
=
tests
(
Frozen_MPFDefaultTests
,
Source_MPFDefaultTests
)
=
test_util
.
test_both
(
MetaPathFinderDefaultsTests
)
class
PathEntryFinder
:
...
...
@@ -172,10 +176,10 @@ class PathEntryFinder:
def
find_loader
(
self
,
fullname
):
return
super
().
find_loader
(
fullname
)
Frozen_PEF
,
Source_PEF
=
make_abc_subclasses
(
PathEntryFinder
)
class
PathEntryFinderDefaultsTests
(
ABCTestHarness
):
class
PathEntryFinderDefaultsTests
:
SPLIT
=
make_abc_subclasses
(
PathEntryFinder
)
def
test_find_loader
(
self
):
self
.
assertEqual
((
None
,
[]),
self
.
ins
.
find_loader
(
'something'
))
...
...
@@ -188,8 +192,9 @@ class PathEntryFinderDefaultsTests:
self
.
ins
.
invalidate_caches
()
tests
=
make_return_value_tests
(
PathEntryFinder
,
PathEntryFinderDefaultsTests
)
Frozen_PEFDefaultTests
,
Source_PEFDefaultTests
=
tests
(
Frozen_PEFDefaultTests
,
Source_PEFDefaultTests
)
=
test_util
.
test_both
(
PathEntryFinderDefaultsTests
)
class
Loader
:
...
...
@@ -198,10 +203,9 @@ class Loader:
return
super
().
load_module
(
fullname
)
Frozen_L
,
Source_L
=
make_abc_subclasses
(
Loader
)
class
LoaderDefaultsTests
(
ABCTestHarness
):
class
LoaderDefaultsTests
:
SPLIT
=
make_abc_subclasses
(
Loader
)
def
test_load_module
(
self
):
with
self
.
assertRaises
(
ImportError
):
...
...
@@ -217,8 +221,9 @@ class LoaderDefaultsTests:
self
.
assertTrue
(
repr
(
mod
))
tests
=
make_return_value_tests
(
Loader
,
LoaderDefaultsTests
)
Frozen_LDefaultTests
,
SourceLDefaultTests
=
tests
(
Frozen_LDefaultTests
,
SourceLDefaultTests
)
=
test_util
.
test_both
(
LoaderDefaultsTests
)
class
ResourceLoader
(
Loader
):
...
...
@@ -227,18 +232,18 @@ class ResourceLoader(Loader):
return
super
().
get_data
(
path
)
Frozen_RL
,
Source_RL
=
make_abc_subclasses
(
ResourceLoader
)
class
ResourceLoaderDefaultsTests
(
ABCTestHarness
):
class
ResourceLoaderDefaultsTests
:
SPLIT
=
make_abc_subclasses
(
ResourceLoader
)
def
test_get_data
(
self
):
with
self
.
assertRaises
(
IOError
):
self
.
ins
.
get_data
(
'/some/path'
)
tests
=
make_return_value_tests
(
ResourceLoader
,
ResourceLoaderDefaultsTests
)
Frozen_RLDefaultTests
,
Source_RLDefaultTests
=
tests
(
Frozen_RLDefaultTests
,
Source_RLDefaultTests
)
=
test_util
.
test_both
(
ResourceLoaderDefaultsTests
)
class
InspectLoader
(
Loader
):
...
...
@@ -250,10 +255,12 @@ class InspectLoader(Loader):
return
super
().
get_source
(
fullname
)
Frozen_IL
,
Source
_IL
=
make_abc_subclasses
(
InspectLoader
)
SPLIT
_IL
=
make_abc_subclasses
(
InspectLoader
)
class
InspectLoaderDefaultsTests
:
class
InspectLoaderDefaultsTests
(
ABCTestHarness
):
SPLIT
=
SPLIT_IL
def
test_is_package
(
self
):
with
self
.
assertRaises
(
ImportError
):
...
...
@@ -264,8 +271,9 @@ class InspectLoaderDefaultsTests:
self
.
ins
.
get_source
(
'blah'
)
tests
=
make_return_value_tests
(
InspectLoader
,
InspectLoaderDefaultsTests
)
Frozen_ILDefaultTests
,
Source_ILDefaultTests
=
tests
(
Frozen_ILDefaultTests
,
Source_ILDefaultTests
)
=
test_util
.
test_both
(
InspectLoaderDefaultsTests
)
class
ExecutionLoader
(
InspectLoader
):
...
...
@@ -273,21 +281,25 @@ class ExecutionLoader(InspectLoader):
def
get_filename
(
self
,
fullname
):
return
super
().
get_filename
(
fullname
)
Frozen_EL
,
Source_EL
=
make_abc_subclasses
(
ExecutionLoader
)
SPLIT_EL
=
make_abc_subclasses
(
ExecutionLoader
)
class
ExecutionLoaderDefaultsTests
:
class
ExecutionLoaderDefaultsTests
(
ABCTestHarness
):
SPLIT
=
SPLIT_EL
def
test_get_filename
(
self
):
with
self
.
assertRaises
(
ImportError
):
self
.
ins
.
get_filename
(
'blah'
)
tests
=
make_return_value_tests
(
ExecutionLoader
,
InspectLoaderDefaultsTests
)
Frozen_ELDefaultTests
,
Source_ELDefaultsTests
=
tests
(
Frozen_ELDefaultTests
,
Source_ELDefaultsTests
)
=
test_util
.
test_both
(
InspectLoaderDefaultsTests
)
##### MetaPathFinder concrete methods ##########################################
##### MetaPathFinder concrete methods ##########################################
class
MetaPathFinderFindModuleTests
:
@
classmethod
...
...
@@ -317,13 +329,12 @@ class MetaPathFinderFindModuleTests:
self
.
assertIs
(
found
,
spec
.
loader
)
Frozen_MPFFindModuleTests
,
Source_MPFFindModuleTests
=
util
.
test_both
(
MetaPathFinderFindModuleTests
,
abc
=
(
frozen_abc
,
source_abc
),
util
=
(
frozen_util
,
source_util
))
(
Frozen_MPFFindModuleTests
,
Source_MPFFindModuleTests
)
=
test_util
.
test_both
(
MetaPathFinderFindModuleTests
,
abc
=
abc
,
util
=
util
)
##### PathEntryFinder concrete methods #########################################
##### PathEntryFinder concrete methods #########################################
class
PathEntryFinderFindLoaderTests
:
@
classmethod
...
...
@@ -361,11 +372,10 @@ class PathEntryFinderFindLoaderTests:
self
.
assertEqual
(
paths
,
found
[
1
])
Frozen_PEFFindLoaderTests
,
Source_PEFFindLoaderTests
=
util
.
test_both
(
PathEntryFinderFindLoaderTests
,
abc
=
(
frozen_abc
,
source_abc
),
machinery
=
machinery
,
util
=
(
frozen_util
,
source_util
))
(
Frozen_PEFFindLoaderTests
,
Source_PEFFindLoaderTests
)
=
test_util
.
test_both
(
PathEntryFinderFindLoaderTests
,
abc
=
abc
,
util
=
util
,
machinery
=
machinery
)
##### Loader concrete methods ##################################################
...
...
@@ -386,7 +396,7 @@ class LoaderLoadModuleTests:
def
test_fresh
(
self
):
loader
=
self
.
loader
()
name
=
'blah'
with
util
.
uncache
(
name
):
with
test_
util
.
uncache
(
name
):
loader
.
load_module
(
name
)
module
=
loader
.
found
self
.
assertIs
(
sys
.
modules
[
name
],
module
)
...
...
@@ -404,7 +414,7 @@ class LoaderLoadModuleTests:
module
=
types
.
ModuleType
(
name
)
module
.
__spec__
=
self
.
util
.
spec_from_loader
(
name
,
loader
)
module
.
__loader__
=
loader
with
util
.
uncache
(
name
):
with
test_
util
.
uncache
(
name
):
sys
.
modules
[
name
]
=
module
loader
.
load_module
(
name
)
found
=
loader
.
found
...
...
@@ -412,10 +422,9 @@ class LoaderLoadModuleTests:
self
.
assertIs
(
module
,
sys
.
modules
[
name
])
Frozen_LoaderLoadModuleTests
,
Source_LoaderLoadModuleTests
=
util
.
test_both
(
LoaderLoadModuleTests
,
abc
=
(
frozen_abc
,
source_abc
),
util
=
(
frozen_util
,
source_util
))
(
Frozen_LoaderLoadModuleTests
,
Source_LoaderLoadModuleTests
)
=
test_util
.
test_both
(
LoaderLoadModuleTests
,
abc
=
abc
,
util
=
util
)
##### InspectLoader concrete methods ###########################################
...
...
@@ -461,11 +470,10 @@ class InspectLoaderSourceToCodeTests:
self
.
assertEqual
(
code
.
co_filename
,
'<string>'
)
class
Frozen_ILSourceToCodeTests
(
InspectLoaderSourceToCodeTests
,
unittest
.
TestCase
):
InspectLoaderSubclass
=
Frozen_IL
class
Source_ILSourceToCodeTests
(
InspectLoaderSourceToCodeTests
,
unittest
.
TestCase
):
InspectLoaderSubclass
=
Source_IL
(
Frozen_ILSourceToCodeTests
,
Source_ILSourceToCodeTests
)
=
test_util
.
test_both
(
InspectLoaderSourceToCodeTests
,
InspectLoaderSubclass
=
SPLIT_IL
)
class
InspectLoaderGetCodeTests
:
...
...
@@ -495,11 +503,10 @@ class InspectLoaderGetCodeTests:
loader
.
get_code
(
'blah'
)
class
Frozen_ILGetCodeTests
(
InspectLoaderGetCodeTests
,
unittest
.
TestCase
):
InspectLoaderSubclass
=
Frozen_IL
class
Source_ILGetCodeTests
(
InspectLoaderGetCodeTests
,
unittest
.
TestCase
):
InspectLoaderSubclass
=
Source_IL
(
Frozen_ILGetCodeTests
,
Source_ILGetCodeTests
)
=
test_util
.
test_both
(
InspectLoaderGetCodeTests
,
InspectLoaderSubclass
=
SPLIT_IL
)
class
InspectLoaderLoadModuleTests
:
...
...
@@ -543,11 +550,10 @@ class InspectLoaderLoadModuleTests:
self
.
assertEqual
(
module
,
sys
.
modules
[
self
.
module_name
])
class
Frozen_ILLoadModuleTests
(
InspectLoaderLoadModuleTests
,
unittest
.
TestCase
):
InspectLoaderSubclass
=
Frozen_IL
class
Source_ILLoadModuleTests
(
InspectLoaderLoadModuleTests
,
unittest
.
TestCase
):
InspectLoaderSubclass
=
Source_IL
(
Frozen_ILLoadModuleTests
,
Source_ILLoadModuleTests
)
=
test_util
.
test_both
(
InspectLoaderLoadModuleTests
,
InspectLoaderSubclass
=
SPLIT_IL
)
##### ExecutionLoader concrete methods #########################################
...
...
@@ -608,15 +614,14 @@ class ExecutionLoaderGetCodeTests:
self
.
assertEqual
(
module
.
attr
,
42
)
class
Frozen_ELGetCodeTests
(
ExecutionLoaderGetCodeTests
,
unittest
.
TestCase
):
ExecutionLoaderSubclass
=
Frozen_EL
class
Source_ELGetCodeTests
(
ExecutionLoaderGetCodeTests
,
unittest
.
TestCase
):
ExecutionLoaderSubclass
=
Source_EL
(
Frozen_ELGetCodeTests
,
Source_ELGetCodeTests
)
=
test_util
.
test_both
(
ExecutionLoaderGetCodeTests
,
ExecutionLoaderSubclass
=
SPLIT_EL
)
##### SourceLoader concrete methods ############################################
class
SourceLoader
:
class
Source
Only
Loader
:
# Globals that should be defined for all modules.
source
=
(
b"_ = '::'.join([__name__, __file__, __cached__, __package__, "
...
...
@@ -637,10 +642,10 @@ class SourceLoader:
return
'<module>'
Frozen_SourceOnlyL
,
Source_SourceOnlyL
=
make_abc_subclasses
(
SourceLoader
)
SPLIT_SOL
=
make_abc_subclasses
(
SourceOnlyLoader
,
'SourceLoader'
)
class
SourceLoader
(
SourceLoader
):
class
SourceLoader
(
Source
Only
Loader
):
source_mtime
=
1
...
...
@@ -677,11 +682,7 @@ class SourceLoader(SourceLoader):
return
path
==
self
.
bytecode_path
Frozen_SL
,
Source_SL
=
make_abc_subclasses
(
SourceLoader
)
Frozen_SL
.
util
=
frozen_util
Source_SL
.
util
=
source_util
Frozen_SL
.
init
=
frozen_init
Source_SL
.
init
=
source_init
SPLIT_SL
=
make_abc_subclasses
(
SourceLoader
,
util
=
util
,
init
=
init
)
class
SourceLoaderTestHarness
:
...
...
@@ -765,7 +766,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
# Loading a module should set __name__, __loader__, __package__,
# __path__ (for packages), __file__, and __cached__.
# The module should also be put into sys.modules.
with
util
.
uncache
(
self
.
name
):
with
test_
util
.
uncache
(
self
.
name
):
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
'ignore'
,
DeprecationWarning
)
module
=
self
.
loader
.
load_module
(
self
.
name
)
...
...
@@ -778,7 +779,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
# is a package.
# Testing the values for a package are covered by test_load_module.
self
.
setUp
(
is_package
=
False
)
with
util
.
uncache
(
self
.
name
):
with
test_
util
.
uncache
(
self
.
name
):
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
'ignore'
,
DeprecationWarning
)
module
=
self
.
loader
.
load_module
(
self
.
name
)
...
...
@@ -798,13 +799,10 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
self
.
assertEqual
(
returned_source
,
source
)
class
Frozen_SourceOnlyLTests
(
SourceOnlyLoaderTests
,
unittest
.
TestCase
):
loader_mock
=
Frozen_SourceOnlyL
util
=
frozen_util
class
Source_SourceOnlyLTests
(
SourceOnlyLoaderTests
,
unittest
.
TestCase
):
loader_mock
=
Source_SourceOnlyL
util
=
source_util
(
Frozen_SourceOnlyLoaderTests
,
Source_SourceOnlyLoaderTests
)
=
test_util
.
test_both
(
SourceOnlyLoaderTests
,
util
=
util
,
loader_mock
=
SPLIT_SOL
)
@
unittest
.
skipIf
(
sys
.
dont_write_bytecode
,
"sys.dont_write_bytecode is true"
)
...
...
@@ -896,15 +894,10 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
self
.
verify_code
(
code_object
)
class
Frozen_SLBytecodeTests
(
SourceLoaderBytecodeTests
,
unittest
.
TestCase
):
loader_mock
=
Frozen_SL
init
=
frozen_init
util
=
frozen_util
class
SourceSLBytecodeTests
(
SourceLoaderBytecodeTests
,
unittest
.
TestCase
):
loader_mock
=
Source_SL
init
=
source_init
util
=
source_util
(
Frozen_SLBytecodeTests
,
SourceSLBytecodeTests
)
=
test_util
.
test_both
(
SourceLoaderBytecodeTests
,
init
=
init
,
util
=
util
,
loader_mock
=
SPLIT_SL
)
class
SourceLoaderGetSourceTests
:
...
...
@@ -940,11 +933,10 @@ class SourceLoaderGetSourceTests:
self
.
assertEqual
(
mock
.
get_source
(
name
),
expect
)
class
Frozen_SourceOnlyLGetSourceTests
(
SourceLoaderGetSourceTests
,
unittest
.
TestCase
):
SourceOnlyLoaderMock
=
Frozen_SourceOnlyL
class
Source_SourceOnlyLGetSourceTests
(
SourceLoaderGetSourceTests
,
unittest
.
TestCase
):
SourceOnlyLoaderMock
=
Source_SourceOnlyL
(
Frozen_SourceOnlyLoaderGetSourceTests
,
Source_SourceOnlyLoaderGetSourceTests
)
=
test_util
.
test_both
(
SourceLoaderGetSourceTests
,
SourceOnlyLoaderMock
=
SPLIT_SOL
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/test_api.py
View file @
3497c0bf
from
.
import
util
from
.
import
util
as
test_util
frozen_init
,
source_init
=
util
.
import_importlib
(
'importlib'
)
frozen_util
,
source_util
=
util
.
import_importlib
(
'importlib.util'
)
frozen_machinery
,
source_machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
init
=
test_
util
.
import_importlib
(
'importlib'
)
util
=
test_
util
.
import_importlib
(
'importlib.util'
)
machinery
=
test_
util
.
import_importlib
(
'importlib.machinery'
)
import
os.path
import
sys
...
...
@@ -18,8 +18,8 @@ class ImportModuleTests:
def
test_module_import
(
self
):
# Test importing a top-level module.
with
util
.
mock_modules
(
'top_level'
)
as
mock
:
with
util
.
import_state
(
meta_path
=
[
mock
]):
with
test_
util
.
mock_modules
(
'top_level'
)
as
mock
:
with
test_
util
.
import_state
(
meta_path
=
[
mock
]):
module
=
self
.
init
.
import_module
(
'top_level'
)
self
.
assertEqual
(
module
.
__name__
,
'top_level'
)
...
...
@@ -28,8 +28,8 @@ class ImportModuleTests:
pkg_name
=
'pkg'
pkg_long_name
=
'{0}.__init__'
.
format
(
pkg_name
)
name
=
'{0}.mod'
.
format
(
pkg_name
)
with
util
.
mock_modules
(
pkg_long_name
,
name
)
as
mock
:
with
util
.
import_state
(
meta_path
=
[
mock
]):
with
test_
util
.
mock_modules
(
pkg_long_name
,
name
)
as
mock
:
with
test_
util
.
import_state
(
meta_path
=
[
mock
]):
module
=
self
.
init
.
import_module
(
name
)
self
.
assertEqual
(
module
.
__name__
,
name
)
...
...
@@ -40,16 +40,16 @@ class ImportModuleTests:
module_name
=
'mod'
absolute_name
=
'{0}.{1}'
.
format
(
pkg_name
,
module_name
)
relative_name
=
'.{0}'
.
format
(
module_name
)
with
util
.
mock_modules
(
pkg_long_name
,
absolute_name
)
as
mock
:
with
util
.
import_state
(
meta_path
=
[
mock
]):
with
test_
util
.
mock_modules
(
pkg_long_name
,
absolute_name
)
as
mock
:
with
test_
util
.
import_state
(
meta_path
=
[
mock
]):
self
.
init
.
import_module
(
pkg_name
)
module
=
self
.
init
.
import_module
(
relative_name
,
pkg_name
)
self
.
assertEqual
(
module
.
__name__
,
absolute_name
)
def
test_deep_relative_package_import
(
self
):
modules
=
[
'a.__init__'
,
'a.b.__init__'
,
'a.c'
]
with
util
.
mock_modules
(
*
modules
)
as
mock
:
with
util
.
import_state
(
meta_path
=
[
mock
]):
with
test_
util
.
mock_modules
(
*
modules
)
as
mock
:
with
test_
util
.
import_state
(
meta_path
=
[
mock
]):
self
.
init
.
import_module
(
'a'
)
self
.
init
.
import_module
(
'a.b'
)
module
=
self
.
init
.
import_module
(
'..c'
,
'a.b'
)
...
...
@@ -61,8 +61,8 @@ class ImportModuleTests:
pkg_name
=
'pkg'
pkg_long_name
=
'{0}.__init__'
.
format
(
pkg_name
)
name
=
'{0}.mod'
.
format
(
pkg_name
)
with
util
.
mock_modules
(
pkg_long_name
,
name
)
as
mock
:
with
util
.
import_state
(
meta_path
=
[
mock
]):
with
test_
util
.
mock_modules
(
pkg_long_name
,
name
)
as
mock
:
with
test_
util
.
import_state
(
meta_path
=
[
mock
]):
self
.
init
.
import_module
(
pkg_name
)
module
=
self
.
init
.
import_module
(
name
,
pkg_name
)
self
.
assertEqual
(
module
.
__name__
,
name
)
...
...
@@ -86,16 +86,15 @@ class ImportModuleTests:
b_load_count
+=
1
code
=
{
'a'
:
load_a
,
'a.b'
:
load_b
}
modules
=
[
'a.__init__'
,
'a.b'
]
with
util
.
mock_modules
(
*
modules
,
module_code
=
code
)
as
mock
:
with
util
.
import_state
(
meta_path
=
[
mock
]):
with
test_
util
.
mock_modules
(
*
modules
,
module_code
=
code
)
as
mock
:
with
test_
util
.
import_state
(
meta_path
=
[
mock
]):
self
.
init
.
import_module
(
'a.b'
)
self
.
assertEqual
(
b_load_count
,
1
)
class
Frozen_ImportModuleTests
(
ImportModuleTests
,
unittest
.
TestCase
):
init
=
frozen_init
class
Source_ImportModuleTests
(
ImportModuleTests
,
unittest
.
TestCase
):
init
=
source_init
(
Frozen_ImportModuleTests
,
Source_ImportModuleTests
)
=
test_util
.
test_both
(
ImportModuleTests
,
init
=
init
)
class
FindLoaderTests
:
...
...
@@ -107,7 +106,7 @@ class FindLoaderTests:
def
test_sys_modules
(
self
):
# If a module with __loader__ is in sys.modules, then return it.
name
=
'some_mod'
with
util
.
uncache
(
name
):
with
test_
util
.
uncache
(
name
):
module
=
types
.
ModuleType
(
name
)
loader
=
'a loader!'
module
.
__loader__
=
loader
...
...
@@ -120,7 +119,7 @@ class FindLoaderTests:
def
test_sys_modules_loader_is_None
(
self
):
# If sys.modules[name].__loader__ is None, raise ValueError.
name
=
'some_mod'
with
util
.
uncache
(
name
):
with
test_
util
.
uncache
(
name
):
module
=
types
.
ModuleType
(
name
)
module
.
__loader__
=
None
sys
.
modules
[
name
]
=
module
...
...
@@ -133,7 +132,7 @@ class FindLoaderTests:
# Should raise ValueError
# Issue #17099
name
=
'some_mod'
with
util
.
uncache
(
name
):
with
test_
util
.
uncache
(
name
):
module
=
types
.
ModuleType
(
name
)
try
:
del
module
.
__loader__
...
...
@@ -148,8 +147,8 @@ class FindLoaderTests:
def
test_success
(
self
):
# Return the loader found on sys.meta_path.
name
=
'some_mod'
with
util
.
uncache
(
name
):
with
util
.
import_state
(
meta_path
=
[
self
.
FakeMetaFinder
]):
with
test_
util
.
uncache
(
name
):
with
test_
util
.
import_state
(
meta_path
=
[
self
.
FakeMetaFinder
]):
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
'ignore'
,
DeprecationWarning
)
self
.
assertEqual
((
name
,
None
),
self
.
init
.
find_loader
(
name
))
...
...
@@ -158,8 +157,8 @@ class FindLoaderTests:
# Searching on a path should work.
name
=
'some_mod'
path
=
'path to some place'
with
util
.
uncache
(
name
):
with
util
.
import_state
(
meta_path
=
[
self
.
FakeMetaFinder
]):
with
test_
util
.
uncache
(
name
):
with
test_
util
.
import_state
(
meta_path
=
[
self
.
FakeMetaFinder
]):
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
'ignore'
,
DeprecationWarning
)
self
.
assertEqual
((
name
,
path
),
...
...
@@ -171,11 +170,10 @@ class FindLoaderTests:
warnings
.
simplefilter
(
'ignore'
,
DeprecationWarning
)
self
.
assertIsNone
(
self
.
init
.
find_loader
(
'nevergoingtofindthismodule'
))
class
Frozen_FindLoaderTests
(
FindLoaderTests
,
unittest
.
TestCase
):
init
=
frozen_init
class
Source_FindLoaderTests
(
FindLoaderTests
,
unittest
.
TestCase
):
init
=
source_init
(
Frozen_FindLoaderTests
,
Source_FindLoaderTests
)
=
test_util
.
test_both
(
FindLoaderTests
,
init
=
init
)
class
ReloadTests
:
...
...
@@ -195,10 +193,10 @@ class ReloadTests:
module
=
type
(
sys
)(
'top_level'
)
module
.
spam
=
3
sys
.
modules
[
'top_level'
]
=
module
mock
=
util
.
mock_modules
(
'top_level'
,
module_code
=
{
'top_level'
:
code
})
mock
=
test_
util
.
mock_modules
(
'top_level'
,
module_code
=
{
'top_level'
:
code
})
with
mock
:
with
util
.
import_state
(
meta_path
=
[
mock
]):
with
test_
util
.
import_state
(
meta_path
=
[
mock
]):
module
=
self
.
init
.
import_module
(
'top_level'
)
reloaded
=
self
.
init
.
reload
(
module
)
actual
=
sys
.
modules
[
'top_level'
]
...
...
@@ -230,7 +228,7 @@ class ReloadTests:
def
test_reload_location_changed
(
self
):
name
=
'spam'
with
support
.
temp_cwd
(
None
)
as
cwd
:
with
util
.
uncache
(
'spam'
):
with
test_
util
.
uncache
(
'spam'
):
with
support
.
DirsOnSysPath
(
cwd
):
# Start as a plain module.
self
.
init
.
invalidate_caches
()
...
...
@@ -281,7 +279,7 @@ class ReloadTests:
def
test_reload_namespace_changed
(
self
):
name
=
'spam'
with
support
.
temp_cwd
(
None
)
as
cwd
:
with
util
.
uncache
(
'spam'
):
with
test_
util
.
uncache
(
'spam'
):
with
support
.
DirsOnSysPath
(
cwd
):
# Start as a namespace package.
self
.
init
.
invalidate_caches
()
...
...
@@ -338,20 +336,16 @@ class ReloadTests:
# See #19851.
name
=
'spam'
subname
=
'ham'
with
util
.
temp_module
(
name
,
pkg
=
True
)
as
pkg_dir
:
fullname
,
_
=
util
.
submodule
(
name
,
subname
,
pkg_dir
)
with
test_
util
.
temp_module
(
name
,
pkg
=
True
)
as
pkg_dir
:
fullname
,
_
=
test_
util
.
submodule
(
name
,
subname
,
pkg_dir
)
ham
=
self
.
init
.
import_module
(
fullname
)
reloaded
=
self
.
init
.
reload
(
ham
)
self
.
assertIs
(
reloaded
,
ham
)
class
Frozen_ReloadTests
(
ReloadTests
,
unittest
.
TestCase
):
init
=
frozen_init
util
=
frozen_util
class
Source_ReloadTests
(
ReloadTests
,
unittest
.
TestCase
):
init
=
source_init
util
=
source_util
(
Frozen_ReloadTests
,
Source_ReloadTests
)
=
test_util
.
test_both
(
ReloadTests
,
init
=
init
,
util
=
util
)
class
InvalidateCacheTests
:
...
...
@@ -384,11 +378,10 @@ class InvalidateCacheTests:
self
.
addCleanup
(
lambda
:
sys
.
path_importer_cache
.
__delitem__
(
key
))
self
.
init
.
invalidate_caches
()
# Shouldn't trigger an exception.
class
Frozen_InvalidateCacheTests
(
InvalidateCacheTests
,
unittest
.
TestCase
):
init
=
frozen_init
class
Source_InvalidateCacheTests
(
InvalidateCacheTests
,
unittest
.
TestCase
):
init
=
source_init
(
Frozen_InvalidateCacheTests
,
Source_InvalidateCacheTests
)
=
test_util
.
test_both
(
InvalidateCacheTests
,
init
=
init
)
class
FrozenImportlibTests
(
unittest
.
TestCase
):
...
...
@@ -398,6 +391,7 @@ class FrozenImportlibTests(unittest.TestCase):
# Can't do an isinstance() check since separate copies of importlib
# may have been used for import, so just check the name is not for the
# frozen loader.
source_init
=
init
[
'Source'
]
self
.
assertNotEqual
(
source_init
.
__loader__
.
__class__
.
__name__
,
'FrozenImporter'
)
...
...
@@ -426,11 +420,10 @@ class StartupTests:
elif
self
.
machinery
.
FrozenImporter
.
find_module
(
name
):
self
.
assertIsNot
(
module
.
__spec__
,
None
)
class
Frozen_StartupTests
(
StartupTests
,
unittest
.
TestCase
):
machinery
=
frozen_machinery
class
Source_StartupTests
(
StartupTests
,
unittest
.
TestCase
):
machinery
=
source_machinery
(
Frozen_StartupTests
,
Source_StartupTests
)
=
test_util
.
test_both
(
StartupTests
,
machinery
=
machinery
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/test_locks.py
View file @
3497c0bf
from
.
import
util
frozen_init
,
source_init
=
util
.
import_importlib
(
'importlib'
)
frozen_bootstrap
=
frozen_init
.
_bootstrap
source_bootstrap
=
source_init
.
_bootstrap
from
.
import
util
as
test_util
init
=
test_util
.
import_importlib
(
'importlib'
)
import
sys
import
time
...
...
@@ -33,13 +32,16 @@ if threading is not None:
# _release_save() unsupported
test_release_save_unacquired
=
None
class
Frozen_ModuleLockAsRLockTests
(
ModuleLockAsRLockTests
,
lock_tests
.
RLockTests
):
LockType
=
frozen_bootstrap
.
_ModuleLock
class
Source_ModuleLockAsRLockTests
(
ModuleLockAsRLockTests
,
lock_tests
.
RLockTests
):
LockType
=
source_bootstrap
.
_ModuleLock
LOCK_TYPES
=
{
kind
:
splitinit
.
_bootstrap
.
_ModuleLock
for
kind
,
splitinit
in
init
.
items
()}
(
Frozen_ModuleLockAsRLockTests
,
Source_ModuleLockAsRLockTests
)
=
test_util
.
test_both
(
ModuleLockAsRLockTests
,
lock_tests
.
RLockTests
,
LockType
=
LOCK_TYPES
)
else
:
LOCK_TYPES
=
{}
class
Frozen_ModuleLockAsRLockTests
(
unittest
.
TestCase
):
pass
...
...
@@ -47,6 +49,7 @@ else:
pass
@
unittest
.
skipUnless
(
threading
,
"threads needed for this test"
)
class
DeadlockAvoidanceTests
:
def
setUp
(
self
):
...
...
@@ -106,19 +109,22 @@ class DeadlockAvoidanceTests:
self
.
assertEqual
(
results
.
count
((
True
,
False
)),
0
)
self
.
assertEqual
(
results
.
count
((
True
,
True
)),
len
(
results
))
@
unittest
.
skipUnless
(
threading
,
"threads needed for this test"
)
class
Frozen_DeadlockAvoidanceTests
(
DeadlockAvoidanceTests
,
unittest
.
TestCase
):
LockType
=
frozen_bootstrap
.
_ModuleLock
DeadlockError
=
frozen_bootstrap
.
_DeadlockError
@
unittest
.
skipUnless
(
threading
,
"threads needed for this test"
)
class
Source_DeadlockAvoidanceTests
(
DeadlockAvoidanceTests
,
unittest
.
TestCase
):
LockType
=
source_bootstrap
.
_ModuleLock
DeadlockError
=
source_bootstrap
.
_DeadlockError
DEADLOCK_ERRORS
=
{
kind
:
splitinit
.
_bootstrap
.
_DeadlockError
for
kind
,
splitinit
in
init
.
items
()}
(
Frozen_DeadlockAvoidanceTests
,
Source_DeadlockAvoidanceTests
)
=
test_util
.
test_both
(
DeadlockAvoidanceTests
,
LockType
=
LOCK_TYPES
,
DeadlockError
=
DEADLOCK_ERRORS
)
class
LifetimeTests
:
@
property
def
bootstrap
(
self
):
return
self
.
init
.
_bootstrap
def
test_lock_lifetime
(
self
):
name
=
"xyzzy"
self
.
assertNotIn
(
name
,
self
.
bootstrap
.
_module_locks
)
...
...
@@ -135,11 +141,10 @@ class LifetimeTests:
self
.
assertEqual
(
0
,
len
(
self
.
bootstrap
.
_module_locks
),
self
.
bootstrap
.
_module_locks
)
class
Frozen_LifetimeTests
(
LifetimeTests
,
unittest
.
TestCase
):
bootstrap
=
frozen_bootstrap
class
Source_LifetimeTests
(
LifetimeTests
,
unittest
.
TestCase
):
bootstrap
=
source_bootstrap
(
Frozen_LifetimeTests
,
Source_LifetimeTests
)
=
test_util
.
test_both
(
LifetimeTests
,
init
=
init
)
@
support
.
reap_threads
...
...
Lib/test/test_importlib/test_spec.py
View file @
3497c0bf
from
.
import
util
from
.
import
util
as
test_util
frozen_init
,
source_init
=
util
.
import_importlib
(
'importlib'
)
frozen_bootstrap
=
frozen_init
.
_bootstrap
source_bootstrap
=
source_init
.
_bootstrap
frozen_machinery
,
source_machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
frozen_util
,
source_util
=
util
.
import_importlib
(
'importlib.util'
)
init
=
test_util
.
import_importlib
(
'importlib'
)
machinery
=
test_util
.
import_importlib
(
'importlib.machinery'
)
util
=
test_util
.
import_importlib
(
'importlib.util'
)
import
os.path
from
test.support
import
CleanImport
...
...
@@ -52,6 +50,8 @@ class LegacyLoader(TestLoader):
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
"ignore"
,
DeprecationWarning
)
frozen_util
=
util
[
'Frozen'
]
@
frozen_util
.
module_for_loader
def
load_module
(
self
,
module
):
module
.
ham
=
self
.
HAM
...
...
@@ -221,18 +221,17 @@ class ModuleSpecTests:
self
.
assertEqual
(
self
.
loc_spec
.
cached
,
'spam.pyc'
)
class
Frozen_ModuleSpecTests
(
ModuleSpecTests
,
unittest
.
TestCase
):
util
=
frozen_util
machinery
=
frozen_machinery
class
Source_ModuleSpecTests
(
ModuleSpecTests
,
unittest
.
TestCase
):
util
=
source_util
machinery
=
source_machinery
(
Frozen_ModuleSpecTests
,
Source_ModuleSpecTests
)
=
test_util
.
test_both
(
ModuleSpecTests
,
util
=
util
,
machinery
=
machinery
)
class
ModuleSpecMethodsTests
:
@
property
def
bootstrap
(
self
):
return
self
.
init
.
_bootstrap
def
setUp
(
self
):
self
.
name
=
'spam'
self
.
path
=
'spam.py'
...
...
@@ -528,20 +527,18 @@ class ModuleSpecMethodsTests:
self
.
assertIs
(
installed
,
loaded
)
class
Frozen_ModuleSpecMethodsTests
(
ModuleSpecMethodsTests
,
unittest
.
TestCase
):
bootstrap
=
frozen_bootstrap
machinery
=
frozen_machinery
util
=
frozen_util
class
Source_ModuleSpecMethodsTests
(
ModuleSpecMethodsTests
,
unittest
.
TestCase
):
bootstrap
=
source_bootstrap
machinery
=
source_machinery
util
=
source_util
(
Frozen_ModuleSpecMethodsTests
,
Source_ModuleSpecMethodsTests
)
=
test_util
.
test_both
(
ModuleSpecMethodsTests
,
init
=
init
,
util
=
util
,
machinery
=
machinery
)
class
ModuleReprTests
:
@
property
def
bootstrap
(
self
):
return
self
.
init
.
_bootstrap
def
setUp
(
self
):
self
.
module
=
type
(
os
)(
'spam'
)
self
.
spec
=
self
.
machinery
.
ModuleSpec
(
'spam'
,
TestLoader
())
...
...
@@ -625,16 +622,10 @@ class ModuleReprTests:
self
.
assertEqual
(
modrepr
,
'<module {!r}>'
.
format
(
'spam'
))
class
Frozen_ModuleReprTests
(
ModuleReprTests
,
unittest
.
TestCase
):
bootstrap
=
frozen_bootstrap
machinery
=
frozen_machinery
util
=
frozen_util
class
Source_ModuleReprTests
(
ModuleReprTests
,
unittest
.
TestCase
):
bootstrap
=
source_bootstrap
machinery
=
source_machinery
util
=
source_util
(
Frozen_ModuleReprTests
,
Source_ModuleReprTests
)
=
test_util
.
test_both
(
ModuleReprTests
,
init
=
init
,
util
=
util
,
machinery
=
machinery
)
class
FactoryTests
:
...
...
@@ -787,7 +778,7 @@ class FactoryTests:
# spec_from_file_location()
def
test_spec_from_file_location_default
(
self
):
if
self
.
machinery
is
source_machinery
:
if
self
.
machinery
is
machinery
[
'Source'
]
:
raise
unittest
.
SkipTest
(
'not sure why this is breaking...'
)
spec
=
self
.
util
.
spec_from_file_location
(
self
.
name
,
self
.
path
)
...
...
@@ -947,11 +938,6 @@ class FactoryTests:
self
.
assertTrue
(
spec
.
has_location
)
class
Frozen_FactoryTests
(
FactoryTests
,
unittest
.
TestCase
):
util
=
frozen_util
machinery
=
frozen_machinery
class
Source_FactoryTests
(
FactoryTests
,
unittest
.
TestCase
):
util
=
source_util
machinery
=
source_machinery
(
Frozen_FactoryTests
,
Source_FactoryTests
)
=
test_util
.
test_both
(
FactoryTests
,
util
=
util
,
machinery
=
machinery
)
Lib/test/test_importlib/test_util.py
View file @
3497c0bf
from
importlib
import
util
import
importlib.
util
from
.
import
util
as
test_util
frozen_init
,
source_
init
=
test_util
.
import_importlib
(
'importlib'
)
frozen_machinery
,
source_
machinery
=
test_util
.
import_importlib
(
'importlib.machinery'
)
frozen_util
,
source_
util
=
test_util
.
import_importlib
(
'importlib.util'
)
init
=
test_util
.
import_importlib
(
'importlib'
)
machinery
=
test_util
.
import_importlib
(
'importlib.machinery'
)
util
=
test_util
.
import_importlib
(
'importlib.util'
)
import
os
import
sys
...
...
@@ -32,8 +32,10 @@ class DecodeSourceBytesTests:
self
.
assertEqual
(
self
.
util
.
decode_source
(
source_bytes
),
'
\
n
'
.
join
([
self
.
source
,
self
.
source
]))
Frozen_DecodeSourceBytesTests
,
Source_DecodeSourceBytesTests
=
test_util
.
test_both
(
DecodeSourceBytesTests
,
util
=
[
frozen_util
,
source_util
])
(
Frozen_DecodeSourceBytesTests
,
Source_DecodeSourceBytesTests
)
=
test_util
.
test_both
(
DecodeSourceBytesTests
,
util
=
util
)
class
ModuleForLoaderTests
:
...
...
@@ -161,8 +163,10 @@ class ModuleForLoaderTests:
self
.
assertIs
(
module
.
__loader__
,
loader
)
self
.
assertEqual
(
module
.
__package__
,
name
)
Frozen_ModuleForLoaderTests
,
Source_ModuleForLoaderTests
=
test_util
.
test_both
(
ModuleForLoaderTests
,
util
=
[
frozen_util
,
source_util
])
(
Frozen_ModuleForLoaderTests
,
Source_ModuleForLoaderTests
)
=
test_util
.
test_both
(
ModuleForLoaderTests
,
util
=
util
)
class
SetPackageTests
:
...
...
@@ -222,18 +226,25 @@ class SetPackageTests:
self
.
assertEqual
(
wrapped
.
__name__
,
fxn
.
__name__
)
self
.
assertEqual
(
wrapped
.
__qualname__
,
fxn
.
__qualname__
)
Frozen_SetPackageTests
,
Source_SetPackageTests
=
test_util
.
test_both
(
SetPackageTests
,
util
=
[
frozen_util
,
source_util
])
(
Frozen_SetPackageTests
,
Source_SetPackageTests
)
=
test_util
.
test_both
(
SetPackageTests
,
util
=
util
)
class
SetLoaderTests
:
"""Tests importlib.util.set_loader()."""
class
DummyLoader
:
@
util
.
set_loader
def
load_module
(
self
,
module
):
return
self
.
module
@
property
def
DummyLoader
(
self
):
# Set DummyLoader on the class lazily.
class
DummyLoader
:
@
self
.
util
.
set_loader
def
load_module
(
self
,
module
):
return
self
.
module
self
.
__class__
.
DummyLoader
=
DummyLoader
return
DummyLoader
def
test_no_attribute
(
self
):
loader
=
self
.
DummyLoader
()
...
...
@@ -262,17 +273,10 @@ class SetLoaderTests:
warnings
.
simplefilter
(
'ignore'
,
DeprecationWarning
)
self
.
assertEqual
(
42
,
loader
.
load_module
(
'blah'
).
__loader__
)
class
Frozen_SetLoaderTests
(
SetLoaderTests
,
unittest
.
TestCase
):
class
DummyLoader
:
@
frozen_util
.
set_loader
def
load_module
(
self
,
module
):
return
self
.
module
class
Source_SetLoaderTests
(
SetLoaderTests
,
unittest
.
TestCase
):
class
DummyLoader
:
@
source_util
.
set_loader
def
load_module
(
self
,
module
):
return
self
.
module
(
Frozen_SetLoaderTests
,
Source_SetLoaderTests
)
=
test_util
.
test_both
(
SetLoaderTests
,
util
=
util
)
class
ResolveNameTests
:
...
...
@@ -307,9 +311,10 @@ class ResolveNameTests:
with
self
.
assertRaises
(
ValueError
):
self
.
util
.
resolve_name
(
'..bacon'
,
'spam'
)
Frozen_ResolveNameTests
,
Source_ResolveNameTests
=
test_util
.
test_both
(
ResolveNameTests
,
util
=
[
frozen_util
,
source_util
])
(
Frozen_ResolveNameTests
,
Source_ResolveNameTests
)
=
test_util
.
test_both
(
ResolveNameTests
,
util
=
util
)
class
FindSpecTests
:
...
...
@@ -446,15 +451,10 @@ class FindSpecTests:
self
.
assertNotIn
(
fullname
,
sorted
(
sys
.
modules
))
class
Frozen_FindSpecTests
(
FindSpecTests
,
unittest
.
TestCase
):
init
=
frozen_init
machinery
=
frozen_machinery
util
=
frozen_util
class
Source_FindSpecTests
(
FindSpecTests
,
unittest
.
TestCase
):
init
=
source_init
machinery
=
source_machinery
util
=
source_util
(
Frozen_FindSpecTests
,
Source_FindSpecTests
)
=
test_util
.
test_both
(
FindSpecTests
,
init
=
init
,
util
=
util
,
machinery
=
machinery
)
class
MagicNumberTests
:
...
...
@@ -467,8 +467,10 @@ class MagicNumberTests:
# The magic number uses \r\n to come out wrong when splitting on lines.
self
.
assertTrue
(
self
.
util
.
MAGIC_NUMBER
.
endswith
(
b'
\
r
\
n
'
))
Frozen_MagicNumberTests
,
Source_MagicNumberTests
=
test_util
.
test_both
(
MagicNumberTests
,
util
=
[
frozen_util
,
source_util
])
(
Frozen_MagicNumberTests
,
Source_MagicNumberTests
)
=
test_util
.
test_both
(
MagicNumberTests
,
util
=
util
)
class
PEP3147Tests
:
...
...
@@ -583,9 +585,10 @@ class PEP3147Tests:
ValueError
,
self
.
util
.
source_from_cache
,
'/foo/bar/foo.cpython-32.foo.pyc'
)
Frozen_PEP3147Tests
,
Source_PEP3147Tests
=
test_util
.
test_both
(
PEP3147Tests
,
util
=
[
frozen_util
,
source_util
])
(
Frozen_PEP3147Tests
,
Source_PEP3147Tests
)
=
test_util
.
test_both
(
PEP3147Tests
,
util
=
util
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_importlib/test_windows.py
View file @
3497c0bf
from
.
import
util
frozen_machinery
,
source_machinery
=
util
.
import_importlib
(
'importlib.machinery'
)
from
.
import
util
as
test_util
machinery
=
test_
util
.
import_importlib
(
'importlib.machinery'
)
import
sys
import
unittest
...
...
@@ -19,11 +19,6 @@ class WindowsRegistryFinderTests:
self
.
assertIs
(
loader
,
None
)
class
Frozen_WindowsRegistryFinderTests
(
WindowsRegistryFinderTests
,
unittest
.
TestCase
):
machinery
=
frozen_machinery
class
Source_WindowsRegistryFinderTests
(
WindowsRegistryFinderTests
,
unittest
.
TestCase
):
machinery
=
source_machinery
(
Frozen_WindowsRegistryFinderTests
,
Source_WindowsRegistryFinderTests
)
=
test_util
.
test_both
(
WindowsRegistryFinderTests
,
machinery
=
machinery
)
Lib/test/test_importlib/util.py
View file @
3497c0bf
...
...
@@ -50,19 +50,36 @@ def import_importlib(module_name):
frozen
=
support
.
import_fresh_module
(
module_name
)
source
=
support
.
import_fresh_module
(
module_name
,
fresh
=
fresh
,
blocked
=
(
'_frozen_importlib'
,))
return
{
'Frozen'
:
frozen
,
'Source'
:
source
}
def
specialize_class
(
cls
,
kind
,
base
=
None
,
**
kwargs
):
# XXX Support passing in submodule names--load (and cache) them?
# That would clean up the test modules a bit more.
if
base
is
None
:
base
=
unittest
.
TestCase
elif
not
isinstance
(
base
,
type
):
base
=
base
[
kind
]
name
=
'{}_{}'
.
format
(
kind
,
cls
.
__name__
)
bases
=
(
cls
,
base
)
specialized
=
types
.
new_class
(
name
,
bases
)
specialized
.
__module__
=
cls
.
__module__
specialized
.
_NAME
=
cls
.
__name__
specialized
.
_KIND
=
kind
for
attr
,
values
in
kwargs
.
items
():
value
=
values
[
kind
]
setattr
(
specialized
,
attr
,
value
)
return
specialized
def
split_frozen
(
cls
,
base
=
None
,
**
kwargs
):
frozen
=
specialize_class
(
cls
,
'Frozen'
,
base
,
**
kwargs
)
source
=
specialize_class
(
cls
,
'Source'
,
base
,
**
kwargs
)
return
frozen
,
source
def
test_both
(
test_class
,
**
kwargs
):
frozen_tests
=
types
.
new_class
(
'Frozen_'
+
test_class
.
__name__
,
(
test_class
,
unittest
.
TestCase
))
source_tests
=
types
.
new_class
(
'Source_'
+
test_class
.
__name__
,
(
test_class
,
unittest
.
TestCase
))
frozen_tests
.
__module__
=
source_tests
.
__module__
=
test_class
.
__module__
for
attr
,
(
frozen_value
,
source_value
)
in
kwargs
.
items
():
setattr
(
frozen_tests
,
attr
,
frozen_value
)
setattr
(
source_tests
,
attr
,
source_value
)
return
frozen_tests
,
source_tests
def
test_both
(
test_class
,
base
=
None
,
**
kwargs
):
return
split_frozen
(
test_class
,
base
,
**
kwargs
)
CASE_INSENSITIVE_FS
=
True
...
...
@@ -75,8 +92,9 @@ if sys.platform not in ('win32', 'cygwin'):
if
not
os
.
path
.
exists
(
changed_name
):
CASE_INSENSITIVE_FS
=
False
_
,
source_importlib
=
import_importlib
(
'importlib'
)
__import__
=
staticmethod
(
builtins
.
__import__
),
staticmethod
(
source_importlib
.
__import__
)
source_importlib
=
import_importlib
(
'importlib'
)[
'Source'
]
__import__
=
{
'Frozen'
:
staticmethod
(
builtins
.
__import__
),
'Source'
:
staticmethod
(
source_importlib
.
__import__
)}
def
case_insensitive_tests
(
test
):
...
...
Misc/NEWS
View file @
3497c0bf
...
...
@@ -496,6 +496,8 @@ Tests
-
Issue
#
21097
:
Move
test_namespace_pkgs
into
test_importlib
.
-
Issue
#
21503
:
Use
test_both
()
consistently
in
test_importlib
.
-
Issue
#
20939
:
Avoid
various
network
test
failures
due
to
new
redirect
of
http
://
www
.
python
.
org
/
to
https
://
www
.
python
.
org
:
use
http
://
www
.
example
.
com
instead
.
...
...
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