summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/compat
diff options
context:
space:
mode:
authorSteven Knight <knight@baldmt.com>2010-04-12 01:50:52 (GMT)
committerSteven Knight <knight@baldmt.com>2010-04-12 01:50:52 (GMT)
commitc74d83bbf1422d3eac32ffa12bf0bbc1d73461df (patch)
treefa0fbd5e94a2affbfe6c3543439475c25119978b /src/engine/SCons/compat
parentdf6e06482bdbacf0c7f5c6838edc5236db133a22 (diff)
downloadSCons-c74d83bbf1422d3eac32ffa12bf0bbc1d73461df.zip
SCons-c74d83bbf1422d3eac32ffa12bf0bbc1d73461df.tar.gz
SCons-c74d83bbf1422d3eac32ffa12bf0bbc1d73461df.tar.bz2
Issue 2336: Forward compatibility for use of the "builtins" module
instead of __builtin__.
Diffstat (limited to 'src/engine/SCons/compat')
-rw-r--r--src/engine/SCons/compat/__init__.py44
-rw-r--r--src/engine/SCons/compat/_scons_builtins.py43
-rw-r--r--src/engine/SCons/compat/_scons_optparse.py10
3 files changed, 52 insertions, 45 deletions
diff --git a/src/engine/SCons/compat/__init__.py b/src/engine/SCons/compat/__init__.py
index 79c6241..2cec73d 100644
--- a/src/engine/SCons/compat/__init__.py
+++ b/src/engine/SCons/compat/__init__.py
@@ -31,12 +31,12 @@ we still support.
Other code will not generally reference things in this package through
the SCons.compat namespace. The modules included here add things to
-the __builtin__ namespace or the global module list so that the rest
+the builtins namespace or the global module list so that the rest
of our code can use the objects and names imported here regardless of
Python version.
-Simply enough, things that go in the __builtin__ name space come from
-our builtins module.
+Simply enough, things that go in the builtins name space come from
+our _scons_builtins module.
The rest of the things here will be in individual compatibility modules
that are either: 1) suitably modified copies of the future modules that
@@ -73,8 +73,21 @@ def import_as(module, name):
file, filename, suffix_mode_type = imp.find_module(module, [dir])
imp.load_module(name, file, filename, suffix_mode_type)
+
+try:
+ import builtins
+except ImportError:
+ # Use the "imp" module to protect the import from fixers.
+ import imp
+ import sys
+ __builtin__ = imp.load_module('__builtin__',
+ *imp.find_module('__builtin__'))
+ sys.modules['builtins'] = __builtin__
+ del __builtin__
+
import _scons_builtins
+
try:
import hashlib
except ImportError:
@@ -93,8 +106,8 @@ try:
except NameError:
# Pre-2.4 Python has no native set type
import_as('_scons_sets', 'sets')
- import __builtin__, sets
- __builtin__.set = sets.Set
+ import builtins, sets
+ builtins.set = sets.Set
try:
@@ -306,9 +319,9 @@ try:
sys.intern
except AttributeError:
# Pre-2.6 Python has no sys.intern() function.
- import __builtin__
+ import builtins
try:
- sys.intern = __builtin__.intern
+ sys.intern = builtins.intern
except AttributeError:
# Pre-2.x Python has no builtin intern() function.
def intern(x):
@@ -357,23 +370,6 @@ except AttributeError:
tempfile.mkstemp = mkstemp
del mkstemp
-try:
- # pre-2.7 doesn't have the memoryview() built-in
- memoryview
-except NameError:
- class memoryview:
- from types import SliceType
- def __init__(self, obj):
- # wrapping buffer in () keeps the fixer from changing it
- self.obj = (buffer)(obj)
- def __getitem__(self, indx):
- if isinstance(indx, self.SliceType):
- return self.obj[indx.start:indx.stop]
- else:
- return self.obj[indx]
- import __builtin__
- __builtin__.memoryview = memoryview
-
# Local Variables:
# tab-width:4
diff --git a/src/engine/SCons/compat/_scons_builtins.py b/src/engine/SCons/compat/_scons_builtins.py
index 4f7e710..6a725c1 100644
--- a/src/engine/SCons/compat/_scons_builtins.py
+++ b/src/engine/SCons/compat/_scons_builtins.py
@@ -27,19 +27,20 @@
# Copyright (c) 2001-2004 Twisted Matrix Laboratories
__doc__ = """
-Compatibility idioms for __builtin__ names
+Compatibility idioms for builtins names
-This module adds names to the __builtin__ module for things that we want
+This module adds names to the builtins module for things that we want
to use in SCons but which don't show up until later Python versions than
the earliest ones we support.
-This module checks for the following __builtin__ names:
+This module checks for the following builtins names:
all()
any()
bool()
dict()
sorted()
+ memoryview()
True
False
zip()
@@ -58,7 +59,7 @@ to this version of Python and we don't need to add them from this module.
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
-import __builtin__
+import builtins
try:
all
@@ -72,7 +73,7 @@ except NameError:
if not element:
return False
return True
- __builtin__.all = all
+ builtins.all = all
all = all
try:
@@ -87,7 +88,7 @@ except NameError:
if element:
return True
return False
- __builtin__.any = any
+ builtins.any = any
any = any
try:
@@ -102,7 +103,7 @@ except NameError:
worth the trouble.
"""
return not not value
- __builtin__.bool = bool
+ builtins.bool = bool
bool = bool
try:
@@ -118,13 +119,13 @@ except NameError:
d[k] = v
d.update(kwargs)
return d
- __builtin__.dict = dict
+ builtins.dict = dict
try:
False
except NameError:
# Pre-2.2 Python has no False keyword.
- __builtin__.False = not 1
+ builtins.False = not 1
# Assign to False in this module namespace so it shows up in pydoc output.
False = False
@@ -132,7 +133,7 @@ try:
True
except NameError:
# Pre-2.2 Python has no True keyword.
- __builtin__.True = not 0
+ builtins.True = not 0
# Assign to True in this module namespace so it shows up in pydoc output.
True = True
@@ -140,7 +141,23 @@ try:
file
except NameError:
# Pre-2.2 Python has no file() function.
- __builtin__.file = open
+ builtins.file = open
+
+try:
+ memoryview
+except NameError:
+ # Pre-2.7 doesn't have the memoryview() built-in.
+ class memoryview:
+ from types import SliceType
+ def __init__(self, obj):
+ # wrapping buffer in () keeps the fixer from changing it
+ self.obj = (buffer)(obj)
+ def __getitem__(self, indx):
+ if isinstance(indx, self.SliceType):
+ return self.obj[indx.start:indx.stop]
+ else:
+ return self.obj[indx]
+ builtins.memoryview = memoryview
try:
sorted
@@ -166,7 +183,7 @@ except NameError:
if reverse:
result.reverse()
return result
- __builtin__.sorted = sorted
+ builtins.sorted = sorted
#
try:
@@ -187,7 +204,7 @@ except NameError:
for i in range(min(list(map(len, lists)))):
result.append(tuple([l[i] for l in lists]))
return result
- __builtin__.zip = zip
+ builtins.zip = zip
#if sys.version_info[:3] in ((2, 2, 0), (2, 2, 1)):
# def lstrip(s, c=string.whitespace):
diff --git a/src/engine/SCons/compat/_scons_optparse.py b/src/engine/SCons/compat/_scons_optparse.py
index b59537d..9c3df52 100644
--- a/src/engine/SCons/compat/_scons_optparse.py
+++ b/src/engine/SCons/compat/_scons_optparse.py
@@ -667,14 +667,8 @@ class Option:
self.type = "string"
else:
# Allow type objects or builtin type conversion functions
- # (int, str, etc.) as an alternative to their names. (The
- # complicated check of __builtin__ is only necessary for
- # Python 2.1 and earlier, and is short-circuited by the
- # first check on modern Pythons.)
- import __builtin__
- if ( isinstance(self.type, type) or
- (hasattr(self.type, "__name__") and
- getattr(__builtin__, self.type.__name__, None) is self.type) ):
+ # (int, str, etc.) as an alternative to their names.
+ if isinstance(self.type, type):
self.type = self.type.__name__
if self.type == "str":