_compat.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. # flake8: noqa
  3. import marshal
  4. import sys
  5. PY2 = sys.version_info[0] == 2
  6. PYPY = hasattr(sys, "pypy_translation_info")
  7. _identity = lambda x: x
  8. if not PY2:
  9. unichr = chr
  10. range_type = range
  11. text_type = str
  12. string_types = (str,)
  13. integer_types = (int,)
  14. iterkeys = lambda d: iter(d.keys())
  15. itervalues = lambda d: iter(d.values())
  16. iteritems = lambda d: iter(d.items())
  17. import pickle
  18. from io import BytesIO, StringIO
  19. NativeStringIO = StringIO
  20. def reraise(tp, value, tb=None):
  21. if value.__traceback__ is not tb:
  22. raise value.with_traceback(tb)
  23. raise value
  24. ifilter = filter
  25. imap = map
  26. izip = zip
  27. intern = sys.intern
  28. implements_iterator = _identity
  29. implements_to_string = _identity
  30. encode_filename = _identity
  31. marshal_dump = marshal.dump
  32. marshal_load = marshal.load
  33. else:
  34. unichr = unichr
  35. text_type = unicode
  36. range_type = xrange
  37. string_types = (str, unicode)
  38. integer_types = (int, long)
  39. iterkeys = lambda d: d.iterkeys()
  40. itervalues = lambda d: d.itervalues()
  41. iteritems = lambda d: d.iteritems()
  42. import cPickle as pickle
  43. from cStringIO import StringIO as BytesIO, StringIO
  44. NativeStringIO = BytesIO
  45. exec("def reraise(tp, value, tb=None):\n raise tp, value, tb")
  46. from itertools import imap, izip, ifilter
  47. intern = intern
  48. def implements_iterator(cls):
  49. cls.next = cls.__next__
  50. del cls.__next__
  51. return cls
  52. def implements_to_string(cls):
  53. cls.__unicode__ = cls.__str__
  54. cls.__str__ = lambda x: x.__unicode__().encode("utf-8")
  55. return cls
  56. def encode_filename(filename):
  57. if isinstance(filename, unicode):
  58. return filename.encode("utf-8")
  59. return filename
  60. def marshal_dump(code, f):
  61. if isinstance(f, file):
  62. marshal.dump(code, f)
  63. else:
  64. f.write(marshal.dumps(code))
  65. def marshal_load(f):
  66. if isinstance(f, file):
  67. return marshal.load(f)
  68. return marshal.loads(f.read())
  69. def with_metaclass(meta, *bases):
  70. """Create a base class with a metaclass."""
  71. # This requires a bit of explanation: the basic idea is to make a
  72. # dummy metaclass for one level of class instantiation that replaces
  73. # itself with the actual metaclass.
  74. class metaclass(type):
  75. def __new__(cls, name, this_bases, d):
  76. return meta(name, bases, d)
  77. return type.__new__(metaclass, "temporary_class", (), {})
  78. try:
  79. from urllib.parse import quote_from_bytes as url_quote
  80. except ImportError:
  81. from urllib import quote as url_quote
  82. try:
  83. from collections import abc
  84. except ImportError:
  85. import collections as abc
  86. try:
  87. from os import fspath
  88. except ImportError:
  89. try:
  90. from pathlib import PurePath
  91. except ImportError:
  92. PurePath = None
  93. def fspath(path):
  94. if hasattr(path, "__fspath__"):
  95. return path.__fspath__()
  96. # Python 3.5 doesn't have __fspath__ yet, use str.
  97. if PurePath is not None and isinstance(path, PurePath):
  98. return str(path)
  99. return path