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 This package will be imported by other code:
33
34 import SCons.compat
35
36 But other code will not generally reference things in this package through
37 the SCons.compat namespace. The modules included here add things to
38 the __builtin__ namespace or the global module list so that the rest
39 of our code can use the objects and names imported here regardless of
40 Python version.
41
42 Simply enough, things that go in the __builtin__ name space come from
43 our builtins module.
44
45 The rest of the things here will be in individual compatibility modules
46 that are either: 1) suitably modified copies of the future modules that
47 we want to use; or 2) backwards compatible re-implementations of the
48 specific portions of a future module's API that we want to use.
49
50 GENERAL WARNINGS: Implementations of functions in the SCons.compat
51 modules are *NOT* guaranteed to be fully compliant with these functions in
52 later versions of Python. We are only concerned with adding functionality
53 that we actually use in SCons, so be wary if you lift this code for
54 other uses. (That said, making these more nearly the same as later,
55 official versions is still a desirable goal, we just don't need to be
56 obsessive about it.)
57
58 We name the compatibility modules with an initial '_scons_' (for example,
59 _scons_subprocess.py is our compatibility module for subprocess) so
60 that we can still try to import the real module name and fall back to
61 our compatibility module if we get an ImportError. The import_as()
62 function defined below loads the module as the "real" name (without the
63 '_scons'), after which all of the "import {module}" statements in the
64 rest of our code will find our pre-loaded compatibility module.
65 """
66
67 __revision__ = "src/engine/SCons/compat/__init__.py 3266 2008/08/12 07:31:01 knight"
68
70 """
71 Imports the specified module (from our local directory) as the
72 specified name.
73 """
74 import imp
75 import os.path
76 dir = os.path.split(__file__)[0]
77 file, filename, suffix_mode_type = imp.find_module(module, [dir])
78 imp.load_module(name, file, filename, suffix_mode_type)
79
80 import builtins
81
82 try:
83 import hashlib
84 except ImportError:
85
86 try:
87 import_as('_scons_hashlib', 'hashlib')
88 except ImportError:
89
90
91
92
93 pass
94
95 try:
96 set
97 except NameError:
98
99 try:
100
101
102 import_as('_scons_sets', 'sets')
103 except (ImportError, SyntaxError):
104
105
106
107
108
109 import_as('_scons_sets15', 'sets')
110 import __builtin__
111 import sets
112 __builtin__.set = sets.Set
113
114 import fnmatch
115 try:
116 fnmatch.filter
117 except AttributeError:
118
139 fnmatch.filter = filter
140 del filter
141
142 try:
143 import itertools
144 except ImportError:
145
146 import_as('_scons_itertools', 'itertools')
147
148
149
150 try:
151 import textwrap
152 except ImportError:
153
154 import_as('_scons_textwrap', 'textwrap')
155
156 try:
157 import optparse
158 except ImportError:
159
160 import_as('_scons_optparse', 'optparse')
161
162 import shlex
163 try:
164 shlex.split
165 except AttributeError:
166
167
168
169
170
171
172 del shlex
173 import_as('_scons_shlex', 'shlex')
174
175 try:
176 import subprocess
177 except ImportError:
178
179 import_as('_scons_subprocess', 'subprocess')
180
181 import sys
182 try:
183 sys.version_info
184 except AttributeError:
185
186 import string
187 version_string = string.split(sys.version)[0]
188 version_ints = map(int, string.split(version_string, '.'))
189 sys.version_info = tuple(version_ints + ['final', 0])
190
191 try:
192 import UserString
193 except ImportError:
194
195 import_as('_scons_UserString', 'UserString')
196