_asyncbackend.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # This is a nullcontext for both sync and async. 3.7 has a nullcontext,
  3. # but it is only for sync use.
  4. class NullContext:
  5. def __init__(self, enter_result=None):
  6. self.enter_result = enter_result
  7. def __enter__(self):
  8. return self.enter_result
  9. def __exit__(self, exc_type, exc_value, traceback):
  10. pass
  11. async def __aenter__(self):
  12. return self.enter_result
  13. async def __aexit__(self, exc_type, exc_value, traceback):
  14. pass
  15. # These are declared here so backends can import them without creating
  16. # circular dependencies with dns.asyncbackend.
  17. class Socket: # pragma: no cover
  18. def __init__(self, family: int, type: int):
  19. self.family = family
  20. self.type = type
  21. async def close(self):
  22. pass
  23. async def getpeername(self):
  24. raise NotImplementedError
  25. async def getsockname(self):
  26. raise NotImplementedError
  27. async def getpeercert(self, timeout):
  28. raise NotImplementedError
  29. async def __aenter__(self):
  30. return self
  31. async def __aexit__(self, exc_type, exc_value, traceback):
  32. await self.close()
  33. class DatagramSocket(Socket): # pragma: no cover
  34. async def sendto(self, what, destination, timeout):
  35. raise NotImplementedError
  36. async def recvfrom(self, size, timeout):
  37. raise NotImplementedError
  38. class StreamSocket(Socket): # pragma: no cover
  39. async def sendall(self, what, timeout):
  40. raise NotImplementedError
  41. async def recv(self, size, timeout):
  42. raise NotImplementedError
  43. class NullTransport:
  44. async def connect_tcp(self, host, port, timeout, local_address):
  45. raise NotImplementedError
  46. class Backend: # pragma: no cover
  47. def name(self):
  48. return "unknown"
  49. async def make_socket(
  50. self,
  51. af,
  52. socktype,
  53. proto=0,
  54. source=None,
  55. destination=None,
  56. timeout=None,
  57. ssl_context=None,
  58. server_hostname=None,
  59. ):
  60. raise NotImplementedError
  61. def datagram_connection_required(self):
  62. return False
  63. async def sleep(self, interval):
  64. raise NotImplementedError
  65. def get_transport_class(self):
  66. raise NotImplementedError
  67. async def wait_for(self, awaitable, timeout):
  68. raise NotImplementedError