Merged revisions 1907-1940,1942-1967 via svnmerge from
[scons.git] / src / engine / SCons / compat / builtins.py
1 #
2 # __COPYRIGHT__
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
24 # Portions of the following are derived from the compat.py file in
25 # Twisted, under the following copyright:
26 #
27 # Copyright (c) 2001-2004 Twisted Matrix Laboratories
28
29 __doc__ = """
30 Compatibility idioms for __builtin__ names
31
32 This module adds names to the __builtin__ module for things that we want
33 to use in SCons but which don't show up until later Python versions than
34 the earliest ones we support.
35
36 This module checks for the following __builtin__ names:
37
38         bool()
39         dict()
40         True
41         False
42         zip()
43
44 Implementations of functions are *NOT* guaranteed to be fully compliant
45 with these functions in later versions of Python.  We are only concerned
46 with adding functionality that we actually use in SCons, so be wary
47 if you lift this code for other uses.  (That said, making these more
48 nearly the same as later, official versions is still a desirable goal,
49 we just don't need to be obsessive about it.)
50
51 If you're looking at this with pydoc and various names don't show up in
52 the FUNCTIONS or DATA output, that means those names are already built in
53 to this version of Python and we don't need to add them from this module.
54 """
55
56 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
57
58 import __builtin__
59
60 try:
61     bool
62 except NameError:
63     # Pre-2.2 Python has no bool() function.
64     def bool(value):
65         """Demote a value to 0 or 1, depending on its truth value.
66
67         This is not to be confused with types.BooleanType, which is
68         way too hard to duplicate in early Python versions to be
69         worth the trouble.
70         """
71         return not not value
72     __builtin__.bool = bool
73     bool = bool
74
75 try:
76     dict
77 except NameError:
78     # Pre-2.2 Python has no dict() keyword.
79     def dict(seq=[], **kwargs):
80         """
81         New dictionary initialization.
82         """
83         d = {}
84         for k, v in seq:
85             d[k] = v
86         d.update(kwargs)
87         return d
88     __builtin__.dict = dict
89
90 try:
91     False
92 except NameError:
93     # Pre-2.2 Python has no False keyword.
94     __builtin__.False = not 1
95     # Assign to False in this module namespace so it shows up in pydoc output.
96     False = False
97
98 try:
99     True
100 except NameError:
101     # Pre-2.2 Python has no True keyword.
102     __builtin__.True = not 0
103     # Assign to True in this module namespace so it shows up in pydoc output.
104     True = True
105
106 #
107 try:
108     zip
109 except NameError:
110     # Pre-2.2 Python has no zip() function.
111     def zip(*lists):
112         """
113         Emulates the behavior we need from the built-in zip() function
114         added in Python 2.2.
115
116         Returns a list of tuples, where each tuple contains the i-th
117         element rom each of the argument sequences.  The returned
118         list is truncated in length to the length of the shortest
119         argument sequence.
120         """
121         result = []
122         for i in xrange(min(map(len, lists))):
123             result.append(tuple(map(lambda l, i=i: l[i], lists)))
124         return result
125     __builtin__.zip = zip
126
127
128
129 #if sys.version_info[:3] in ((2, 2, 0), (2, 2, 1)):
130 #    def lstrip(s, c=string.whitespace):
131 #        while s and s[0] in c:
132 #            s = s[1:]
133 #        return s
134 #    def rstrip(s, c=string.whitespace):
135 #        while s and s[-1] in c:
136 #            s = s[:-1]
137 #        return s
138 #    def strip(s, c=string.whitespace, l=lstrip, r=rstrip):
139 #        return l(r(s, c), c)
140 #
141 #    object.__setattr__(str, 'lstrip', lstrip)
142 #    object.__setattr__(str, 'rstrip', rstrip)
143 #    object.__setattr__(str, 'strip', strip)