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.3KB

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