Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_compat.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2016 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. import sys
  15. PY3 = sys.version_info[0] >= 3
  16. # Native string object IO
  17. if str is not bytes:
  18. from io import StringIO as NStringIO
  19. string_types = str
  20. else:
  21. # Python 2
  22. from io import BytesIO as NStringIO
  23. string_types = str, unicode
  24. NStringIO = NStringIO
  25. from io import StringIO
  26. from io import BytesIO
  27. def TextIO(text):
  28. "Return StringIO or BytesIO as appropriate"
  29. return BytesIO(text) if isinstance(text, bytes) else StringIO(text)
  30. try:
  31. import urllib2
  32. except ImportError:
  33. # Python 3 support.
  34. import urllib.request as urllib2
  35. urllib2 = urllib2
  36. try:
  37. from urllib import pathname2url
  38. except ImportError:
  39. # Python 3 support.
  40. from urllib.request import pathname2url
  41. pathname2url = pathname2url
  42. try:
  43. import urlparse as urlparse
  44. except ImportError:
  45. # Python 3 support
  46. import urllib.parse as urlparse
  47. urlparse = urlparse
  48. if PY3: # pragma: no cover
  49. import builtins
  50. exec_ = getattr(builtins, "exec")
  51. text_type = str
  52. binary_type = bytes
  53. maxsize = sys.maxsize
  54. def reraise(tp, value, tb=None): #pragma NO COVER
  55. if value.__traceback__ is not tb:
  56. raise value.with_traceback(tb)
  57. raise value
  58. else: # pragma: no cover
  59. text_type = unicode
  60. binary_type = bytes
  61. maxsize = sys.maxint
  62. def exec_(code, globs=None, locs=None): #pragma NO COVER
  63. """Execute code in a namespace."""
  64. if globs is None:
  65. frame = sys._getframe(1)
  66. globs = frame.f_globals
  67. if locs is None:
  68. locs = frame.f_locals
  69. del frame
  70. elif locs is None:
  71. locs = globs
  72. exec("""exec code in globs, locs""")
  73. exec_("""def reraise(tp, value, tb=None):
  74. raise tp, value, tb
  75. """)
  76. def raise_with_same_tb(exception):
  77. "Raise an exception having the current traceback (if there is one)"
  78. reraise(type(exception), exception, sys.exc_info()[2])
  79. import abc
  80. # workaround the metaclass diff in Py2/Py3
  81. AbstractBaseClass = abc.ABCMeta('AbstractBaseClass', (object,), {})