py3compat.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright 2009-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you
  4. # may not use this file except in compliance with the License. You
  5. # may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied. See the License for the specific language governing
  13. # permissions and limitations under the License.
  14. """Utility functions and definitions for python3 compatibility."""
  15. import sys
  16. PY3 = sys.version_info[0] == 3
  17. if PY3:
  18. import codecs
  19. import collections.abc as abc
  20. import _thread as thread
  21. from abc import ABC, abstractmethod
  22. from io import BytesIO as StringIO
  23. def abstractproperty(func):
  24. return property(abstractmethod(func))
  25. MAXSIZE = sys.maxsize
  26. imap = map
  27. def b(s):
  28. # BSON and socket operations deal in binary data. In
  29. # python 3 that means instances of `bytes`. In python
  30. # 2.7 you can create an alias for `bytes` using
  31. # the b prefix (e.g. b'foo').
  32. # See http://python3porting.com/problems.html#nicer-solutions
  33. return codecs.latin_1_encode(s)[0]
  34. def bytes_from_hex(h):
  35. return bytes.fromhex(h)
  36. def iteritems(d):
  37. return iter(d.items())
  38. def itervalues(d):
  39. return iter(d.values())
  40. def reraise(exctype, value, trace=None):
  41. raise exctype(str(value)).with_traceback(trace)
  42. def reraise_instance(exc_instance, trace=None):
  43. raise exc_instance.with_traceback(trace)
  44. def _unicode(s):
  45. return s
  46. text_type = str
  47. string_type = str
  48. integer_types = int
  49. else:
  50. import collections as abc
  51. import thread
  52. from abc import ABCMeta, abstractproperty
  53. from itertools import imap
  54. try:
  55. from cStringIO import StringIO
  56. except ImportError:
  57. from StringIO import StringIO
  58. ABC = ABCMeta('ABC', (object,), {})
  59. MAXSIZE = sys.maxint
  60. def b(s):
  61. # See comments above. In python 2.x b('foo') is just 'foo'.
  62. return s
  63. def bytes_from_hex(h):
  64. return h.decode('hex')
  65. def iteritems(d):
  66. return d.iteritems()
  67. def itervalues(d):
  68. return d.itervalues()
  69. def reraise(exctype, value, trace=None):
  70. _reraise(exctype, str(value), trace)
  71. def reraise_instance(exc_instance, trace=None):
  72. _reraise(exc_instance, None, trace)
  73. # "raise x, y, z" raises SyntaxError in Python 3
  74. exec("""def _reraise(exc, value, trace):
  75. raise exc, value, trace
  76. """)
  77. _unicode = unicode
  78. string_type = basestring
  79. text_type = unicode
  80. integer_types = (int, long)