8000 Attempt to prevent imports from the python-future source directory fr… · thecodingchicken/python-future@d288411 · GitHub
[go: up one dir, main page]

Skip to content

Commit d288411

Browse files
committed
Attempt to prevent imports from the python-future source directory from failing on Py3
1 parent 5bdfe42 commit d288411

File tree

17 files changed

+165
-65
lines changed

17 files changed

+165
-65
lines changed

_dummy_thread/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import absolute_import
2-
from future.utils import PY3
2+
import sys
3+
__future_module__ = True
34

4-
if PY3:
5-
from _dummy_thread import *
6-
else:
7-
__future_module__ = True
5+
if sys.version_info[0] < 3:
86
from dummy_thread import *
7+
else:
8+
from future.standard_library import exclude_local_folder_imports
9+
with exclude_local_folder_imports('_dummy_thread'):
10+
from _dummy_thread import *

_markupbase/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import absolute_import
2-
from future.utils import PY3
2+
import sys
3+
__future_module__ = True
34

4-
if PY3:
5-
from _markupbase import *
6-
else:
7-
__future_module__ = True
5+
if sys.version_info[0] < 3:
86
from markupbase import *
7+
else:
8+
from future.standard_library import exclude_local_folder_imports
9+
with exclude_local_folder_imports('_markupbase'):
10+
from _markupbase import *

_thread/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import absolute_import
2-
from future.utils import PY3
2+
import sys
3+
__future_module__ = True
34

4-
if PY3:
5-
from _thread import *
5+
if sys.version_info[0] < 3:
6+
from dummy_thread import *
67
else:
7-
__future_module__ = True
8-
from thread import *
8+
from future.standard_library import exclude_local_folder_imports
9+
with exclude_local_folder_imports('_dummy_thread'):
10+
from _dummy_thread import *

builtins/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import absolute_import
2-
from future.utils import PY3
2+
import sys
3+
__future_module__ = True
34

4-
if PY3:
5-
from builtins import *
6-
else:
7-
__future_module__ = True
5+
if sys.version_info[0] < 3:
86
from __builtin__ import *
97
# Overwrite any old definitions with the equivalent future.builtins ones:
108
from future.builtins import *
9+
else:
10+
from future.standard_library import exclude_local_folder_imports
11+
with exclude_local_folder_imports('builtins'):
12+
from builtins import *

configparser/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import absolute_import
2+
import sys
23

3-
from future.utils import PY2
4-
5-
if PY2:
4+
if sys.version_info[0] < 3:
65
from ConfigParser import *
76
else:
8-
from configparser import *
7+
from future.standard_library import exclude_local_folder_imports
8+
with exclude_local_folder_imports('configparser'):
9+
from configparser import *

future/standard_library/__init__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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]

future/tests/test_standard_library.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ def test_install_aliases(self):
445445

446446
from sys import intern
447447

448-
import test.support
448+
# test_support may not be available (e.g. on Anaconda Py2.6):
449+
# import test.support
449450

450451
import urllib.error
451452
import urllib.parse

html/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from __future__ import absolute_import, unicode_literals
2-
from future.utils import PY3
1+
from __future__ import absolute_import
2+
import sys
3+
__future_module__ = True
34

4-
if PY3:
5-
from html import *
5+
if sys.version_info[0] == 3:
6+
from future.standard_library import exclude_local_folder_imports
7+
with exclude_local_folder_imports('html'):
8+
from html import *
69
else:
7-
__future_module__ = True
810

911
# cgi.escape isn't good enough for the single Py3.3 html test to pass.
1012
# Define it inline here instead. From the Py3.3 stdlib

http/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from future.utils import PY3
1+
from __future__ import absolute_import
2+
import sys
3+
__future_module__ = True
24

3-
if not PY3:
4-
__future_module__ = True
5+
if sys.version_info[0] < 3:
6+
pass
7+
else:
8+
from future.standard_library import exclude_local_folder_imports
9+
with exclude_local_folder_imports('http'):
10+
from http import *

http/server.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from __future__ import absolute_import
2-
from future.utils import PY3
2+
import sys
3+
__future_module__ = True
34

4-
if PY3:
5-
from http.server import *
5+
if sys.version_info[0] == 3:
6+
from future.standard_library import exclude_local_folder_imports
7+
with exclude_local_folder_imports('http.server'):
8+
from http.server import *
69
else:
710
__future_module__ = True
811
from BaseHTTPServer import *

0 commit comments

Comments
 (0)
0