SCons.compat.builtins

Package SCons :: Package compat :: Module builtins |
[hide private]
[frames] | no frames]

# Source Code for Module SCons.compat.builtins

      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 2725 2008/03/31 12:52:02 knight"
     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  #
    139  try:
    140      zip
    141  except NameError:
    142      # Pre-2.2 Python has no zip() function.
    143 -    def zip(*lists):
    144          """
    145          Emulates the behavior we need from the built-in zip() function
    146          added in Python 2.2.
    147
    148          Returns a list of tuples, where each tuple contains the i-th
    149          element rom each of the argument sequences.  The returned
    150          list is truncated in length to the length of the shortest
    151          argument sequence.
    152          """
    153          result = []
    154          for i in xrange(min(map(len, lists))):
    155              result.append(tuple(map(lambda l, i=i: l[i], lists)))
    156          return result
    157      __builtin__.zip = zip
    158
    159
    160
    161  #if sys.version_info[:3] in ((2, 2, 0), (2, 2, 1)):
    162  #    def lstrip(s, c=string.whitespace):
    163  #        while s and s[0] in c:
    164  #            s = s[1:]
    165  #        return s
    166  #    def rstrip(s, c=string.whitespace):
    167  #        while s and s[-1] in c:
    168  #            s = s[:-1]
    169  #        return s
    170  #    def strip(s, c=string.whitespace, l=lstrip, r=rstrip):
    171  #        return l(r(s, c), c)
    172  #
    173  #    object.__setattr__(str, 'lstrip', lstrip)
    174  #    object.__setattr__(str, 'rstrip', rstrip)
    175  #    object.__setattr__(str, 'strip', strip)
    176