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

Skip to content

Commit 1dae0c6

Browse files
committed
Issue #25186: Remove duplicated function from importlib._bootstrap_external
1 parent b5bb6f3 commit 1dae0c6

2 files changed

Lines changed: 2502 additions & 2523 deletions

File tree

Lib/importlib/_bootstrap_external.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,6 @@ def _calc_mode(path):
360360
return mode
361361

362362

363-
def _verbose_message(message, *args, verbosity=1):
364-
"""Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
365-
if sys.flags.verbose >= verbosity:
366-
if not message.startswith(('#', 'import ')):
367-
message = '# ' + message
368-
print(message.format(*args), file=sys.stderr)
369-
370-
371363
def _check_name(method):
372364
"""Decorator to verify that the module being requested matches the one the
373365
loader can handle.
@@ -437,15 +429,15 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
437429
raw_size = data[8:12]
438430
if magic != MAGIC_NUMBER:
439431
message = 'bad magic number in {!r}: {!r}'.format(name, magic)
440-
_verbose_message(message)
432+
_bootstrap._verbose_message(message)
441433
raise ImportError(message, **exc_details)
442434
elif len(raw_timestamp) != 4:
443435
message = 'reached EOF while reading timestamp in {!r}'.format(name)
444-
_verbose_message(message)
436+
_bootstrap._verbose_message(message)
445437
raise EOFError(message)
446438
elif len(raw_size) != 4:
447439
message = 'reached EOF while reading size of source in {!r}'.format(name)
448-
_verbose_message(message)
440+
_bootstrap._verbose_message(message)
449441
raise EOFError(message)
450442
if source_stats is not None:
451443
try:
@@ -455,7 +447,7 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
455447
else:
456448
if _r_long(raw_timestamp) != source_mtime:
457449
message = 'bytecode is stale for {!r}'.format(name)
458-
_verbose_message(message)
450+
_bootstrap._verbose_message(message)
459451
raise ImportError(message, **exc_details)
460452
try:
461453
source_size = source_stats['size'] & 0xFFFFFFFF
@@ -472,7 +464,7 @@ def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None):
472464
"""Compile bytecode as returned by _validate_bytecode_header()."""
473465
code = marshal.loads(data)
474466
if isinstance(code, _code_type):
475-
_verbose_message('code object from {!r}', bytecode_path)
467+
_bootstrap._verbose_message('code object from {!r}', bytecode_path)
476468
if source_path is not None:
477469
_imp._fix_co_filename(code, source_path)
478470
return code
@@ -755,21 +747,21 @@ def get_code(self, fullname):
755747
except (ImportError, EOFError):
756748
pass
757749
else:
758-
_verbose_message('{} matches {}', bytecode_path,
759-
source_path)
750+
_bootstrap._verbose_message('{} matches {}', bytecode_path,
751+
source_path)
760752
return _compile_bytecode(bytes_data, name=fullname,
761753
bytecode_path=bytecode_path,
762754
source_path=source_path)
763755
source_bytes = self.get_data(source_path)
764756
code_object = self.source_to_code(source_bytes, source_path)
765-
_verbose_message('code object from {}', source_path)
757+
_bootstrap._verbose_message('code object from {}', source_path)
766758
if (not sys.dont_write_bytecode and bytecode_path is not None and
767759
source_mtime is not None):
768760
data = _code_to_bytecode(code_object, source_mtime,
769761
len(source_bytes))
770762
try:
771763
self._cache_bytecode(source_path, bytecode_path, data)
772-
_verbose_message('wrote {!r}', bytecode_path)
764+
_bootstrap._verbose_message('wrote {!r}', bytecode_path)
773765
except NotImplementedError:
774766
pass
775767
return code_object
@@ -849,14 +841,16 @@ def set_data(self, path, data, *, _mode=0o666):
849841
except OSError as exc:
850842
# Could be a permission error, read-only filesystem: just forget
851843
# about writing the data.
852-
_verbose_message('could not create {!r}: {!r}', parent, exc)
844+
_bootstrap._verbose_message('could not create {!r}: {!r}',
845+
parent, exc)
853846
return
854847
try:
855848
_write_atomic(path, data, _mode)
856-
_verbose_message('created {!r}', path)
849+
_bootstrap._verbose_message('created {!r}', path)
857850
except OSError as exc:
858851
# Same as above: just don't write the bytecode.
859-
_verbose_message('could not create {!r}: {!r}', path, exc)
852+
_bootstrap._verbose_message('could not create {!r}: {!r}', path,
853+
exc)
860854

861855

862856
class SourcelessFileLoader(FileLoader, _LoaderBasics):
@@ -901,14 +895,14 @@ def create_module(self, spec):
901895
"""Create an unitialized extension module"""
902896
module = _bootstrap._call_with_frames_removed(
903897
_imp.create_dynamic, spec)
904-
_verbose_message('extension module {!r} loaded from {!r}',
898+
_bootstrap._verbose_message('extension module {!r} loaded from {!r}',
905899
spec.name, self.path)
906900
return module
907901

908902
def exec_module(self, module):
909903
"""Initialize an extension module"""
910904
_bootstrap._call_with_frames_removed(_imp.exec_dynamic, module)
911-
_verbose_message('extension module {!r} executed from {!r}',
905+
_bootstrap._verbose_message('extension module {!r} executed from {!r}',
912906
self.name, self.path)
913907

914908
def is_package(self, fullname):
@@ -1023,7 +1017,8 @@ def load_module(self, fullname):
10231017
10241018
"""
10251019
# The import system never calls this method.
1026-
_verbose_message('namespace module loaded with path {!r}', self._path)
1020+
_bootstrap._verbose_message('namespace module loaded with path {!r}',
1021+
self._path)
10271022
return _bootstrap._load_module_shim(self, fullname)
10281023

10291024

@@ -1243,12 +1238,13 @@ def find_spec(self, fullname, target=None):
12431238
# Check for a file w/ a proper suffix exists.
12441239
for suffix, loader_class in self._loaders:
12451240
full_path = _path_join(self.path, tail_module + suffix)
1246-
_verbose_message('trying {}'.format(full_path), verbosity=2)
1241+
_bootstrap._verbose_message('trying {}', full_path, verbosity=2)
12471242
if cache_module + suffix in cache:
12481243
if _path_isfile(full_path):
1249-
return self._get_spec(loader_class, fullname, full_path, None, target)
1244+
return self._get_spec(loader_class, fullname, full_path,
1245+
None, target)
12501246
if is_namespace:
1251-
_verbose_message('possible namespace for {}'.format(base_path))
1247+
_bootstrap._verbose_message('possible namespace for {}', base_path)
12521248
spec = _bootstrap.ModuleSpec(fullname, None)
12531249
spec.submodule_search_locations = [base_path]
12541250
return spec

0 commit comments

Comments
 (0)