@@ -160,6 +160,13 @@ def _wrap(new, old):
160160
161161# Finder/loader utility code ##################################################
162162
163+ def verbose_message (message , * args ):
164+ """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
165+ if sys .flags .verbose :
166+ if not message .startswith ('#' ) and not message .startswith ('import ' ):
167+ message = '# ' + message
168+ print (message .format (* args ), file = sys .stderr )
169+
163170
164171def set_package (fxn ):
165172 """Set __package__ on the returned module."""
@@ -388,19 +395,24 @@ def _bytes_from_bytecode(self, fullname, data, bytecode_path, source_stats):
388395 raise ImportError ("bad magic number in {}" .format (fullname ),
389396 name = fullname , path = bytecode_path )
390397 elif len (raw_timestamp ) != 4 :
391- raise EOFError ("bad timestamp in {}" .format (fullname ))
398+ message = 'bad timestamp in {}' .format (fullname )
399+ verbose_message (message )
400+ raise EOFError (message )
392401 elif len (raw_size ) != 4 :
393- raise EOFError ("bad size in {}" .format (fullname ))
402+ message = 'bad size in {}' .format (fullname )
403+ verbose_message (message )
404+ raise EOFError (message )
394405 if source_stats is not None :
395406 try :
396407 source_mtime = int (source_stats ['mtime' ])
397408 except KeyError :
398409 pass
399410 else :
400411 if _r_long (raw_timestamp ) != source_mtime :
401- raise ImportError (
402- "bytecode is stale for {}" .format (fullname ),
403- name = fullname , path = bytecode_path )
412+ message = 'bytecode is stale for {}' .format (fullname )
413+ verbose_message (message )
414+ raise ImportError (message , name = fullname ,
415+ path = bytecode_path )
404416 try :
405417 source_size = source_stats ['size' ] & 0xFFFFFFFF
406418 except KeyError :
@@ -506,9 +518,13 @@ def get_code(self, fullname):
506518 except (ImportError , EOFError ):
507519 pass
508520 else :
521+ verbose_message ('{} matches {}' , bytecode_path ,
522+ source_path )
509523 found = marshal .loads (bytes_data )
510524 if isinstance (found , code_type ):
511525 imp ._fix_co_filename (found , source_path )
526+ verbose_message ('code object from {}' ,
527+ bytecode_path )
512528 return found
513529 else :
514530 msg = "Non-code object in {}"
@@ -517,6 +533,7 @@ def get_code(self, fullname):
517533 source_bytes = self .get_data (source_path )
518534 code_object = compile (source_bytes , source_path , 'exec' ,
519535 dont_inherit = True )
536+ verbose_message ('code object from {}' , source_path )
520537 if (not sys .dont_write_bytecode and bytecode_path is not None and
521538 source_mtime is not None ):
522539 # If e.g. Jython ever implements imp.cache_from_source to have
@@ -528,6 +545,7 @@ def get_code(self, fullname):
528545 data .extend (marshal .dumps (code_object ))
529546 try :
530547 self .set_data (bytecode_path , data )
548+ verbose_message ('wrote {!r}' , bytecode_path )
531549 except NotImplementedError :
532550 pass
533551 return code_object
@@ -596,6 +614,7 @@ def set_data(self, path, data):
596614 return
597615 try :
598616 _write_atomic (path , data )
617+ verbose_message ('created {!r}' , path )
599618 except (PermissionError , FileExistsError ):
600619 # Don't worry if you can't write bytecode or someone is writing
601620 # it at the same time.
@@ -615,6 +634,7 @@ def get_code(self, fullname):
615634 bytes_data = self ._bytes_from_bytecode (fullname , data , path , None )
616635 found = marshal .loads (bytes_data )
617636 if isinstance (found , code_type ):
637+ verbose_message ('code object from {!r}' , path )
618638 return found
619639 else :
620640 raise ImportError ("Non-code object in {}" .format (path ),
@@ -644,7 +664,9 @@ def load_module(self, fullname):
644664 """Load an extension module."""
645665 is_reload = fullname in sys .modules
646666 try :
647- return imp .load_dynamic (fullname , self ._path )
667+ module = imp .load_dynamic (fullname , self ._path )
668+ verbose_message ('extension module loaded from {!r}' , self ._path )
669+ return module
648670 except :
649671 if not is_reload and fullname in sys .modules :
650672 del sys .modules [fullname ]
@@ -953,6 +975,7 @@ def _find_and_load(name, import_):
953975 elif name not in sys .modules :
954976 # The parent import may have already imported this module.
955977 loader .load_module (name )
978+ verbose_message ('import {!r} # {!r}' , name , loader )
956979 # Backwards-compatibility; be nicer to skip the dict lookup.
957980 module = sys .modules [name ]
958981 if parent :
0 commit comments