summaryrefslogtreecommitdiffstats
path: root/SCons/compat
diff options
context:
space:
mode:
Diffstat (limited to 'SCons/compat')
-rw-r--r--SCons/compat/__init__.py151
-rw-r--r--SCons/compat/_scons_dbm.py45
2 files changed, 196 insertions, 0 deletions
diff --git a/SCons/compat/__init__.py b/SCons/compat/__init__.py
new file mode 100644
index 0000000..18bef48
--- /dev/null
+++ b/SCons/compat/__init__.py
@@ -0,0 +1,151 @@
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__doc__ = """
+SCons compatibility package for old Python versions
+
+This subpackage holds modules that provide backwards-compatible
+implementations of various things that we'd like to use in SCons but which
+only show up in later versions of Python than the early, old version(s)
+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 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.
+
+The rest of the things here will be in individual compatibility modules
+that are either: 1) suitably modified copies of the future modules that
+we want to use; or 2) backwards compatible re-implementations of the
+specific portions of a future module's API that we want to use.
+
+GENERAL WARNINGS: Implementations of functions in the SCons.compat
+modules are *NOT* guaranteed to be fully compliant with these functions in
+later versions of Python. We are only concerned with adding functionality
+that we actually use in SCons, so be wary if you lift this code for
+other uses. (That said, making these more nearly the same as later,
+official versions is still a desirable goal, we just don't need to be
+obsessive about it.)
+
+We name the compatibility modules with an initial '_scons_' (for example,
+_scons_subprocess.py is our compatibility module for subprocess) so
+that we can still try to import the real module name and fall back to
+our compatibility module if we get an ImportError. The import_as()
+function defined below loads the module as the "real" name (without the
+'_scons'), after which all of the "import {module}" statements in the
+rest of our code will find our pre-loaded compatibility module.
+"""
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+import os
+import sys
+import importlib
+
+PYPY = hasattr(sys, 'pypy_translation_info')
+
+
+def rename_module(new, old):
+ """
+ Attempt to import the old module and load it under the new name.
+ Used for purely cosmetic name changes in Python 3.x.
+ """
+ try:
+ sys.modules[new] = importlib.import_module(old)
+ return True
+ except ImportError:
+ return False
+
+
+# Default pickle protocol. Higher protocols are more efficient/featured
+# but incompatible with older Python versions.
+# Negative numbers choose the highest available protocol.
+import pickle
+
+# Was pickle.HIGHEST_PROTOCOL
+# Changed to 2 so py3.5+'s pickle will be compatible with py2.7.
+PICKLE_PROTOCOL = pickle.HIGHEST_PROTOCOL
+
+import shutil
+try:
+ shutil.SameFileError
+except AttributeError:
+ class SameFileError(Exception):
+ pass
+
+ shutil.SameFileError = SameFileError
+
+def with_metaclass(meta, *bases):
+ """
+ Function from jinja2/_compat.py. License: BSD.
+
+ Use it like this::
+
+ class BaseForm(object):
+ pass
+
+ class FormType(type):
+ pass
+
+ class Form(with_metaclass(FormType, BaseForm)):
+ pass
+
+ This requires a bit of explanation: the basic idea is to make a
+ dummy metaclass for one level of class instantiation that replaces
+ itself with the actual metaclass. Because of internal type checks
+ we also need to make sure that we downgrade the custom metaclass
+ for one level to something closer to type (that's why __call__ and
+ __init__ comes back from type etc.).
+
+ This has the advantage over six.with_metaclass of not introducing
+ dummy classes into the final MRO.
+ """
+
+ class metaclass(meta):
+ __call__ = type.__call__
+ __init__ = type.__init__
+
+ def __new__(cls, name, this_bases, d):
+ if this_bases is None:
+ return type.__new__(cls, name, (), d)
+ return meta(name, bases, d)
+
+ return metaclass('temporary_class', None, {})
+
+
+class NoSlotsPyPy(type):
+ """
+ Workaround for PyPy not working well with __slots__ and __class__ assignment.
+ """
+
+ def __new__(meta, name, bases, dct):
+ if PYPY and '__slots__' in dct:
+ dct.pop('__slots__')
+ return super(NoSlotsPyPy, meta).__new__(meta, name, bases, dct)
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4:
diff --git a/SCons/compat/_scons_dbm.py b/SCons/compat/_scons_dbm.py
new file mode 100644
index 0000000..edfe6fd
--- /dev/null
+++ b/SCons/compat/_scons_dbm.py
@@ -0,0 +1,45 @@
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__doc__ = """
+dbm compatibility module for Python versions that don't have dbm.
+
+This does not not NOT (repeat, *NOT*) provide complete dbm functionality.
+It's just a stub on which to hang just enough pieces of dbm functionality
+that the whichdb.whichdb() implementstation in the various 2.X versions of
+Python won't blow up even if dbm wasn't compiled in.
+"""
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+class error(Exception):
+ pass
+
+def open(*args, **kw):
+ raise error()
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4: