You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
7.0 KiB

6 years ago
  1. #
  2. # Copyright (c) 2001 - 2017 The SCons Foundation
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining
  5. # a copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and to
  9. # permit persons to whom the Software is furnished to do so, subject to
  10. # the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included
  13. # in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  16. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #
  23. __doc__ = """
  24. SCons compatibility package for old Python versions
  25. This subpackage holds modules that provide backwards-compatible
  26. implementations of various things that we'd like to use in SCons but which
  27. only show up in later versions of Python than the early, old version(s)
  28. we still support.
  29. Other code will not generally reference things in this package through
  30. the SCons.compat namespace. The modules included here add things to
  31. the builtins namespace or the global module list so that the rest
  32. of our code can use the objects and names imported here regardless of
  33. Python version.
  34. The rest of the things here will be in individual compatibility modules
  35. that are either: 1) suitably modified copies of the future modules that
  36. we want to use; or 2) backwards compatible re-implementations of the
  37. specific portions of a future module's API that we want to use.
  38. GENERAL WARNINGS: Implementations of functions in the SCons.compat
  39. modules are *NOT* guaranteed to be fully compliant with these functions in
  40. later versions of Python. We are only concerned with adding functionality
  41. that we actually use in SCons, so be wary if you lift this code for
  42. other uses. (That said, making these more nearly the same as later,
  43. official versions is still a desirable goal, we just don't need to be
  44. obsessive about it.)
  45. We name the compatibility modules with an initial '_scons_' (for example,
  46. _scons_subprocess.py is our compatibility module for subprocess) so
  47. that we can still try to import the real module name and fall back to
  48. our compatibility module if we get an ImportError. The import_as()
  49. function defined below loads the module as the "real" name (without the
  50. '_scons'), after which all of the "import {module}" statements in the
  51. rest of our code will find our pre-loaded compatibility module.
  52. """
  53. __revision__ = "src/engine/SCons/compat/__init__.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  54. import os
  55. import sys
  56. import imp # Use the "imp" module to protect imports from fixers.
  57. PYPY = hasattr(sys, 'pypy_translation_info')
  58. def import_as(module, name):
  59. """
  60. Imports the specified module (from our local directory) as the
  61. specified name, returning the loaded module object.
  62. """
  63. dir = os.path.split(__file__)[0]
  64. return imp.load_module(name, *imp.find_module(module, [dir]))
  65. def rename_module(new, old):
  66. """
  67. Attempts to import the old module and load it under the new name.
  68. Used for purely cosmetic name changes in Python 3.x.
  69. """
  70. try:
  71. sys.modules[new] = imp.load_module(old, *imp.find_module(old))
  72. return True
  73. except ImportError:
  74. return False
  75. # TODO: FIXME
  76. # In 3.x, 'pickle' automatically loads the fast version if available.
  77. rename_module('pickle', 'cPickle')
  78. # Default pickle protocol. Higher protocols are more efficient/featureful
  79. # but incompatible with older Python versions. On Python 2.7 this is 2.
  80. # Negative numbers choose the highest available protocol.
  81. import pickle
  82. # Was pickle.HIGHEST_PROTOCOL
  83. # Changed to 2 so py3.5+'s pickle will be compatible with py2.7.
  84. PICKLE_PROTOCOL = 2
  85. # TODO: FIXME
  86. # In 3.x, 'profile' automatically loads the fast version if available.
  87. rename_module('profile', 'cProfile')
  88. # TODO: FIXME
  89. # Before Python 3.0, the 'queue' module was named 'Queue'.
  90. rename_module('queue', 'Queue')
  91. # TODO: FIXME
  92. # Before Python 3.0, the 'winreg' module was named '_winreg'
  93. rename_module('winreg', '_winreg')
  94. # Python 3 moved builtin intern() to sys package
  95. # To make porting easier, make intern always live
  96. # in sys package (for python 2.7.x)
  97. try:
  98. sys.intern
  99. except AttributeError:
  100. # We must be using python 2.7.x so monkey patch
  101. # intern into the sys package
  102. sys.intern = intern
  103. # Preparing for 3.x. UserDict, UserList, UserString are in
  104. # collections for 3.x, but standalone in 2.7.x
  105. import collections
  106. try:
  107. collections.UserDict
  108. except AttributeError:
  109. exec ('from UserDict import UserDict as _UserDict')
  110. collections.UserDict = _UserDict
  111. del _UserDict
  112. try:
  113. collections.UserList
  114. except AttributeError:
  115. exec ('from UserList import UserList as _UserList')
  116. collections.UserList = _UserList
  117. del _UserList
  118. try:
  119. collections.UserString
  120. except AttributeError:
  121. exec ('from UserString import UserString as _UserString')
  122. collections.UserString = _UserString
  123. del _UserString
  124. import shutil
  125. try:
  126. shutil.SameFileError
  127. except AttributeError:
  128. class SameFileError(Exception):
  129. pass
  130. shutil.SameFileError = SameFileError
  131. def with_metaclass(meta, *bases):
  132. """
  133. Function from jinja2/_compat.py. License: BSD.
  134. Use it like this::
  135. class BaseForm(object):
  136. pass
  137. class FormType(type):
  138. pass
  139. class Form(with_metaclass(FormType, BaseForm)):
  140. pass
  141. This requires a bit of explanation: the basic idea is to make a
  142. dummy metaclass for one level of class instantiation that replaces
  143. itself with the actual metaclass. Because of internal type checks
  144. we also need to make sure that we downgrade the custom metaclass
  145. for one level to something closer to type (that's why __call__ and
  146. __init__ comes back from type etc.).
  147. This has the advantage over six.with_metaclass of not introducing
  148. dummy classes into the final MRO.
  149. """
  150. class metaclass(meta):
  151. __call__ = type.__call__
  152. __init__ = type.__init__
  153. def __new__(cls, name, this_bases, d):
  154. if this_bases is None:
  155. return type.__new__(cls, name, (), d)
  156. return meta(name, bases, d)
  157. return metaclass('temporary_class', None, {})
  158. class NoSlotsPyPy(type):
  159. """
  160. Workaround for PyPy not working well with __slots__ and __class__ assignment.
  161. """
  162. def __new__(meta, name, bases, dct):
  163. if PYPY and '__slots__' in dct:
  164. dct.pop('__slots__')
  165. return super(NoSlotsPyPy, meta).__new__(meta, name, bases, dct)
  166. # Local Variables:
  167. # tab-width:4
  168. # indent-tabs-mode:nil
  169. # End:
  170. # vim: set expandtab tabstop=4 shiftwidth=4: