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

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