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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from __future__ import absolute_import
  2. import errno
  3. import os
  4. import sys
  5. from .five import range
  6. if sys.platform == 'win32':
  7. try:
  8. import _winapi # noqa
  9. except ImportError: # pragma: no cover
  10. try:
  11. from _billiard import win32 as _winapi # noqa
  12. except (ImportError, AttributeError):
  13. from _multiprocessing import win32 as _winapi # noqa
  14. else:
  15. _winapi = None # noqa
  16. if sys.version_info > (2, 7, 5):
  17. buf_t, is_new_buffer = memoryview, True # noqa
  18. else:
  19. buf_t, is_new_buffer = buffer, False # noqa
  20. if hasattr(os, 'write'):
  21. __write__ = os.write
  22. if is_new_buffer:
  23. def send_offset(fd, buf, offset):
  24. return __write__(fd, buf[offset:])
  25. else: # Py<2.7.6
  26. def send_offset(fd, buf, offset): # noqa
  27. return __write__(fd, buf_t(buf, offset))
  28. else: # non-posix platform
  29. def send_offset(fd, buf, offset): # noqa
  30. raise NotImplementedError('send_offset')
  31. if sys.version_info[0] == 3:
  32. bytes = bytes
  33. else:
  34. _bytes = bytes
  35. # the 'bytes' alias in Python2 does not support an encoding argument.
  36. class bytes(_bytes): # noqa
  37. def __new__(cls, *args):
  38. if len(args) > 1:
  39. return _bytes(args[0]).encode(*args[1:])
  40. return _bytes(*args)
  41. try:
  42. closerange = os.closerange
  43. except AttributeError:
  44. def closerange(fd_low, fd_high): # noqa
  45. for fd in reversed(range(fd_low, fd_high)):
  46. try:
  47. os.close(fd)
  48. except OSError as exc:
  49. if exc.errno != errno.EBADF:
  50. raise
  51. def get_errno(exc):
  52. """:exc:`socket.error` and :exc:`IOError` first got
  53. the ``.errno`` attribute in Py2.7"""
  54. try:
  55. return exc.errno
  56. except AttributeError:
  57. try:
  58. # e.args = (errno, reason)
  59. if isinstance(exc.args, tuple) and len(exc.args) == 2:
  60. return exc.args[0]
  61. except AttributeError:
  62. pass
  63. return 0
  64. if sys.platform == 'win32':
  65. def setblocking(handle, blocking):
  66. raise NotImplementedError('setblocking not implemented on win32')
  67. def isblocking(handle):
  68. raise NotImplementedError('isblocking not implemented on win32')
  69. else:
  70. from os import O_NONBLOCK
  71. from fcntl import fcntl, F_GETFL, F_SETFL
  72. def isblocking(handle): # noqa
  73. return not (fcntl(handle, F_GETFL) & O_NONBLOCK)
  74. def setblocking(handle, blocking): # noqa
  75. flags = fcntl(handle, F_GETFL, 0)
  76. fcntl(
  77. handle, F_SETFL,
  78. flags & (~O_NONBLOCK) if blocking else flags | O_NONBLOCK,
  79. )