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
37148b27
Commit
37148b27
authored
Jan 04, 2014
by
Eric Snow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #19708: Update pkgutil to use the new importer APIs.
parent
335e14dd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
15 deletions
+32
-15
Lib/pkgutil.py
Lib/pkgutil.py
+18
-2
Lib/test/test_pkgutil.py
Lib/test/test_pkgutil.py
+12
-13
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/pkgutil.py
View file @
37148b27
...
...
@@ -16,6 +16,21 @@ __all__ = [
'ImpImporter'
,
'ImpLoader'
,
'read_code'
,
'extend_path'
,
]
def
_get_spec
(
finder
,
name
):
"""Return the finder-specific module spec."""
# Works with legacy finders.
try
:
find_spec
=
finder
.
find_spec
except
AttributeError
:
loader
=
finder
.
find_module
(
name
)
if
loader
is
None
:
return
None
return
importlib
.
util
.
spec_from_loader
(
name
,
loader
)
else
:
return
find_spec
(
name
)
def
read_code
(
stream
):
# This helper is needed in order for the PEP 302 emulation to
# correctly handle compiled files
...
...
@@ -326,9 +341,10 @@ class ImpLoader:
self
.
source
=
self
.
_get_delegate
().
get_source
()
return
self
.
source
def
_get_delegate
(
self
):
return
ImpImporter
(
self
.
filename
).
find_module
(
'__init__'
)
finder
=
ImpImporter
(
self
.
filename
)
spec
=
_get_spec
(
finder
,
'__init__'
)
return
spec
.
loader
def
get_filename
(
self
,
fullname
=
None
):
fullname
=
self
.
_fix_name
(
fullname
)
...
...
Lib/test/test_pkgutil.py
View file @
37148b27
...
...
@@ -2,6 +2,7 @@ from test.support import run_unittest, unload, check_warnings
import
unittest
import
sys
import
importlib
from
importlib.util
import
spec_from_file_location
import
pkgutil
import
os
import
os.path
...
...
@@ -103,23 +104,20 @@ class PkgutilTests(unittest.TestCase):
class
PkgutilPEP302Tests
(
unittest
.
TestCase
):
class
MyTestLoader
(
object
):
def
load_module
(
self
,
fullname
):
# Create an empty module
mod
=
sys
.
modules
.
setdefault
(
fullname
,
types
.
ModuleType
(
fullname
))
mod
.
__file__
=
"<%s>"
%
self
.
__class__
.
__name__
mod
.
__loader__
=
self
# Make it a package
mod
.
__path__
=
[]
def
exec_module
(
self
,
mod
):
# Count how many times the module is reloaded
mod
.
__dict__
[
'loads'
]
=
mod
.
__dict__
.
get
(
'loads'
,
0
)
+
1
return
mod
mod
.
__dict__
[
'loads'
]
=
mod
.
__dict__
.
get
(
'loads'
,
0
)
+
1
def
get_data
(
self
,
path
):
return
"Hello, world!"
class
MyTestImporter
(
object
):
def
find_module
(
self
,
fullname
,
path
=
None
):
return
PkgutilPEP302Tests
.
MyTestLoader
()
def
find_spec
(
self
,
fullname
,
path
=
None
,
target
=
None
):
loader
=
PkgutilPEP302Tests
.
MyTestLoader
()
return
spec_from_file_location
(
fullname
,
'<%s>'
%
loader
.
__class__
.
__name__
,
loader
=
loader
,
submodule_search_locations
=
[])
def
setUp
(
self
):
sys
.
meta_path
.
insert
(
0
,
self
.
MyTestImporter
())
...
...
@@ -210,7 +208,8 @@ class ExtendPathTests(unittest.TestCase):
importers
=
list
(
iter_importers
(
fullname
))
expected_importer
=
get_importer
(
pathitem
)
for
finder
in
importers
:
loader
=
finder
.
find_module
(
fullname
)
spec
=
pkgutil
.
_get_spec
(
finder
,
fullname
)
loader
=
spec
.
loader
try
:
loader
=
loader
.
loader
except
AttributeError
:
...
...
@@ -221,7 +220,7 @@ class ExtendPathTests(unittest.TestCase):
self
.
assertEqual
(
finder
,
expected_importer
)
self
.
assertIsInstance
(
loader
,
importlib
.
machinery
.
SourceFileLoader
)
self
.
assertIsNone
(
finder
.
find_module
(
pkgname
))
self
.
assertIsNone
(
pkgutil
.
_get_spec
(
finder
,
pkgname
))
with
self
.
assertRaises
(
ImportError
):
list
(
iter_importers
(
'invalid.module'
))
...
...
Misc/NEWS
View file @
37148b27
...
...
@@ -257,6 +257,8 @@ Library
- Issue #19713: Move away from using find_module/load_module.
- Issue #19708: Update pkgutil to use the new importer APIs.
- Issue #19851: Fixed a regression in reloading sub-modules.
- ssl.create_default_context() sets OP_NO_COMPRESSION to prevent CRIME.
...
...
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