7070import copy
7171import os
7272
73+ # Make a dedicated logger; leave the root logger to be configured
74+ # by the application.
75+ flog = logging .getLogger ('future_stdlib' )
76+ _formatter = logging .Formatter (logging .BASIC_FORMAT )
77+ _handler = logging .StreamHandler ()
78+ _handler .setFormatter (_formatter )
79+ flog .addHandler (_handler )
80+ flog .setLevel (logging .WARN )
81+
7382from future .utils import PY2 , PY3
7483
7584# The modules that are defined under the same names on Py3 but with
214223# module_info = imp.find_module(name, self.path)
215224# module = imp.load_module(name, *module_info)
216225# sys.modules[name] = module
217- # logging .warning("Imported deprecated module %s", name)
226+ # flog .warning("Imported deprecated module %s", name)
218227# return module
219228
220229
@@ -278,10 +287,10 @@ def _find_and_load_module(self, name, path=None):
278287 path = package .__path__
279288 except AttributeError :
280289 # This could be e.g. moves.
281- logging .debug ('Package {0} has no __path__.' .format (package ))
290+ flog .debug ('Package {0} has no __path__.' .format (package ))
282291 if name in sys .modules :
283292 return sys .modules [name ]
284- logging .debug ('What to do here?' )
293+ flog .debug ('What to do here?' )
285294
286295 name = bits [0 ]
287296 # We no longer use the fake module six.moves:
@@ -311,15 +320,15 @@ class hooks(object):
311320 imported modules (like requests).
312321 """
313322 def __enter__ (self ):
314- # logging .debug('Entering hooks context manager')
323+ # flog .debug('Entering hooks context manager')
315324 self .old_sys_modules = copy .copy (sys .modules )
316325 self .hooks_were_installed = detect_hooks ()
317326 self .scrubbed = scrub_py2_sys_modules ()
318327 install_hooks ()
319328 return self
320329
321330 def __exit__ (self , * args ):
322- # logging .debug('Exiting hooks context manager')
331+ # flog .debug('Exiting hooks context manager')
323332 restore_sys_modules (self .scrubbed )
324333 if not self .hooks_were_installed :
325334 remove_hooks ()
@@ -344,7 +353,7 @@ def is_py2_stdlib_module(m):
344353 if not len (set (stdlib_paths )) == 1 :
345354 # This seems to happen on travis-ci.org. Very strange. We'll try to
346355 # ignore it.
347- logging .warn ('Multiple locations found for the Python standard '
356+ flog .warn ('Multiple locations found for the Python standard '
348357 'library: %s' % stdlib_paths )
349358 # Choose the first one arbitrarily
350359 is_py2_stdlib_module .stdlib_path = stdlib_paths [0 ]
@@ -380,7 +389,7 @@ def scrub_py2_sys_modules():
380389 module = sys .modules [modulename ]
381390
382391 if is_py2_stdlib_module (module ):
383- logging .debug ('Deleting (Py2) {} from sys.modules' .format (modulename ))
392+ flog .debug ('Deleting (Py2) {} from sys.modules' .format (modulename ))
384393 scrubbed [modulename ] = sys .modules [modulename ]
385394 del sys .modules [modulename ]
386395 return scrubbed
@@ -423,7 +432,7 @@ def scrub_future_sys_modules():
423432 return {}
424433 for modulename , module in sys .modules .items ():
425434 if modulename .startswith ('future' ):
426- logging .debug ('Not removing %s' % modulename )
435+ flog .debug ('Not removing %s' % modulename )
427436 continue
428437 # We don't want to remove Python 2.x urllib if this is cached.
429438 # But we do want to remove modules under their new names, e.g.
@@ -437,7 +446,7 @@ def scrub_future_sys_modules():
437446
438447 if module is None :
439448 # This happens for e.g. __future__ imports. Delete it.
440- logging .debug ('Deleting empty module {0} from sys.modules'
449+ flog .debug ('Deleting empty module {0} from sys.modules'
441450 .format (modulename ))
442451 del sys .modules [modulename ]
443452 continue
@@ -448,12 +457,12 @@ def scrub_future_sys_modules():
448457 # six.moves doesn't have a __file__ attribute:
449458 if (hasattr (module , '__file__' ) and p in module .__file__ or
450459 hasattr (module , '__future_module__' )):
451- logging .debug ('Deleting (future) {0} {1} from sys.modules'
460+ flog .debug ('Deleting (future) {0} {1} from sys.modules'
452461 .format (modulename , module ))
453462 scrubbed [modulename ] = sys .modules [modulename ]
454463 del sys .modules [modulename ]
455464 else :
456- logging .debug ('Not deleting {0} {1} from sys.modules'
465+ flog .debug ('Not deleting {0} {1} from sys.modules'
457466 .format (modulename , module ))
458467 return scrubbed
459468
@@ -561,14 +570,14 @@ def install_hooks():
561570
562571 install_aliases ()
563572
564- logging .debug ('sys.meta_path was: {0}' .format (sys .meta_path ))
565- logging .debug ('Installing hooks ...' )
573+ flog .debug ('sys.meta_path was: {0}' .format (sys .meta_path ))
574+ flog .debug ('Installing hooks ...' )
566575
567576 # Add it unless it's there already
568577 newhook = RenameImport (RENAMES )
569578 if not detect_hooks ():
570579 sys .meta_path .append (newhook )
571- logging .debug ('sys.meta_path is now: {0}' .format (sys .meta_path ))
580+ flog .debug ('sys.meta_path is now: {0}' .format (sys .meta_path ))
572581
573582
574583def enable_hooks ():
@@ -585,7 +594,7 @@ def remove_hooks(scrub_sys_modules=True):
585594 """
586595 if PY3 :
587596 return
588- logging .debug ('Uninstalling hooks ...' )
597+ flog .debug ('Uninstalling hooks ...' )
589598 # Loop backwards, so deleting items keeps the ordering:
590599 for i , hook in list (enumerate (sys .meta_path ))[::- 1 ]:
591600 if hasattr (hook , 'RENAMER' ):
@@ -611,12 +620,12 @@ def detect_hooks():
611620 """
612621 Returns True if the import hooks are installed, False if not.
613622 """
614- logging .debug ('Detecting hooks ...' )
623+ flog .debug ('Detecting hooks ...' )
615624 present = any ([hasattr (hook , 'RENAMER' ) for hook in sys .meta_path ])
616625 if present :
617- logging .debug ('Detected.' )
626+ flog .debug ('Detected.' )
618627 else :
619- logging .debug ('Not detected.' )
628+ flog .debug ('Not detected.' )
620629 return present
621630
622631
0 commit comments