Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eeaa0a

Browse files
committed
Make utility code in importlib._bootstrap private.
1 parent 9495f18 commit 3eeaa0a

1 file changed

Lines changed: 32 additions & 32 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _path_isdir(path):
5656

5757
def _path_without_ext(path, ext_type):
5858
"""Replacement for os.path.splitext()[0]."""
59-
for suffix in suffix_list(ext_type):
59+
for suffix in _suffix_list(ext_type):
6060
if path.endswith(suffix):
6161
return path[:-len(suffix)]
6262
else:
@@ -76,7 +76,7 @@ def _path_absolute(path):
7676
return _path_join(_os.getcwd(), path)
7777

7878

79-
class closing:
79+
class _closing:
8080

8181
"""Simple replacement for contextlib.closing."""
8282

@@ -90,7 +90,7 @@ def __exit__(self, *args):
9090
self.obj.close()
9191

9292

93-
def wrap(new, old):
93+
def _wrap(new, old):
9494
"""Simple substitute for functools.wraps."""
9595
for replace in ['__module__', '__name__', '__doc__']:
9696
setattr(new, replace, getattr(old, replace))
@@ -106,7 +106,7 @@ def wrapper(*args, **kwargs):
106106
if not hasattr(module, '__path__'):
107107
module.__package__ = module.__package__.rpartition('.')[0]
108108
return module
109-
wrap(wrapper, fxn)
109+
_wrap(wrapper, fxn)
110110
return wrapper
111111

112112

@@ -117,7 +117,7 @@ def wrapper(self, *args, **kwargs):
117117
if not hasattr(module, '__loader__'):
118118
module.__loader__ = self
119119
return module
120-
wrap(wrapper, fxn)
120+
_wrap(wrapper, fxn)
121121
return wrapper
122122

123123

@@ -187,7 +187,7 @@ def load_module(cls, fullname):
187187
raise
188188

189189

190-
def chained_path_hook(*path_hooks):
190+
def _chained_path_hook(*path_hooks):
191191
"""Create a closure which sequentially checks path hooks to see which ones
192192
(if any) can work with a path."""
193193
def path_hook(entry):
@@ -203,12 +203,12 @@ def path_hook(entry):
203203
if not finders:
204204
raise ImportError("no finder found")
205205
else:
206-
return ChainedFinder(*finders)
206+
return _ChainedFinder(*finders)
207207

208208
return path_hook
209209

210210

211-
class ChainedFinder:
211+
class _ChainedFinder:
212212

213213
"""Finder that sequentially calls other finders."""
214214

@@ -224,7 +224,7 @@ def find_module(self, fullname, path=None):
224224
return None
225225

226226

227-
def check_name(method):
227+
def _check_name(method):
228228
"""Decorator to verify that the module being requested matches the one the
229229
loader can handle.
230230
@@ -236,7 +236,7 @@ def inner(self, name, *args, **kwargs):
236236
if self._name != name:
237237
raise ImportError("loader cannot handle %s" % name)
238238
return method(self, name, *args, **kwargs)
239-
wrap(inner, method)
239+
_wrap(inner, method)
240240
return inner
241241

242242

@@ -260,7 +260,7 @@ def __init__(self, name, path, is_pkg):
260260
if is_pkg:
261261
raise ValueError("extension modules cannot be packages")
262262

263-
@check_name
263+
@_check_name
264264
@set_package
265265
@set_loader
266266
def load_module(self, fullname):
@@ -273,23 +273,23 @@ def load_module(self, fullname):
273273
del sys.modules[fullname]
274274
raise
275275

276-
@check_name
276+
@_check_name
277277
def is_package(self, fullname):
278278
"""Return False as an extension module can never be a package."""
279279
return False
280280

281-
@check_name
281+
@_check_name
282282
def get_code(self, fullname):
283283
"""Return None as an extension module cannot create a code object."""
284284
return None
285285

286-
@check_name
286+
@_check_name
287287
def get_source(self, fullname):
288288
"""Return None as extension modules have no source code."""
289289
return None
290290

291291

292-
def suffix_list(suffix_type):
292+
def _suffix_list(suffix_type):
293293
"""Return a list of file suffixes based on the imp file type."""
294294
return [suffix[0] for suffix in imp.get_suffixes()
295295
if suffix[2] == suffix_type]
@@ -323,7 +323,7 @@ def decorated(self, fullname):
323323
if not is_reload:
324324
del sys.modules[fullname]
325325
raise
326-
wrap(decorated, fxn)
326+
_wrap(decorated, fxn)
327327
return decorated
328328

329329

@@ -484,21 +484,21 @@ def __init__(self, name, path, is_pkg):
484484
def _find_path(self, ext_type):
485485
"""Find a path from the base path and the specified extension type that
486486
exists, returning None if one is not found."""
487-
for suffix in suffix_list(ext_type):
487+
for suffix in _suffix_list(ext_type):
488488
path = self._base_path + suffix
489489
if _path_exists(path):
490490
return path
491491
else:
492492
return None
493493

494-
@check_name
494+
@_check_name
495495
def source_path(self, fullname):
496496
"""Return the path to an existing source file for the module, or None
497497
if one cannot be found."""
498498
# Not a property so that it is easy to override.
499499
return self._find_path(imp.PY_SOURCE)
500500

501-
@check_name
501+
@_check_name
502502
def get_source(self, fullname):
503503
"""Return the source for the module as a string.
504504
@@ -510,7 +510,7 @@ def get_source(self, fullname):
510510
if source_path is None:
511511
return None
512512
import tokenize
513-
with closing(_io.FileIO(source_path, 'r')) as file: # Assuming bytes.
513+
with _closing(_io.FileIO(source_path, 'r')) as file: # Assuming bytes.
514514
encoding, lines = tokenize.detect_encoding(file.readline)
515515
# XXX Will fail when passed to compile() if the encoding is
516516
# anything other than UTF-8.
@@ -521,7 +521,7 @@ def get_data(self, path):
521521
"""Return the data from path as raw bytes."""
522522
return _io.FileIO(path, 'r').read() # Assuming bytes.
523523

524-
@check_name
524+
@_check_name
525525
def is_package(self, fullname):
526526
"""Return a boolean based on whether the module is a package.
527527
@@ -536,7 +536,7 @@ class PyPycFileLoader(PyPycLoader, PyFileLoader):
536536

537537
"""Load a module from a source or bytecode file."""
538538

539-
@check_name
539+
@_check_name
540540
def source_mtime(self, name):
541541
"""Return the modification time of the source for the specified
542542
module."""
@@ -545,14 +545,14 @@ def source_mtime(self, name):
545545
return None
546546
return int(_os.stat(source_path).st_mtime)
547547

548-
@check_name
548+
@_check_name
549549
def bytecode_path(self, fullname):
550550
"""Return the path to a bytecode file, or None if one does not
551551
exist."""
552552
# Not a property for easy overriding.
553553
return self._find_path(imp.PY_COMPILED)
554554

555-
@check_name
555+
@_check_name
556556
def write_bytecode(self, name, data):
557557
"""Write out 'data' for the specified module, returning a boolean
558558
signifying if the write-out actually occurred.
@@ -563,10 +563,10 @@ def write_bytecode(self, name, data):
563563
"""
564564
bytecode_path = self.bytecode_path(name)
565565
if not bytecode_path:
566-
bytecode_path = self._base_path + suffix_list(imp.PY_COMPILED)[0]
566+
bytecode_path = self._base_path + _suffix_list(imp.PY_COMPILED)[0]
567567
file = _io.FileIO(bytecode_path, 'w') # Assuming bytes.
568568
try:
569-
with closing(file) as bytecode_file:
569+
with _closing(file) as bytecode_file:
570570
bytecode_file.write(data)
571571
return True
572572
except IOError as exc:
@@ -645,7 +645,7 @@ class ExtensionFileFinder(FileFinder):
645645
def __init__(self, path_entry):
646646
# Assigning to _suffixes here instead of at the class level because
647647
# imp is not imported at the time of class creation.
648-
self._suffixes = suffix_list(imp.C_EXTENSION)
648+
self._suffixes = _suffix_list(imp.C_EXTENSION)
649649
super().__init__(path_entry)
650650

651651

@@ -660,7 +660,7 @@ def __init__(self, path_entry):
660660
# Lack of imp during class creation means _suffixes is set here.
661661
# Make sure that Python source files are listed first! Needed for an
662662
# optimization by the loader.
663-
self._suffixes = suffix_list(imp.PY_SOURCE)
663+
self._suffixes = _suffix_list(imp.PY_SOURCE)
664664
super().__init__(path_entry)
665665

666666

@@ -672,7 +672,7 @@ class PyPycFileFinder(PyFileFinder):
672672

673673
def __init__(self, path_entry):
674674
super().__init__(path_entry)
675-
self._suffixes += suffix_list(imp.PY_COMPILED)
675+
self._suffixes += _suffix_list(imp.PY_COMPILED)
676676

677677

678678
class PathFinder:
@@ -738,7 +738,7 @@ def find_module(cls, fullname, path=None):
738738
return None
739739

740740

741-
_DEFAULT_PATH_HOOK = chained_path_hook(ExtensionFileFinder, PyPycFileFinder)
741+
_DEFAULT_PATH_HOOK = _chained_path_hook(ExtensionFileFinder, PyPycFileFinder)
742742

743743
class _DefaultPathFinder(PathFinder):
744744

@@ -761,7 +761,7 @@ def _path_importer_cache(cls, path):
761761
return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK)
762762

763763

764-
class ImportLockContext:
764+
class _ImportLockContext:
765765

766766
"""Context manager for the import lock."""
767767

@@ -806,7 +806,7 @@ def _gcd_import(name, package=None, level=0):
806806
name = "{0}.{1}".format(package[:dot], name)
807807
else:
808808
name = package[:dot]
809-
with ImportLockContext():
809+
with _ImportLockContext():
810810
try:
811811
return sys.modules[name]
812812
except KeyError:

0 commit comments

Comments
 (0)