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
2cf03a82
Commit
2cf03a82
authored
Mar 10, 2009
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement importlib.util.set_loader: a decorator to automatically set
__loader__ on modules.
parent
d43b30b0
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
34 additions
and
12 deletions
+34
-12
Doc/library/importlib.rst
Doc/library/importlib.rst
+9
-1
Lib/importlib/NOTES
Lib/importlib/NOTES
+0
-4
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+15
-3
Lib/importlib/test/builtin/test_loader.py
Lib/importlib/test/builtin/test_loader.py
+2
-1
Lib/importlib/test/extension/test_loader.py
Lib/importlib/test/extension/test_loader.py
+2
-0
Lib/importlib/test/frozen/test_loader.py
Lib/importlib/test/frozen/test_loader.py
+5
-3
Lib/importlib/util.py
Lib/importlib/util.py
+1
-0
No files found.
Doc/library/importlib.rst
View file @
2cf03a82
...
...
@@ -348,7 +348,15 @@ an :term:`importer`.
loader
should
initialize
as
specified
by
:
pep
:`
302
`.
..
function
::
set_package
(
method
)
..
function
::
set_loader
(
fxn
)
A
:
term
:`
decorator
`
for
a
:
term
:`
loader
`
to
set
the
:
attr
:`
__loader__
`
attribute
on
loaded
modules
.
If
the
attribute
is
already
set
the
decorator
does
nothing
.
It
is
assumed
that
the
first
positional
argument
to
the
wrapped
method
is
what
:
attr
:`
__loader__
`
should
be
set
to
.
..
function
::
set_package
(
fxn
)
A
:
term
:`
decorator
`
for
a
:
term
:`
loader
`
to
set
the
:
attr
:`
__package__
`
attribute
on
the
module
returned
by
the
loader
.
If
:
attr
:`
__package__
`
is
...
...
Lib/importlib/NOTES
View file @
2cf03a82
to do
/////
* Public API left to expose (w/ docs!)
+ util.set_loader
* Implement InspectLoader for BuiltinImporter and FrozenImporter.
+ Expose function to see if a frozen module is a package.
...
...
Lib/importlib/_bootstrap.py
View file @
2cf03a82
...
...
@@ -110,6 +110,17 @@ def set_package(fxn):
return
wrapper
def
set_loader
(
fxn
):
"""Set __loader__ on the returned module."""
def
wrapper
(
self
,
*
args
,
**
kwargs
):
module
=
fxn
(
self
,
*
args
,
**
kwargs
)
if
not
hasattr
(
module
,
'__loader__'
):
module
.
__loader__
=
self
return
module
wrap
(
wrapper
,
fxn
)
return
wrapper
class
BuiltinImporter
:
"""Meta path loader for built-in modules.
...
...
@@ -132,6 +143,7 @@ class BuiltinImporter:
@
classmethod
@
set_package
@
set_loader
def
load_module
(
cls
,
fullname
):
"""Load a built-in module."""
if
fullname
not
in
sys
.
builtin_module_names
:
...
...
@@ -161,6 +173,7 @@ class FrozenImporter:
@
classmethod
@
set_package
@
set_loader
def
load_module
(
cls
,
fullname
):
"""Load a frozen module."""
if
cls
.
find_module
(
fullname
)
is
None
:
...
...
@@ -249,13 +262,12 @@ class _ExtensionFileLoader:
@
check_name
@
set_package
@
set_loader
def
load_module
(
self
,
fullname
):
"""Load an extension module."""
is_reload
=
fullname
in
sys
.
modules
try
:
module
=
imp
.
load_dynamic
(
fullname
,
self
.
_path
)
module
.
__loader__
=
self
return
module
return
imp
.
load_dynamic
(
fullname
,
self
.
_path
)
except
:
if
not
is_reload
and
fullname
in
sys
.
modules
:
del
sys
.
modules
[
fullname
]
...
...
Lib/importlib/test/builtin/test_loader.py
View file @
2cf03a82
...
...
@@ -15,7 +15,8 @@ class LoaderTests(abc.LoaderTests):
assert
'errno'
in
sys
.
builtin_module_names
name
=
'errno'
verification
=
{
'__name__'
:
'errno'
,
'__package__'
:
''
}
verification
=
{
'__name__'
:
'errno'
,
'__package__'
:
''
,
'__loader__'
:
machinery
.
BuiltinImporter
}
def
verify
(
self
,
module
):
"""Verify that the module matches against what it should have."""
...
...
Lib/importlib/test/extension/test_loader.py
View file @
2cf03a82
...
...
@@ -24,6 +24,8 @@ class LoaderTests(abc.LoaderTests):
(
'__package__'
,
''
)]:
self
.
assertEqual
(
getattr
(
module
,
attr
),
value
)
self
.
assert_
(
ext_util
.
NAME
in
sys
.
modules
)
self
.
assert_
(
isinstance
(
module
.
__loader__
,
importlib
.
_ExtensionFileLoader
))
def
test_package
(
self
):
# Extensions are not found in packages.
...
...
Lib/importlib/test/frozen/test_loader.py
View file @
2cf03a82
...
...
@@ -9,7 +9,7 @@ class LoaderTests(abc.LoaderTests):
with
util
.
uncache
(
'__hello__'
):
module
=
machinery
.
FrozenImporter
.
load_module
(
'__hello__'
)
check
=
{
'__name__'
:
'__hello__'
,
'__file__'
:
'<frozen>'
,
'__package__'
:
''
}
'__package__'
:
''
,
'__loader__'
:
machinery
.
FrozenImporter
}
for
attr
,
value
in
check
.
items
():
self
.
assertEqual
(
getattr
(
module
,
attr
),
value
)
...
...
@@ -17,7 +17,8 @@ class LoaderTests(abc.LoaderTests):
with
util
.
uncache
(
'__phello__'
):
module
=
machinery
.
FrozenImporter
.
load_module
(
'__phello__'
)
check
=
{
'__name__'
:
'__phello__'
,
'__file__'
:
'<frozen>'
,
'__package__'
:
'__phello__'
,
'__path__'
:
[
'__phello__'
]}
'__package__'
:
'__phello__'
,
'__path__'
:
[
'__phello__'
],
'__loader__'
:
machinery
.
FrozenImporter
}
for
attr
,
value
in
check
.
items
():
attr_value
=
getattr
(
module
,
attr
)
self
.
assertEqual
(
attr_value
,
value
,
...
...
@@ -28,7 +29,8 @@ class LoaderTests(abc.LoaderTests):
with
util
.
uncache
(
'__phello__'
,
'__phello__.spam'
):
module
=
machinery
.
FrozenImporter
.
load_module
(
'__phello__.spam'
)
check
=
{
'__name__'
:
'__phello__.spam'
,
'__file__'
:
'<frozen>'
,
'__package__'
:
'__phello__'
}
'__package__'
:
'__phello__'
,
'__loader__'
:
machinery
.
FrozenImporter
}
for
attr
,
value
in
check
.
items
():
attr_value
=
getattr
(
module
,
attr
)
self
.
assertEqual
(
attr_value
,
value
,
...
...
Lib/importlib/util.py
View file @
2cf03a82
"""Utility code for constructing importers, etc."""
from
._bootstrap
import
module_for_loader
from
._bootstrap
import
set_loader
from
._bootstrap
import
set_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