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
7aa21f75
Commit
7aa21f75
authored
Mar 15, 2009
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
A few more docstring/API cleanups for importlib.
parent
0e0d8a63
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
53 deletions
+55
-53
Lib/importlib/__init__.py
Lib/importlib/__init__.py
+1
-15
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+16
-12
Lib/importlib/abc.py
Lib/importlib/abc.py
+37
-25
Lib/importlib/test/import_/util.py
Lib/importlib/test/import_/util.py
+1
-1
No files found.
Lib/importlib/__init__.py
View file @
7aa21f75
...
@@ -26,20 +26,6 @@ import os
...
@@ -26,20 +26,6 @@ import os
import
re
import
re
import
tokenize
import
tokenize
# XXX Temporary functions that should eventually be removed.
def
_set__import__
():
"""Set __import__ to an instance of Import."""
global
original__import__
original__import__
=
__import__
__builtins__
[
'__import__'
]
=
_bootstrap
.
_import
def
_reset__import__
():
"""Set __import__ back to the original implementation (assumes
_set__import__ was called previously)."""
__builtins__
[
'__import__'
]
=
original__import__
# Bootstrap help #####################################################
# Bootstrap help #####################################################
def
_case_ok
(
directory
,
check
):
def
_case_ok
(
directory
,
check
):
...
@@ -116,7 +102,7 @@ marshal._r_long = _r_long
...
@@ -116,7 +102,7 @@ marshal._r_long = _r_long
# Public API #########################################################
# Public API #########################################################
__import__
=
_bootstrap
.
_import
from
._bootstrap
import
__import__
def
import_module
(
name
,
package
=
None
):
def
import_module
(
name
,
package
=
None
):
...
...
Lib/importlib/_bootstrap.py
View file @
7aa21f75
...
@@ -183,16 +183,16 @@ def _suffix_list(suffix_type):
...
@@ -183,16 +183,16 @@ def _suffix_list(suffix_type):
class
BuiltinImporter
:
class
BuiltinImporter
:
"""Meta path
loader
for built-in modules.
"""Meta path
import
for built-in modules.
All methods are either class or static methods
, allowing direct use of the
All methods are either class or static methods
to avoid the need to
class.
instantiate the
class.
"""
"""
@
classmethod
@
classmethod
def
find_module
(
cls
,
fullname
,
path
=
None
):
def
find_module
(
cls
,
fullname
,
path
=
None
):
"""
Try to f
ind the built-in module.
"""
F
ind the built-in module.
If 'path' is ever specified then the search is considered a failure.
If 'path' is ever specified then the search is considered a failure.
...
@@ -219,10 +219,10 @@ class BuiltinImporter:
...
@@ -219,10 +219,10 @@ class BuiltinImporter:
class
FrozenImporter
:
class
FrozenImporter
:
"""Meta path
class for importing
frozen modules.
"""Meta path
import for
frozen modules.
All methods are either class or static method
to allow direct use of the
All methods are either class or static method
s to avoid the need to
class.
instantiate the
class.
"""
"""
...
@@ -249,10 +249,13 @@ class FrozenImporter:
...
@@ -249,10 +249,13 @@ class FrozenImporter:
class
PyLoader
:
class
PyLoader
:
"""Loader base class for Python source.
"""Loader base class for Python source
code
.
Requires implementing the optional PEP 302 protocols as well as
Subclasses need to implement the methods:
source_path.
- source_path
- get_data
- is_package
"""
"""
...
@@ -595,7 +598,8 @@ class PathFinder:
...
@@ -595,7 +598,8 @@ class PathFinder:
@
classmethod
@
classmethod
def
find_module
(
cls
,
fullname
,
path
=
None
):
def
find_module
(
cls
,
fullname
,
path
=
None
):
"""Find the module on sys.path or 'path'."""
"""Find the module on sys.path or 'path' based on sys.path_hooks and
sys.path_importer_cache."""
if
not
path
:
if
not
path
:
path
=
sys
.
path
path
=
sys
.
path
for
entry
in
path
:
for
entry
in
path
:
...
@@ -857,7 +861,7 @@ def _gcd_import(name, package=None, level=0):
...
@@ -857,7 +861,7 @@ def _gcd_import(name, package=None, level=0):
return
module
return
module
def
_
import
(
name
,
globals
=
{},
locals
=
{},
fromlist
=
[],
level
=
0
):
def
_
_import__
(
name
,
globals
=
{},
locals
=
{},
fromlist
=
[],
level
=
0
):
"""Import a module.
"""Import a module.
The 'globals' argument is used to infer where the import is occuring from
The 'globals' argument is used to infer where the import is occuring from
...
...
Lib/importlib/abc.py
View file @
7aa21f75
...
@@ -7,13 +7,11 @@ import types
...
@@ -7,13 +7,11 @@ import types
class
Loader
(
metaclass
=
abc
.
ABCMeta
):
class
Loader
(
metaclass
=
abc
.
ABCMeta
):
"""Abstract base class for import loaders.
"""Abstract base class for import loaders."""
See PEP 302 for details.
"""
@
abc
.
abstractmethod
def
load_module
(
self
,
fullname
:
str
)
->
types
.
ModuleType
:
def
load_module
(
self
,
fullname
:
str
)
->
types
.
ModuleType
:
"""Abstract method which when implemented should load a module."""
raise
NotImplementedError
raise
NotImplementedError
Loader
.
register
(
machinery
.
BuiltinImporter
)
Loader
.
register
(
machinery
.
BuiltinImporter
)
...
@@ -22,14 +20,11 @@ Loader.register(machinery.FrozenImporter)
...
@@ -22,14 +20,11 @@ Loader.register(machinery.FrozenImporter)
class
Finder
(
metaclass
=
abc
.
ABCMeta
):
class
Finder
(
metaclass
=
abc
.
ABCMeta
):
"""Abstract base class for import finders.
"""Abstract base class for import finders."""
See PEP 302 for details.
"""
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
find_module
(
self
,
fullname
:
str
,
path
:[
str
]
=
None
)
->
Loader
:
def
find_module
(
self
,
fullname
:
str
,
path
:[
str
]
=
None
)
->
Loader
:
"""Abstract method which when implemented should find a module."""
raise
NotImplementedError
raise
NotImplementedError
Finder
.
register
(
machinery
.
BuiltinImporter
)
Finder
.
register
(
machinery
.
BuiltinImporter
)
...
@@ -37,16 +32,10 @@ Finder.register(machinery.FrozenImporter)
...
@@ -37,16 +32,10 @@ Finder.register(machinery.FrozenImporter)
Finder
.
register
(
machinery
.
PathFinder
)
Finder
.
register
(
machinery
.
PathFinder
)
class
Importer
(
Finder
,
Loader
):
"""Abstract base class for importers."""
class
ResourceLoader
(
Loader
):
class
ResourceLoader
(
Loader
):
"""Abstract base class for loaders which can return data from the
back-end
"""Abstract base class for loaders which can return data from the
ir
storage.
back-end
storage.
This ABC represents one of the optional protocols specified by PEP 302.
This ABC represents one of the optional protocols specified by PEP 302.
...
@@ -54,12 +43,15 @@ class ResourceLoader(Loader):
...
@@ -54,12 +43,15 @@ class ResourceLoader(Loader):
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
get_data
(
self
,
path
:
str
)
->
bytes
:
def
get_data
(
self
,
path
:
str
)
->
bytes
:
"""Abstract method which when implemented should return the bytes for
the specified path."""
raise
NotImplementedError
raise
NotImplementedError
class
InspectLoader
(
Loader
):
class
InspectLoader
(
Loader
):
"""Abstract base class for loaders which supports introspection.
"""Abstract base class for loaders which support inspection about the
modules they can load.
This ABC represents one of the optional protocols specified by PEP 302.
This ABC represents one of the optional protocols specified by PEP 302.
...
@@ -67,44 +59,64 @@ class InspectLoader(Loader):
...
@@ -67,44 +59,64 @@ class InspectLoader(Loader):
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
is_package
(
self
,
fullname
:
str
)
->
bool
:
def
is_package
(
self
,
fullname
:
str
)
->
bool
:
"""Abstract method which when implemented should return whether the
module is a package."""
return
NotImplementedError
return
NotImplementedError
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
get_code
(
self
,
fullname
:
str
)
->
types
.
CodeType
:
def
get_code
(
self
,
fullname
:
str
)
->
types
.
CodeType
:
"""Abstract method which when implemented should return the code object
for the module"""
return
NotImplementedError
return
NotImplementedError
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
get_source
(
self
,
fullname
:
str
)
->
str
:
def
get_source
(
self
,
fullname
:
str
)
->
str
:
"""Abstract method which should return the source code for the
module."""
return
NotImplementedError
return
NotImplementedError
class
PyLoader
(
_bootstrap
.
PyLoader
,
InspectLoader
):
class
PyLoader
(
_bootstrap
.
PyLoader
,
InspectLoader
):
"""Abstract base class t
hat implements the core parts needed to load Python
"""Abstract base class t
o assist in loading source code by requiring only
source code."""
back-end storage methods to be implemented.
# load_module and get_code are implemented.
The methods get_code, get_source, and load_module are implemented for the
user.
"""
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
source_path
(
self
,
fullname
:
str
)
->
object
:
def
source_path
(
self
,
fullname
:
str
)
->
object
:
"""Abstract method which when implemented should return the path to the
sourced code for the module."""
raise
NotImplementedError
raise
NotImplementedError
class
PyPycLoader
(
_bootstrap
.
PyPycLoader
,
PyLoader
):
class
PyPycLoader
(
_bootstrap
.
PyPycLoader
,
PyLoader
):
"""Abstract base class that implements the core parts needed to load Python
"""Abstract base class to assist in loading source and bytecode by
source and bytecode."""
requiring only back-end storage methods to be implemented.
The methods get_code, get_source, and load_module are implemented for the
user.
# Implements load_module and get_code.
"""
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
source_mtime
(
self
,
fullname
:
str
)
->
int
:
def
source_mtime
(
self
,
fullname
:
str
)
->
int
:
"""Abstract method which when implemented should return the
modification time for the source of the module."""
raise
NotImplementedError
raise
NotImplementedError
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
bytecode_path
(
self
,
fullname
:
str
)
->
object
:
def
bytecode_path
(
self
,
fullname
:
str
)
->
object
:
"""Abstract method which when implemented should return the path to the
bytecode for the module."""
raise
NotImplementedError
raise
NotImplementedError
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
write_bytecode
(
self
,
fullname
:
str
,
bytecode
:
bytes
):
def
write_bytecode
(
self
,
fullname
:
str
,
bytecode
:
bytes
):
"""Abstract method which when implemented should attempt to write the
bytecode for the module."""
raise
NotImplementedError
raise
NotImplementedError
Lib/importlib/test/import_/util.py
View file @
7aa21f75
...
@@ -10,7 +10,7 @@ def import_(*args, **kwargs):
...
@@ -10,7 +10,7 @@ def import_(*args, **kwargs):
if
using___import__
:
if
using___import__
:
return
__import__
(
*
args
,
**
kwargs
)
return
__import__
(
*
args
,
**
kwargs
)
else
:
else
:
return
importlib
.
_bootstrap
.
_
import
(
*
args
,
**
kwargs
)
return
importlib
.
_bootstrap
.
_
_import__
(
*
args
,
**
kwargs
)
def
importlib_only
(
fxn
):
def
importlib_only
(
fxn
):
...
...
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