OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # Util/py21compat.py : Compatibility code for Python 2.1 |
| 4 # |
| 5 # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net> |
| 6 # |
| 7 # =================================================================== |
| 8 # The contents of this file are dedicated to the public domain. To |
| 9 # the extent that dedication to the public domain is not available, |
| 10 # everyone is granted a worldwide, perpetual, royalty-free, |
| 11 # non-exclusive license to exercise all rights associated with the |
| 12 # contents of this file for any purpose whatsoever. |
| 13 # No rights are reserved. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 # SOFTWARE. |
| 23 # =================================================================== |
| 24 |
| 25 """Compatibility code for Python 2.1 |
| 26 |
| 27 Currently, this just defines: |
| 28 - True and False |
| 29 - object |
| 30 - isinstance |
| 31 """ |
| 32 |
| 33 __revision__ = "$Id$" |
| 34 __all__ = [] |
| 35 |
| 36 import sys |
| 37 import __builtin__ |
| 38 |
| 39 # 'True' and 'False' aren't defined in Python 2.1. Define them. |
| 40 try: |
| 41 True, False |
| 42 except NameError: |
| 43 (True, False) = (1, 0) |
| 44 __all__ += ['True', 'False'] |
| 45 |
| 46 # New-style classes were introduced in Python 2.2. Defining "object" in Python |
| 47 # 2.1 lets us use new-style classes in versions of Python that support them, |
| 48 # while still maintaining backward compatibility with old-style classes |
| 49 try: |
| 50 object |
| 51 except NameError: |
| 52 class object: pass |
| 53 __all__ += ['object'] |
| 54 |
| 55 # Starting with Python 2.2, isinstance allows a tuple for the second argument. |
| 56 # Also, builtins like "tuple", "list", "str", "unicode", "int", and "long" |
| 57 # became first-class types, rather than functions. We want to support |
| 58 # constructs like: |
| 59 # isinstance(x, (int, long)) |
| 60 # So we hack it for Python 2.1. |
| 61 try: |
| 62 isinstance(5, (int, long)) |
| 63 except TypeError: |
| 64 __all__ += ['isinstance'] |
| 65 _builtin_type_map = { |
| 66 tuple: type(()), |
| 67 list: type([]), |
| 68 str: type(""), |
| 69 unicode: type(u""), |
| 70 int: type(0), |
| 71 long: type(0L), |
| 72 } |
| 73 def isinstance(obj, t): |
| 74 if not __builtin__.isinstance(t, type(())): |
| 75 # t is not a tuple |
| 76 return __builtin__.isinstance(obj, _builtin_type_map.get(t, t)) |
| 77 else: |
| 78 # t is a tuple |
| 79 for typ in t: |
| 80 if __builtin__.isinstance(obj, _builtin_type_map.get(typ, typ)): |
| 81 return True |
| 82 return False |
| 83 |
| 84 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |