From: stevenknight Date: Mon, 12 Apr 2010 01:50:52 +0000 (+0000) Subject: Issue 2336: Forward compatibility for use of the "builtins" module X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=a1e4a973f0e047fe880baec9499ddf4c25f9aec0;p=scons.git Issue 2336: Forward compatibility for use of the "builtins" module instead of __builtin__. git-svn-id: http://scons.tigris.org/svn/scons/trunk@4781 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/src/engine/SCons/Platform/win32.py b/src/engine/SCons/Platform/win32.py index 52255ba6..4b5953af 100644 --- a/src/engine/SCons/Platform/win32.py +++ b/src/engine/SCons/Platform/win32.py @@ -60,10 +60,10 @@ except AttributeError: else: parallel_msg = None - import __builtin__ + import builtins - _builtin_file = __builtin__.file - _builtin_open = __builtin__.open + _builtin_file = builtins.file + _builtin_open = builtins.open def _scons_file(*args, **kw): fp = _builtin_file(*args, **kw) @@ -79,8 +79,8 @@ else: 0) return fp - __builtin__.file = _scons_file - __builtin__.open = _scons_open + builtins.file = _scons_file + builtins.open = _scons_open diff --git a/src/engine/SCons/SConfTests.py b/src/engine/SCons/SConfTests.py index 7e48862e..4d29b789 100644 --- a/src/engine/SCons/SConfTests.py +++ b/src/engine/SCons/SConfTests.py @@ -98,11 +98,11 @@ class SConfTestCase(unittest.TestCase): # original builtin functions whenever we have to reset # all of our global state. - import __builtin__ + import builtins import SCons.Platform.win32 - __builtin__.file = SCons.Platform.win32._builtin_file - __builtin__.open = SCons.Platform.win32._builtin_open + builtins.file = SCons.Platform.win32._builtin_file + builtins.open = SCons.Platform.win32._builtin_open def _baseTryXXX(self, TryFunc): # TryCompile and TryLink are much the same, so we can test them diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index e88a90b7..6816af07 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -29,6 +29,8 @@ Various utility functions go here. __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import SCons.compat + import copy import os import os.path @@ -738,8 +740,8 @@ else: # OSError subclass on Windows.) class WindowsError(OSError): pass - import __builtin__ - __builtin__.WindowsError = WindowsError + import builtins + builtins.WindowsError = WindowsError else: del e diff --git a/src/engine/SCons/compat/__init__.py b/src/engine/SCons/compat/__init__.py index 79c62413..2cec73d2 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 4f7e7105..6a725c1e 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 b59537db..9c3df525 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": diff --git a/src/engine/SCons/dblite.py b/src/engine/SCons/dblite.py index 01770fec..8e40a5f0 100644 --- a/src/engine/SCons/dblite.py +++ b/src/engine/SCons/dblite.py @@ -3,12 +3,12 @@ import SCons.compat +import builtins import os # compat layer imports "cPickle" for us if it's available. import pickle import shutil import time -import __builtin__ keep_all_files = 00000 ignore_corrupt_dbfiles = 0 @@ -44,7 +44,7 @@ class dblite: # See the discussion at: # http://mail.python.org/pipermail/python-bugs-list/2003-March/016877.html - _open = __builtin__.open + _open = builtins.open _pickle_dump = pickle.dump _os_chmod = os.chmod try: