py21compat.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. """Compatibility code for Python 2.1
  25. Currently, this just defines:
  26. - True and False
  27. - object
  28. - isinstance
  29. """
  30. __revision__ = "$Id$"
  31. __all__ = []
  32. import sys
  33. import __builtin__
  34. # 'True' and 'False' aren't defined in Python 2.1. Define them.
  35. try:
  36. True, False
  37. except NameError:
  38. (True, False) = (1, 0)
  39. __all__ += ['True', 'False']
  40. # New-style classes were introduced in Python 2.2. Defining "object" in Python
  41. # 2.1 lets us use new-style classes in versions of Python that support them,
  42. # while still maintaining backward compatibility with old-style classes
  43. try:
  44. object
  45. except NameError:
  46. class object: pass
  47. __all__ += ['object']
  48. # Starting with Python 2.2, isinstance allows a tuple for the second argument.
  49. # Also, builtins like "tuple", "list", "str", "unicode", "int", and "long"
  50. # became first-class types, rather than functions. We want to support
  51. # constructs like:
  52. # isinstance(x, (int, long))
  53. # So we hack it for Python 2.1.
  54. try:
  55. isinstance(5, (int, long))
  56. except TypeError:
  57. __all__ += ['isinstance']
  58. _builtin_type_map = {
  59. tuple: type(()),
  60. list: type([]),
  61. str: type(""),
  62. unicode: type(u""),
  63. int: type(0),
  64. long: type(0L),
  65. }
  66. def isinstance(obj, t):
  67. if not __builtin__.isinstance(t, type(())):
  68. # t is not a tuple
  69. return __builtin__.isinstance(obj, _builtin_type_map.get(t, t))
  70. else:
  71. # t is a tuple
  72. for typ in t:
  73. if __builtin__.isinstance(obj, _builtin_type_map.get(typ, typ)):
  74. return True
  75. return False
  76. # vim:set ts=4 sw=4 sts=4 expandtab: