_compat.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2._compat
  4. ~~~~~~~~~~~~~~
  5. Some py2/py3 compatibility support based on a stripped down
  6. version of six so we don't have to depend on a specific version
  7. of it.
  8. :copyright: Copyright 2013 by the Jinja team, see AUTHORS.
  9. :license: BSD, see LICENSE for details.
  10. """
  11. import sys
  12. PY2 = sys.version_info[0] == 2
  13. PYPY = hasattr(sys, 'pypy_translation_info')
  14. _identity = lambda x: x
  15. if not PY2:
  16. unichr = chr
  17. range_type = range
  18. text_type = str
  19. string_types = (str,)
  20. integer_types = (int,)
  21. iterkeys = lambda d: iter(d.keys())
  22. itervalues = lambda d: iter(d.values())
  23. iteritems = lambda d: iter(d.items())
  24. import pickle
  25. from io import BytesIO, StringIO
  26. NativeStringIO = StringIO
  27. def reraise(tp, value, tb=None):
  28. if value.__traceback__ is not tb:
  29. raise value.with_traceback(tb)
  30. raise value
  31. ifilter = filter
  32. imap = map
  33. izip = zip
  34. intern = sys.intern
  35. implements_iterator = _identity
  36. implements_to_string = _identity
  37. encode_filename = _identity
  38. get_next = lambda x: x.__next__
  39. else:
  40. unichr = unichr
  41. text_type = unicode
  42. range_type = xrange
  43. string_types = (str, unicode)
  44. integer_types = (int, long)
  45. iterkeys = lambda d: d.iterkeys()
  46. itervalues = lambda d: d.itervalues()
  47. iteritems = lambda d: d.iteritems()
  48. import cPickle as pickle
  49. from cStringIO import StringIO as BytesIO, StringIO
  50. NativeStringIO = BytesIO
  51. exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
  52. from itertools import imap, izip, ifilter
  53. intern = intern
  54. def implements_iterator(cls):
  55. cls.next = cls.__next__
  56. del cls.__next__
  57. return cls
  58. def implements_to_string(cls):
  59. cls.__unicode__ = cls.__str__
  60. cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
  61. return cls
  62. get_next = lambda x: x.next
  63. def encode_filename(filename):
  64. if isinstance(filename, unicode):
  65. return filename.encode('utf-8')
  66. return filename
  67. def with_metaclass(meta, *bases):
  68. # This requires a bit of explanation: the basic idea is to make a
  69. # dummy metaclass for one level of class instanciation that replaces
  70. # itself with the actual metaclass. Because of internal type checks
  71. # we also need to make sure that we downgrade the custom metaclass
  72. # for one level to something closer to type (that's why __call__ and
  73. # __init__ comes back from type etc.).
  74. #
  75. # This has the advantage over six.with_metaclass in that it does not
  76. # introduce dummy classes into the final MRO.
  77. class metaclass(meta):
  78. __call__ = type.__call__
  79. __init__ = type.__init__
  80. def __new__(cls, name, this_bases, d):
  81. if this_bases is None:
  82. return type.__new__(cls, name, (), d)
  83. return meta(name, bases, d)
  84. return metaclass('temporary_class', None, {})
  85. try:
  86. from urllib.parse import quote_from_bytes as url_quote
  87. except ImportError:
  88. from urllib import quote as url_quote