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.

five.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.five
  4. ~~~~~~~~~~~
  5. Compatibility implementations of features
  6. only available in newer Python versions.
  7. """
  8. from __future__ import absolute_import
  9. # ############# py3k #########################################################
  10. import sys
  11. PY3 = sys.version_info[0] == 3
  12. try:
  13. reload = reload # noqa
  14. except NameError: # pragma: no cover
  15. from imp import reload # noqa
  16. try:
  17. from collections import UserList # noqa
  18. except ImportError: # pragma: no cover
  19. from UserList import UserList # noqa
  20. try:
  21. from collections import UserDict # noqa
  22. except ImportError: # pragma: no cover
  23. from UserDict import UserDict # noqa
  24. try:
  25. bytes_t = bytes
  26. except NameError: # pragma: no cover
  27. bytes_t = str # noqa
  28. # ############# time.monotonic ###############################################
  29. if sys.version_info < (3, 3):
  30. import platform
  31. SYSTEM = platform.system()
  32. try:
  33. import ctypes
  34. except ImportError: # pragma: no cover
  35. ctypes = None # noqa
  36. if SYSTEM == 'Darwin' and ctypes is not None:
  37. from ctypes.util import find_library
  38. libSystem = ctypes.CDLL(find_library('libSystem.dylib'))
  39. CoreServices = ctypes.CDLL(find_library('CoreServices'),
  40. use_errno=True)
  41. mach_absolute_time = libSystem.mach_absolute_time
  42. mach_absolute_time.restype = ctypes.c_uint64
  43. absolute_to_nanoseconds = CoreServices.AbsoluteToNanoseconds
  44. absolute_to_nanoseconds.restype = ctypes.c_uint64
  45. absolute_to_nanoseconds.argtypes = [ctypes.c_uint64]
  46. def _monotonic():
  47. return absolute_to_nanoseconds(mach_absolute_time()) * 1e-9
  48. elif SYSTEM == 'Linux' and ctypes is not None:
  49. # from stackoverflow:
  50. # questions/1205722/how-do-i-get-monotonic-time-durations-in-python
  51. import os
  52. CLOCK_MONOTONIC = 1 # see <linux/time.h>
  53. class timespec(ctypes.Structure):
  54. _fields_ = [
  55. ('tv_sec', ctypes.c_long),
  56. ('tv_nsec', ctypes.c_long),
  57. ]
  58. librt = ctypes.CDLL('librt.so.1', use_errno=True)
  59. clock_gettime = librt.clock_gettime
  60. clock_gettime.argtypes = [
  61. ctypes.c_int, ctypes.POINTER(timespec),
  62. ]
  63. def _monotonic(): # noqa
  64. t = timespec()
  65. if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
  66. errno_ = ctypes.get_errno()
  67. raise OSError(errno_, os.strerror(errno_))
  68. return t.tv_sec + t.tv_nsec * 1e-9
  69. else:
  70. from time import time as _monotonic
  71. try:
  72. from time import monotonic
  73. except ImportError:
  74. monotonic = _monotonic # noqa
  75. # ############# Py3 <-> Py2 ##################################################
  76. if PY3: # pragma: no cover
  77. import builtins
  78. from queue import Queue, Empty, Full, LifoQueue
  79. from itertools import zip_longest
  80. from io import StringIO, BytesIO
  81. map = map
  82. zip = zip
  83. string = str
  84. string_t = str
  85. long_t = int
  86. text_t = str
  87. range = range
  88. module_name_t = str
  89. open_fqdn = 'builtins.open'
  90. def items(d):
  91. return d.items()
  92. def keys(d):
  93. return d.keys()
  94. def values(d):
  95. return d.values()
  96. def nextfun(it):
  97. return it.__next__
  98. exec_ = getattr(builtins, 'exec')
  99. def reraise(tp, value, tb=None):
  100. if value.__traceback__ is not tb:
  101. raise value.with_traceback(tb)
  102. raise value
  103. class WhateverIO(StringIO):
  104. def write(self, data):
  105. if isinstance(data, bytes):
  106. data = data.encode()
  107. StringIO.write(self, data)
  108. else:
  109. import __builtin__ as builtins # noqa
  110. from Queue import Queue, Empty, Full, LifoQueue # noqa
  111. from itertools import ( # noqa
  112. imap as map,
  113. izip as zip,
  114. izip_longest as zip_longest,
  115. )
  116. try:
  117. from cStringIO import StringIO # noqa
  118. except ImportError: # pragma: no cover
  119. from StringIO import StringIO # noqa
  120. string = unicode # noqa
  121. string_t = basestring # noqa
  122. text_t = unicode
  123. long_t = long # noqa
  124. range = xrange
  125. module_name_t = str
  126. open_fqdn = '__builtin__.open'
  127. def items(d): # noqa
  128. return d.iteritems()
  129. def keys(d): # noqa
  130. return d.iterkeys()
  131. def values(d): # noqa
  132. return d.itervalues()
  133. def nextfun(it): # noqa
  134. return it.next
  135. def exec_(code, globs=None, locs=None): # pragma: no cover
  136. """Execute code in a namespace."""
  137. if globs is None:
  138. frame = sys._getframe(1)
  139. globs = frame.f_globals
  140. if locs is None:
  141. locs = frame.f_locals
  142. del frame
  143. elif locs is None:
  144. locs = globs
  145. exec("""exec code in globs, locs""")
  146. exec_("""def reraise(tp, value, tb=None): raise tp, value, tb""")
  147. BytesIO = WhateverIO = StringIO # noqa
  148. def with_metaclass(Type, skip_attrs=set(['__dict__', '__weakref__'])):
  149. """Class decorator to set metaclass.
  150. Works with both Python 3 and Python 3 and it does not add
  151. an extra class in the lookup order like ``six.with_metaclass`` does
  152. (that is -- it copies the original class instead of using inheritance).
  153. """
  154. def _clone_with_metaclass(Class):
  155. attrs = dict((key, value) for key, value in items(vars(Class))
  156. if key not in skip_attrs)
  157. return Type(Class.__name__, Class.__bases__, attrs)
  158. return _clone_with_metaclass