8ae38b6ffabee6fa970f6b616c32292c20399f76
[senf.git] / scons / scons-1.2.0 / engine / SCons / compat / builtins.py
1 #
2 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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
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         all()
39         any()
40         bool()
41         dict()
42         True
43         False
44         zip()
45
46 Implementations of functions are *NOT* guaranteed to be fully compliant
47 with these functions in later versions of Python.  We are only concerned
48 with adding functionality that we actually use in SCons, so be wary
49 if you lift this code for other uses.  (That said, making these more
50 nearly the same as later, official versions is still a desirable goal,
51 we just don't need to be obsessive about it.)
52
53 If you're looking at this with pydoc and various names don't show up in
54 the FUNCTIONS or DATA output, that means those names are already built in
55 to this version of Python and we don't need to add them from this module.
56 """
57
58 __revision__ = "src/engine/SCons/compat/builtins.py 3842 2008/12/20 22:59:52 scons"
59
60 import __builtin__
61
62 try:
63     all
64 except NameError:
65     # Pre-2.5 Python has no all() function.
66     def all(iterable):
67         """
68         Returns True if all elements of the iterable are true.
69         """
70         for element in iterable:
71             if not element:
72                 return False
73         return True
74     __builtin__.all = all
75     all = all
76
77 try:
78     any
79 except NameError:
80     # Pre-2.5 Python has no any() function.
81     def any(iterable):
82         """
83         Returns True if any element of the iterable is true.
84         """
85         for element in iterable:
86             if element:
87                 return True
88         return False
89     __builtin__.any = any
90     any = any
91
92 try:
93     bool
94 except NameError:
95     # Pre-2.2 Python has no bool() function.
96     def bool(value):
97         """Demote a value to 0 or 1, depending on its truth value.
98
99         This is not to be confused with types.BooleanType, which is
100         way too hard to duplicate in early Python versions to be
101         worth the trouble.
102         """
103         return not not value
104     __builtin__.bool = bool
105     bool = bool
106
107 try:
108     dict
109 except NameError:
110     # Pre-2.2 Python has no dict() keyword.
111     def dict(seq=[], **kwargs):
112         """
113         New dictionary initialization.
114         """
115         d = {}
116         for k, v in seq:
117             d[k] = v
118         d.update(kwargs)
119         return d
120     __builtin__.dict = dict
121
122 try:
123     False
124 except NameError:
125     # Pre-2.2 Python has no False keyword.
126     __builtin__.False = not 1
127     # Assign to False in this module namespace so it shows up in pydoc output.
128     False = False
129
130 try:
131     True
132 except NameError:
133     # Pre-2.2 Python has no True keyword.
134     __builtin__.True = not 0
135     # Assign to True in this module namespace so it shows up in pydoc output.
136     True = True
137
138 try:
139     file
140 except NameError:
141     # Pre-2.2 Python has no file() function.
142     __builtin__.file = open
143
144 #
145 try:
146     zip
147 except NameError:
148     # Pre-2.2 Python has no zip() function.
149     def zip(*lists):
150         """
151         Emulates the behavior we need from the built-in zip() function
152         added in Python 2.2.
153
154         Returns a list of tuples, where each tuple contains the i-th
155         element rom each of the argument sequences.  The returned
156         list is truncated in length to the length of the shortest
157         argument sequence.
158         """
159         result = []
160         for i in xrange(min(map(len, lists))):
161             result.append(tuple(map(lambda l, i=i: l[i], lists)))
162         return result
163     __builtin__.zip = zip
164
165
166
167 #if sys.version_info[:3] in ((2, 2, 0), (2, 2, 1)):
168 #    def lstrip(s, c=string.whitespace):
169 #        while s and s[0] in c:
170 #            s = s[1:]
171 #        return s
172 #    def rstrip(s, c=string.whitespace):
173 #        while s and s[-1] in c:
174 #            s = s[:-1]
175 #        return s
176 #    def strip(s, c=string.whitespace, l=lstrip, r=rstrip):
177 #        return l(r(s, c), c)
178 #
179 #    object.__setattr__(str, 'lstrip', lstrip)
180 #    object.__setattr__(str, 'rstrip', rstrip)
181 #    object.__setattr__(str, 'strip', strip)