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
3eeaa0a8
Commit
3eeaa0a8
authored
Mar 12, 2009
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make utility code in importlib._bootstrap private.
parent
9495f182
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
32 deletions
+32
-32
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+32
-32
No files found.
Lib/importlib/_bootstrap.py
View file @
3eeaa0a8
...
@@ -56,7 +56,7 @@ def _path_isdir(path):
...
@@ -56,7 +56,7 @@ def _path_isdir(path):
def
_path_without_ext
(
path
,
ext_type
):
def
_path_without_ext
(
path
,
ext_type
):
"""Replacement for os.path.splitext()[0]."""
"""Replacement for os.path.splitext()[0]."""
for
suffix
in
suffix_list
(
ext_type
):
for
suffix
in
_
suffix_list
(
ext_type
):
if
path
.
endswith
(
suffix
):
if
path
.
endswith
(
suffix
):
return
path
[:
-
len
(
suffix
)]
return
path
[:
-
len
(
suffix
)]
else
:
else
:
...
@@ -76,7 +76,7 @@ def _path_absolute(path):
...
@@ -76,7 +76,7 @@ def _path_absolute(path):
return
_path_join
(
_os
.
getcwd
(),
path
)
return
_path_join
(
_os
.
getcwd
(),
path
)
class
closing
:
class
_
closing
:
"""Simple replacement for contextlib.closing."""
"""Simple replacement for contextlib.closing."""
...
@@ -90,7 +90,7 @@ class closing:
...
@@ -90,7 +90,7 @@ class closing:
self
.
obj
.
close
()
self
.
obj
.
close
()
def
wrap
(
new
,
old
):
def
_
wrap
(
new
,
old
):
"""Simple substitute for functools.wraps."""
"""Simple substitute for functools.wraps."""
for
replace
in
[
'__module__'
,
'__name__'
,
'__doc__'
]:
for
replace
in
[
'__module__'
,
'__name__'
,
'__doc__'
]:
setattr
(
new
,
replace
,
getattr
(
old
,
replace
))
setattr
(
new
,
replace
,
getattr
(
old
,
replace
))
...
@@ -106,7 +106,7 @@ def set_package(fxn):
...
@@ -106,7 +106,7 @@ def set_package(fxn):
if
not
hasattr
(
module
,
'__path__'
):
if
not
hasattr
(
module
,
'__path__'
):
module
.
__package__
=
module
.
__package__
.
rpartition
(
'.'
)[
0
]
module
.
__package__
=
module
.
__package__
.
rpartition
(
'.'
)[
0
]
return
module
return
module
wrap
(
wrapper
,
fxn
)
_
wrap
(
wrapper
,
fxn
)
return
wrapper
return
wrapper
...
@@ -117,7 +117,7 @@ def set_loader(fxn):
...
@@ -117,7 +117,7 @@ def set_loader(fxn):
if
not
hasattr
(
module
,
'__loader__'
):
if
not
hasattr
(
module
,
'__loader__'
):
module
.
__loader__
=
self
module
.
__loader__
=
self
return
module
return
module
wrap
(
wrapper
,
fxn
)
_
wrap
(
wrapper
,
fxn
)
return
wrapper
return
wrapper
...
@@ -187,7 +187,7 @@ class FrozenImporter:
...
@@ -187,7 +187,7 @@ class FrozenImporter:
raise
raise
def
chained_path_hook
(
*
path_hooks
):
def
_
chained_path_hook
(
*
path_hooks
):
"""Create a closure which sequentially checks path hooks to see which ones
"""Create a closure which sequentially checks path hooks to see which ones
(if any) can work with a path."""
(if any) can work with a path."""
def
path_hook
(
entry
):
def
path_hook
(
entry
):
...
@@ -203,12 +203,12 @@ def chained_path_hook(*path_hooks):
...
@@ -203,12 +203,12 @@ def chained_path_hook(*path_hooks):
if
not
finders
:
if
not
finders
:
raise
ImportError
(
"no finder found"
)
raise
ImportError
(
"no finder found"
)
else
:
else
:
return
ChainedFinder
(
*
finders
)
return
_
ChainedFinder
(
*
finders
)
return
path_hook
return
path_hook
class
ChainedFinder
:
class
_
ChainedFinder
:
"""Finder that sequentially calls other finders."""
"""Finder that sequentially calls other finders."""
...
@@ -224,7 +224,7 @@ class ChainedFinder:
...
@@ -224,7 +224,7 @@ class ChainedFinder:
return
None
return
None
def
check_name
(
method
):
def
_
check_name
(
method
):
"""Decorator to verify that the module being requested matches the one the
"""Decorator to verify that the module being requested matches the one the
loader can handle.
loader can handle.
...
@@ -236,7 +236,7 @@ def check_name(method):
...
@@ -236,7 +236,7 @@ def check_name(method):
if
self
.
_name
!=
name
:
if
self
.
_name
!=
name
:
raise
ImportError
(
"loader cannot handle %s"
%
name
)
raise
ImportError
(
"loader cannot handle %s"
%
name
)
return
method
(
self
,
name
,
*
args
,
**
kwargs
)
return
method
(
self
,
name
,
*
args
,
**
kwargs
)
wrap
(
inner
,
method
)
_
wrap
(
inner
,
method
)
return
inner
return
inner
...
@@ -260,7 +260,7 @@ class _ExtensionFileLoader:
...
@@ -260,7 +260,7 @@ class _ExtensionFileLoader:
if
is_pkg
:
if
is_pkg
:
raise
ValueError
(
"extension modules cannot be packages"
)
raise
ValueError
(
"extension modules cannot be packages"
)
@
check_name
@
_
check_name
@
set_package
@
set_package
@
set_loader
@
set_loader
def
load_module
(
self
,
fullname
):
def
load_module
(
self
,
fullname
):
...
@@ -273,23 +273,23 @@ class _ExtensionFileLoader:
...
@@ -273,23 +273,23 @@ class _ExtensionFileLoader:
del
sys
.
modules
[
fullname
]
del
sys
.
modules
[
fullname
]
raise
raise
@
check_name
@
_
check_name
def
is_package
(
self
,
fullname
):
def
is_package
(
self
,
fullname
):
"""Return False as an extension module can never be a package."""
"""Return False as an extension module can never be a package."""
return
False
return
False
@
check_name
@
_
check_name
def
get_code
(
self
,
fullname
):
def
get_code
(
self
,
fullname
):
"""Return None as an extension module cannot create a code object."""
"""Return None as an extension module cannot create a code object."""
return
None
return
None
@
check_name
@
_
check_name
def
get_source
(
self
,
fullname
):
def
get_source
(
self
,
fullname
):
"""Return None as extension modules have no source code."""
"""Return None as extension modules have no source code."""
return
None
return
None
def
suffix_list
(
suffix_type
):
def
_
suffix_list
(
suffix_type
):
"""Return a list of file suffixes based on the imp file type."""
"""Return a list of file suffixes based on the imp file type."""
return
[
suffix
[
0
]
for
suffix
in
imp
.
get_suffixes
()
return
[
suffix
[
0
]
for
suffix
in
imp
.
get_suffixes
()
if
suffix
[
2
]
==
suffix_type
]
if
suffix
[
2
]
==
suffix_type
]
...
@@ -323,7 +323,7 @@ def module_for_loader(fxn):
...
@@ -323,7 +323,7 @@ def module_for_loader(fxn):
if
not
is_reload
:
if
not
is_reload
:
del
sys
.
modules
[
fullname
]
del
sys
.
modules
[
fullname
]
raise
raise
wrap
(
decorated
,
fxn
)
_
wrap
(
decorated
,
fxn
)
return
decorated
return
decorated
...
@@ -484,21 +484,21 @@ class PyFileLoader(PyLoader):
...
@@ -484,21 +484,21 @@ class PyFileLoader(PyLoader):
def
_find_path
(
self
,
ext_type
):
def
_find_path
(
self
,
ext_type
):
"""Find a path from the base path and the specified extension type that
"""Find a path from the base path and the specified extension type that
exists, returning None if one is not found."""
exists, returning None if one is not found."""
for
suffix
in
suffix_list
(
ext_type
):
for
suffix
in
_
suffix_list
(
ext_type
):
path
=
self
.
_base_path
+
suffix
path
=
self
.
_base_path
+
suffix
if
_path_exists
(
path
):
if
_path_exists
(
path
):
return
path
return
path
else
:
else
:
return
None
return
None
@
check_name
@
_
check_name
def
source_path
(
self
,
fullname
):
def
source_path
(
self
,
fullname
):
"""Return the path to an existing source file for the module, or None
"""Return the path to an existing source file for the module, or None
if one cannot be found."""
if one cannot be found."""
# Not a property so that it is easy to override.
# Not a property so that it is easy to override.
return
self
.
_find_path
(
imp
.
PY_SOURCE
)
return
self
.
_find_path
(
imp
.
PY_SOURCE
)
@
check_name
@
_
check_name
def
get_source
(
self
,
fullname
):
def
get_source
(
self
,
fullname
):
"""Return the source for the module as a string.
"""Return the source for the module as a string.
...
@@ -510,7 +510,7 @@ class PyFileLoader(PyLoader):
...
@@ -510,7 +510,7 @@ class PyFileLoader(PyLoader):
if
source_path
is
None
:
if
source_path
is
None
:
return
None
return
None
import
tokenize
import
tokenize
with
closing
(
_io
.
FileIO
(
source_path
,
'r'
))
as
file
:
# Assuming bytes.
with
_
closing
(
_io
.
FileIO
(
source_path
,
'r'
))
as
file
:
# Assuming bytes.
encoding
,
lines
=
tokenize
.
detect_encoding
(
file
.
readline
)
encoding
,
lines
=
tokenize
.
detect_encoding
(
file
.
readline
)
# XXX Will fail when passed to compile() if the encoding is
# XXX Will fail when passed to compile() if the encoding is
# anything other than UTF-8.
# anything other than UTF-8.
...
@@ -521,7 +521,7 @@ class PyFileLoader(PyLoader):
...
@@ -521,7 +521,7 @@ class PyFileLoader(PyLoader):
"""Return the data from path as raw bytes."""
"""Return the data from path as raw bytes."""
return
_io
.
FileIO
(
path
,
'r'
).
read
()
# Assuming bytes.
return
_io
.
FileIO
(
path
,
'r'
).
read
()
# Assuming bytes.
@
check_name
@
_
check_name
def
is_package
(
self
,
fullname
):
def
is_package
(
self
,
fullname
):
"""Return a boolean based on whether the module is a package.
"""Return a boolean based on whether the module is a package.
...
@@ -536,7 +536,7 @@ class PyPycFileLoader(PyPycLoader, PyFileLoader):
...
@@ -536,7 +536,7 @@ class PyPycFileLoader(PyPycLoader, PyFileLoader):
"""Load a module from a source or bytecode file."""
"""Load a module from a source or bytecode file."""
@
check_name
@
_
check_name
def
source_mtime
(
self
,
name
):
def
source_mtime
(
self
,
name
):
"""Return the modification time of the source for the specified
"""Return the modification time of the source for the specified
module."""
module."""
...
@@ -545,14 +545,14 @@ class PyPycFileLoader(PyPycLoader, PyFileLoader):
...
@@ -545,14 +545,14 @@ class PyPycFileLoader(PyPycLoader, PyFileLoader):
return
None
return
None
return
int
(
_os
.
stat
(
source_path
).
st_mtime
)
return
int
(
_os
.
stat
(
source_path
).
st_mtime
)
@
check_name
@
_
check_name
def
bytecode_path
(
self
,
fullname
):
def
bytecode_path
(
self
,
fullname
):
"""Return the path to a bytecode file, or None if one does not
"""Return the path to a bytecode file, or None if one does not
exist."""
exist."""
# Not a property for easy overriding.
# Not a property for easy overriding.
return
self
.
_find_path
(
imp
.
PY_COMPILED
)
return
self
.
_find_path
(
imp
.
PY_COMPILED
)
@
check_name
@
_
check_name
def
write_bytecode
(
self
,
name
,
data
):
def
write_bytecode
(
self
,
name
,
data
):
"""Write out 'data' for the specified module, returning a boolean
"""Write out 'data' for the specified module, returning a boolean
signifying if the write-out actually occurred.
signifying if the write-out actually occurred.
...
@@ -563,10 +563,10 @@ class PyPycFileLoader(PyPycLoader, PyFileLoader):
...
@@ -563,10 +563,10 @@ class PyPycFileLoader(PyPycLoader, PyFileLoader):
"""
"""
bytecode_path
=
self
.
bytecode_path
(
name
)
bytecode_path
=
self
.
bytecode_path
(
name
)
if
not
bytecode_path
:
if
not
bytecode_path
:
bytecode_path
=
self
.
_base_path
+
suffix_list
(
imp
.
PY_COMPILED
)[
0
]
bytecode_path
=
self
.
_base_path
+
_
suffix_list
(
imp
.
PY_COMPILED
)[
0
]
file
=
_io
.
FileIO
(
bytecode_path
,
'w'
)
# Assuming bytes.
file
=
_io
.
FileIO
(
bytecode_path
,
'w'
)
# Assuming bytes.
try
:
try
:
with
closing
(
file
)
as
bytecode_file
:
with
_
closing
(
file
)
as
bytecode_file
:
bytecode_file
.
write
(
data
)
bytecode_file
.
write
(
data
)
return
True
return
True
except
IOError
as
exc
:
except
IOError
as
exc
:
...
@@ -645,7 +645,7 @@ class ExtensionFileFinder(FileFinder):
...
@@ -645,7 +645,7 @@ class ExtensionFileFinder(FileFinder):
def
__init__
(
self
,
path_entry
):
def
__init__
(
self
,
path_entry
):
# Assigning to _suffixes here instead of at the class level because
# Assigning to _suffixes here instead of at the class level because
# imp is not imported at the time of class creation.
# imp is not imported at the time of class creation.
self
.
_suffixes
=
suffix_list
(
imp
.
C_EXTENSION
)
self
.
_suffixes
=
_
suffix_list
(
imp
.
C_EXTENSION
)
super
().
__init__
(
path_entry
)
super
().
__init__
(
path_entry
)
...
@@ -660,7 +660,7 @@ class PyFileFinder(FileFinder):
...
@@ -660,7 +660,7 @@ class PyFileFinder(FileFinder):
# Lack of imp during class creation means _suffixes is set here.
# Lack of imp during class creation means _suffixes is set here.
# Make sure that Python source files are listed first! Needed for an
# Make sure that Python source files are listed first! Needed for an
# optimization by the loader.
# optimization by the loader.
self
.
_suffixes
=
suffix_list
(
imp
.
PY_SOURCE
)
self
.
_suffixes
=
_
suffix_list
(
imp
.
PY_SOURCE
)
super
().
__init__
(
path_entry
)
super
().
__init__
(
path_entry
)
...
@@ -672,7 +672,7 @@ class PyPycFileFinder(PyFileFinder):
...
@@ -672,7 +672,7 @@ class PyPycFileFinder(PyFileFinder):
def
__init__
(
self
,
path_entry
):
def
__init__
(
self
,
path_entry
):
super
().
__init__
(
path_entry
)
super
().
__init__
(
path_entry
)
self
.
_suffixes
+=
suffix_list
(
imp
.
PY_COMPILED
)
self
.
_suffixes
+=
_
suffix_list
(
imp
.
PY_COMPILED
)
class
PathFinder
:
class
PathFinder
:
...
@@ -738,7 +738,7 @@ class PathFinder:
...
@@ -738,7 +738,7 @@ class PathFinder:
return
None
return
None
_DEFAULT_PATH_HOOK
=
chained_path_hook
(
ExtensionFileFinder
,
PyPycFileFinder
)
_DEFAULT_PATH_HOOK
=
_
chained_path_hook
(
ExtensionFileFinder
,
PyPycFileFinder
)
class
_DefaultPathFinder
(
PathFinder
):
class
_DefaultPathFinder
(
PathFinder
):
...
@@ -761,7 +761,7 @@ class _DefaultPathFinder(PathFinder):
...
@@ -761,7 +761,7 @@ class _DefaultPathFinder(PathFinder):
return
super
().
_path_importer_cache
(
path
,
_DEFAULT_PATH_HOOK
)
return
super
().
_path_importer_cache
(
path
,
_DEFAULT_PATH_HOOK
)
class
ImportLockContext
:
class
_
ImportLockContext
:
"""Context manager for the import lock."""
"""Context manager for the import lock."""
...
@@ -806,7 +806,7 @@ def _gcd_import(name, package=None, level=0):
...
@@ -806,7 +806,7 @@ def _gcd_import(name, package=None, level=0):
name
=
"{0}.{1}"
.
format
(
package
[:
dot
],
name
)
name
=
"{0}.{1}"
.
format
(
package
[:
dot
],
name
)
else
:
else
:
name
=
package
[:
dot
]
name
=
package
[:
dot
]
with
ImportLockContext
():
with
_
ImportLockContext
():
try
:
try
:
return
sys
.
modules
[
name
]
return
sys
.
modules
[
name
]
except
KeyError
:
except
KeyError
:
...
...
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