1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 __doc__ = """
25 SCons compatibility package for old Python versions
26
27 This subpackage holds modules that provide backwards-compatible
28 implementations of various things that we'd like to use in SCons but which
29 only show up in later versions of Python than the early, old version(s)
30 we still support.
31
32 Other code will not generally reference things in this package through
33 the SCons.compat namespace. The modules included here add things to
34 the builtins namespace or the global module list so that the rest
35 of our code can use the objects and names imported here regardless of
36 Python version.
37
38 The rest of the things here will be in individual compatibility modules
39 that are either: 1) suitably modified copies of the future modules that
40 we want to use; or 2) backwards compatible re-implementations of the
41 specific portions of a future module's API that we want to use.
42
43 GENERAL WARNINGS: Implementations of functions in the SCons.compat
44 modules are *NOT* guaranteed to be fully compliant with these functions in
45 later versions of Python. We are only concerned with adding functionality
46 that we actually use in SCons, so be wary if you lift this code for
47 other uses. (That said, making these more nearly the same as later,
48 official versions is still a desirable goal, we just don't need to be
49 obsessive about it.)
50
51 We name the compatibility modules with an initial '_scons_' (for example,
52 _scons_subprocess.py is our compatibility module for subprocess) so
53 that we can still try to import the real module name and fall back to
54 our compatibility module if we get an ImportError. The import_as()
55 function defined below loads the module as the "real" name (without the
56 '_scons'), after which all of the "import {module}" statements in the
57 rest of our code will find our pre-loaded compatibility module.
58 """
59
60 __revision__ = "src/engine/SCons/compat/__init__.py a56bbd8c09fb219ab8a9673330ffcd55279219d0 2019-03-26 23:16:31 bdeegan"
61
62 import os
63 import sys
64 import imp
65
66 PYPY = hasattr(sys, 'pypy_translation_info')
67
68
70 """
71 Imports the specified module (from our local directory) as the
72 specified name, returning the loaded module object.
73 """
74 dir = os.path.split(__file__)[0]
75 return imp.load_module(name, *imp.find_module(module, [dir]))
76
77
79 """
80 Attempts to import the old module and load it under the new name.
81 Used for purely cosmetic name changes in Python 3.x.
82 """
83 try:
84 sys.modules[new] = imp.load_module(old, *imp.find_module(old))
85 return True
86 except ImportError:
87 return False
88
89
90
91
92 rename_module('pickle', 'cPickle')
93
94
95
96
97 import pickle
98
99
100
101 PICKLE_PROTOCOL = pickle.HIGHEST_PROTOCOL
102
103
104
105 rename_module('profile', 'cProfile')
106
107
108
109 rename_module('queue', 'Queue')
110
111
112
113 rename_module('winreg', '_winreg')
114
115
116
117
118 try:
119 sys.intern
120 except AttributeError:
121
122
123 sys.intern = intern
124
125
126
127 import collections
128
129 try:
130 collections.UserDict
131 except AttributeError:
132 exec ('from UserDict import UserDict as _UserDict')
133 collections.UserDict = _UserDict
134 del _UserDict
135
136 try:
137 collections.UserList
138 except AttributeError:
139 exec ('from UserList import UserList as _UserList')
140 collections.UserList = _UserList
141 del _UserList
142
143 try:
144 collections.UserString
145 except AttributeError:
146 exec ('from UserString import UserString as _UserString')
147 collections.UserString = _UserString
148 del _UserString
149
150
151 import shutil
152 try:
153 shutil.SameFileError
154 except AttributeError:
157
158 shutil.SameFileError = SameFileError
159
194
195 return metaclass('temporary_class', None, {})
196
197
199 """
200 Workaround for PyPy not working well with __slots__ and __class__ assignment.
201 """
202
203 - def __new__(meta, name, bases, dct):
207
208
209
210
211
212
213