@@ -733,3 +733,48 @@ def from_import(module_name, *symbol_names, **kwargs):
733733 else :
734734 return output
735735
736+
737+ class exclude_local_folder_imports (object ):
738+ """
739+ A context-manager that prevents standard library modules like configparser
740+ from being imported from the local python-future source folder on Py3.
741+
742+ (The presence of a configparser folder would otherwise prevent setuptools
743+ from running on Py3.)
744+ """
745+ def __init__ (self , * args ):
746+ assert len (args ) > 0
747+ self .module_names = args
748+
749+ def __enter__ (self ):
750+ self .old_sys_path = copy .copy (sys .path )
751+ self .old_sys_modules = copy .copy (sys .modules )
752+ if sys .version_info [0 ] < 3 :
753+ return
754+ FUTURE_SOURCE_SUBFOLDERS = ['future' , 'past' , 'libfuturize' , 'configparser' ]
755+
756+ # Look for the future source folder:
757+ for folder in self .old_sys_path :
758+ if all ([os .path .exists (os .path .join (folder , subfolder ))
759+ for subfolder in FUTURE_SOURCE_SUBFOLDERS ]):
760+ # Found it. Remove it.
761+ sys .path .remove (folder )
762+
763+ # Ensure we import the system module:
764+ for m in self .module_names :
765+ try :
766+ del sys .modules [m ]
767+ except KeyError :
768+ pass
769+ try :
770+ module = __import__ (m , level = 0 )
771+ except ImportError :
772+ # There's a problem importing the system module. E.g. the
773+ # winreg module is not available except on Windows.
774+ pass
775+
776+ def __exit__ (self , * args ):
777+ # Restore sys.path and sys.modules:
778+ sys .path = self .old_sys_path
779+ for m in set (self .old_sys_modules .keys ()) - set (sys .modules .keys ()):
780+ sys .modules [m ] = self .old_sys_modules [m ]
0 commit comments