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 Simply enough, things that go in the builtins name space come from
39 our _scons_builtins module.
40
41 The rest of the things here will be in individual compatibility modules
42 that are either: 1) suitably modified copies of the future modules that
43 we want to use; or 2) backwards compatible re-implementations of the
44 specific portions of a future module's API that we want to use.
45
46 GENERAL WARNINGS: Implementations of functions in the SCons.compat
47 modules are *NOT* guaranteed to be fully compliant with these functions in
48 later versions of Python. We are only concerned with adding functionality
49 that we actually use in SCons, so be wary if you lift this code for
50 other uses. (That said, making these more nearly the same as later,
51 official versions is still a desirable goal, we just don't need to be
52 obsessive about it.)
53
54 We name the compatibility modules with an initial '_scons_' (for example,
55 _scons_subprocess.py is our compatibility module for subprocess) so
56 that we can still try to import the real module name and fall back to
57 our compatibility module if we get an ImportError. The import_as()
58 function defined below loads the module as the "real" name (without the
59 '_scons'), after which all of the "import {module}" statements in the
60 rest of our code will find our pre-loaded compatibility module.
61 """
62
63 __revision__ = "src/engine/SCons/compat/__init__.py 5023 2010/06/14 22:05:46 scons"
64
65 import os
66 import sys
67 import imp
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
78 """
79 Attempts to import the old module and load it under the new name.
80 Used for purely cosmetic name changes in Python 3.x.
81 """
82 try:
83 sys.modules[new] = imp.load_module(old, *imp.find_module(old))
84 return True
85 except ImportError:
86 return False
87
88
89 rename_module('builtins', '__builtin__')
90 import _scons_builtins
91
92
93 try:
94 import hashlib
95 except ImportError:
96
97 try:
98 import_as('_scons_hashlib', 'hashlib')
99 except ImportError:
100
101
102
103
104 pass
105
106 try:
107 set
108 except NameError:
109
110 import_as('_scons_sets', 'sets')
111 import builtins, sets
112 builtins.set = sets.Set
113
114
115 try:
116 import collections
117 except ImportError:
118
119 import_as('_scons_collections', 'collections')
120 else:
121 try:
122 collections.UserDict
123 except AttributeError:
124 exec('from UserDict import UserDict as _UserDict')
125 collections.UserDict = _UserDict
126 del _UserDict
127 try:
128 collections.UserList
129 except AttributeError:
130 exec('from UserList import UserList as _UserList')
131 collections.UserList = _UserList
132 del _UserList
133 try:
134 collections.UserString
135 except AttributeError:
136 exec('from UserString import UserString as _UserString')
137 collections.UserString = _UserString
138 del _UserString
139
140
141 try:
142 import io
143 except ImportError:
144
145 import_as('_scons_io', 'io')
146
147
148 try:
149 os.devnull
150 except AttributeError:
151
152 _names = sys.builtin_module_names
153 if 'posix' in _names:
154 os.devnull = '/dev/null'
155 elif 'nt' in _names:
156 os.devnull = 'nul'
157 os.path.devnull = os.devnull
158 try:
159 os.path.lexists
160 except AttributeError:
161
164 os.path.lexists = lexists
165
166
167
168
169
170 if os.environ.get('SCONS_HORRIBLE_REGRESSION_TEST_HACK') is None:
171
172
173 rename_module('pickle', 'cPickle')
174
175
176
177 rename_module('profile', 'cProfile')
178
179
180
181 rename_module('queue', 'Queue')
182
183
184
185 rename_module('winreg', '_winreg')
186
187
188 try:
189 import subprocess
190 except ImportError:
191
192 import_as('_scons_subprocess', 'subprocess')
193
194 try:
195 sys.intern
196 except AttributeError:
197
198 import builtins
199 try:
200 sys.intern = builtins.intern
201 except AttributeError:
202
205 sys.intern = intern
206 del intern
207 try:
208 sys.maxsize
209 except AttributeError:
210
211
212 sys.maxsize = (sys).maxint
213
214
215 if os.environ.get('SCONS_HORRIBLE_REGRESSION_TEST_HACK') is not None:
216
217
218
219
220
221
222
223 from types import ClassType
225 if hasattr(obj, '__call__'): return True
226 if isinstance(obj, (ClassType, type)): return True
227 return False
228 import builtins
229 builtins.callable = callable
230 del callable
231
232
233
234
235
236
237
238