Issue 2336: Forward compatibility for use of the "builtins" module
[scons.git] / src / engine / SCons / compat / _scons_builtins.py
index 4f7e7105962dadc424c1846193d481346a2c66cb..6a725c1e44fcd6a8a413ad76d3daf95c0c0c80c7 100644 (file)
 # 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):