@ajksunkang-aios/kgraph-linux-x64 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/bin/kgraph-launcher +15 -3
  2. package/lib/kgraph/scripts/build-bundle.sh +17 -4
  3. package/lib/site-packages/outcome/__init__.py +20 -0
  4. package/lib/site-packages/outcome/_impl.py +239 -0
  5. package/lib/site-packages/outcome/_util.py +33 -0
  6. package/lib/site-packages/outcome/_version.py +7 -0
  7. package/lib/site-packages/outcome/py.typed +0 -0
  8. package/lib/site-packages/outcome-1.3.0.post0.dist-info/INSTALLER +1 -0
  9. package/lib/site-packages/outcome-1.3.0.post0.dist-info/LICENSE +3 -0
  10. package/lib/site-packages/outcome-1.3.0.post0.dist-info/LICENSE.APACHE2 +202 -0
  11. package/lib/site-packages/outcome-1.3.0.post0.dist-info/LICENSE.MIT +20 -0
  12. package/lib/site-packages/outcome-1.3.0.post0.dist-info/METADATA +63 -0
  13. package/lib/site-packages/outcome-1.3.0.post0.dist-info/RECORD +13 -0
  14. package/lib/site-packages/outcome-1.3.0.post0.dist-info/WHEEL +6 -0
  15. package/lib/site-packages/outcome-1.3.0.post0.dist-info/top_level.txt +1 -0
  16. package/lib/site-packages/sniffio/__init__.py +17 -0
  17. package/lib/site-packages/sniffio/_impl.py +95 -0
  18. package/lib/site-packages/sniffio/_tests/__init__.py +0 -0
  19. package/lib/site-packages/sniffio/_tests/test_sniffio.py +84 -0
  20. package/lib/site-packages/sniffio/_version.py +3 -0
  21. package/lib/site-packages/sniffio/py.typed +0 -0
  22. package/lib/site-packages/sniffio-1.3.1.dist-info/INSTALLER +1 -0
  23. package/lib/site-packages/sniffio-1.3.1.dist-info/LICENSE +3 -0
  24. package/lib/site-packages/sniffio-1.3.1.dist-info/LICENSE.APACHE2 +202 -0
  25. package/lib/site-packages/sniffio-1.3.1.dist-info/LICENSE.MIT +20 -0
  26. package/lib/site-packages/sniffio-1.3.1.dist-info/METADATA +104 -0
  27. package/lib/site-packages/sniffio-1.3.1.dist-info/RECORD +14 -0
  28. package/lib/site-packages/sniffio-1.3.1.dist-info/WHEEL +5 -0
  29. package/lib/site-packages/sniffio-1.3.1.dist-info/top_level.txt +1 -0
  30. package/lib/site-packages/sortedcontainers/__init__.py +74 -0
  31. package/lib/site-packages/sortedcontainers/sorteddict.py +812 -0
  32. package/lib/site-packages/sortedcontainers/sortedlist.py +2646 -0
  33. package/lib/site-packages/sortedcontainers/sortedset.py +733 -0
  34. package/lib/site-packages/sortedcontainers-2.4.0.dist-info/INSTALLER +1 -0
  35. package/lib/site-packages/sortedcontainers-2.4.0.dist-info/LICENSE +13 -0
  36. package/lib/site-packages/sortedcontainers-2.4.0.dist-info/METADATA +264 -0
  37. package/lib/site-packages/sortedcontainers-2.4.0.dist-info/RECORD +10 -0
  38. package/lib/site-packages/sortedcontainers-2.4.0.dist-info/WHEEL +6 -0
  39. package/lib/site-packages/sortedcontainers-2.4.0.dist-info/top_level.txt +1 -0
  40. package/lib/site-packages/trio/__init__.py +133 -0
  41. package/lib/site-packages/trio/__main__.py +3 -0
  42. package/lib/site-packages/trio/_abc.py +714 -0
  43. package/lib/site-packages/trio/_channel.py +610 -0
  44. package/lib/site-packages/trio/_core/__init__.py +94 -0
  45. package/lib/site-packages/trio/_core/_asyncgens.py +243 -0
  46. package/lib/site-packages/trio/_core/_concat_tb.py +26 -0
  47. package/lib/site-packages/trio/_core/_entry_queue.py +223 -0
  48. package/lib/site-packages/trio/_core/_exceptions.py +169 -0
  49. package/lib/site-packages/trio/_core/_generated_instrumentation.py +50 -0
  50. package/lib/site-packages/trio/_core/_generated_io_epoll.py +98 -0
  51. package/lib/site-packages/trio/_core/_generated_io_kqueue.py +153 -0
  52. package/lib/site-packages/trio/_core/_generated_io_windows.py +204 -0
  53. package/lib/site-packages/trio/_core/_generated_run.py +269 -0
  54. package/lib/site-packages/trio/_core/_generated_windows_ffi.py +10 -0
  55. package/lib/site-packages/trio/_core/_instrumentation.py +117 -0
  56. package/lib/site-packages/trio/_core/_io_common.py +31 -0
  57. package/lib/site-packages/trio/_core/_io_epoll.py +385 -0
  58. package/lib/site-packages/trio/_core/_io_kqueue.py +292 -0
  59. package/lib/site-packages/trio/_core/_io_windows.py +1036 -0
  60. package/lib/site-packages/trio/_core/_ki.py +271 -0
  61. package/lib/site-packages/trio/_core/_local.py +104 -0
  62. package/lib/site-packages/trio/_core/_mock_clock.py +165 -0
  63. package/lib/site-packages/trio/_core/_parking_lot.py +317 -0
  64. package/lib/site-packages/trio/_core/_run.py +3148 -0
  65. package/lib/site-packages/trio/_core/_run_context.py +15 -0
  66. package/lib/site-packages/trio/_core/_tests/__init__.py +0 -0
  67. package/lib/site-packages/trio/_core/_tests/test_asyncgen.py +339 -0
  68. package/lib/site-packages/trio/_core/_tests/test_cancelled.py +222 -0
  69. package/lib/site-packages/trio/_core/_tests/test_exceptiongroup_gc.py +103 -0
  70. package/lib/site-packages/trio/_core/_tests/test_guest_mode.py +755 -0
  71. package/lib/site-packages/trio/_core/_tests/test_instrumentation.py +315 -0
  72. package/lib/site-packages/trio/_core/_tests/test_io.py +522 -0
  73. package/lib/site-packages/trio/_core/_tests/test_ki.py +703 -0
  74. package/lib/site-packages/trio/_core/_tests/test_local.py +118 -0
  75. package/lib/site-packages/trio/_core/_tests/test_mock_clock.py +193 -0
  76. package/lib/site-packages/trio/_core/_tests/test_parking_lot.py +389 -0
  77. package/lib/site-packages/trio/_core/_tests/test_run.py +3024 -0
  78. package/lib/site-packages/trio/_core/_tests/test_thread_cache.py +227 -0
  79. package/lib/site-packages/trio/_core/_tests/test_tutil.py +13 -0
  80. package/lib/site-packages/trio/_core/_tests/test_unbounded_queue.py +154 -0
  81. package/lib/site-packages/trio/_core/_tests/test_windows.py +305 -0
  82. package/lib/site-packages/trio/_core/_tests/tutil.py +117 -0
  83. package/lib/site-packages/trio/_core/_tests/type_tests/nursery_start.py +79 -0
  84. package/lib/site-packages/trio/_core/_tests/type_tests/run.py +51 -0
  85. package/lib/site-packages/trio/_core/_thread_cache.py +317 -0
  86. package/lib/site-packages/trio/_core/_traps.py +318 -0
  87. package/lib/site-packages/trio/_core/_unbounded_queue.py +163 -0
  88. package/lib/site-packages/trio/_core/_wakeup_socketpair.py +75 -0
  89. package/lib/site-packages/trio/_core/_windows_cffi.py +313 -0
  90. package/lib/site-packages/trio/_deprecate.py +171 -0
  91. package/lib/site-packages/trio/_dtls.py +1380 -0
  92. package/lib/site-packages/trio/_file_io.py +513 -0
  93. package/lib/site-packages/trio/_highlevel_generic.py +125 -0
  94. package/lib/site-packages/trio/_highlevel_open_tcp_listeners.py +251 -0
  95. package/lib/site-packages/trio/_highlevel_open_tcp_stream.py +397 -0
  96. package/lib/site-packages/trio/_highlevel_open_unix_stream.py +65 -0
  97. package/lib/site-packages/trio/_highlevel_serve_listeners.py +148 -0
  98. package/lib/site-packages/trio/_highlevel_socket.py +423 -0
  99. package/lib/site-packages/trio/_highlevel_ssl_helpers.py +180 -0
  100. package/lib/site-packages/trio/_path.py +289 -0
  101. package/lib/site-packages/trio/_repl.py +159 -0
  102. package/lib/site-packages/trio/_signals.py +185 -0
  103. package/lib/site-packages/trio/_socket.py +1326 -0
  104. package/lib/site-packages/trio/_ssl.py +964 -0
  105. package/lib/site-packages/trio/_subprocess.py +1178 -0
  106. package/lib/site-packages/trio/_subprocess_platform/__init__.py +123 -0
  107. package/lib/site-packages/trio/_subprocess_platform/kqueue.py +48 -0
  108. package/lib/site-packages/trio/_subprocess_platform/waitid.py +113 -0
  109. package/lib/site-packages/trio/_subprocess_platform/windows.py +11 -0
  110. package/lib/site-packages/trio/_sync.py +908 -0
  111. package/lib/site-packages/trio/_tests/__init__.py +0 -0
  112. package/lib/site-packages/trio/_tests/astrill-codesigning-cert.cer +0 -0
  113. package/lib/site-packages/trio/_tests/check_type_completeness.py +247 -0
  114. package/lib/site-packages/trio/_tests/module_with_deprecations.py +22 -0
  115. package/lib/site-packages/trio/_tests/pytest_plugin.py +54 -0
  116. package/lib/site-packages/trio/_tests/test_abc.py +72 -0
  117. package/lib/site-packages/trio/_tests/test_channel.py +750 -0
  118. package/lib/site-packages/trio/_tests/test_contextvars.py +56 -0
  119. package/lib/site-packages/trio/_tests/test_deprecate.py +277 -0
  120. package/lib/site-packages/trio/_tests/test_deprecate_strict_exception_groups_false.py +64 -0
  121. package/lib/site-packages/trio/_tests/test_dtls.py +950 -0
  122. package/lib/site-packages/trio/_tests/test_exports.py +626 -0
  123. package/lib/site-packages/trio/_tests/test_fakenet.py +317 -0
  124. package/lib/site-packages/trio/_tests/test_file_io.py +269 -0
  125. package/lib/site-packages/trio/_tests/test_highlevel_generic.py +98 -0
  126. package/lib/site-packages/trio/_tests/test_highlevel_open_tcp_listeners.py +419 -0
  127. package/lib/site-packages/trio/_tests/test_highlevel_open_tcp_stream.py +693 -0
  128. package/lib/site-packages/trio/_tests/test_highlevel_open_unix_stream.py +86 -0
  129. package/lib/site-packages/trio/_tests/test_highlevel_serve_listeners.py +186 -0
  130. package/lib/site-packages/trio/_tests/test_highlevel_socket.py +336 -0
  131. package/lib/site-packages/trio/_tests/test_highlevel_ssl_helpers.py +169 -0
  132. package/lib/site-packages/trio/_tests/test_path.py +279 -0
  133. package/lib/site-packages/trio/_tests/test_repl.py +428 -0
  134. package/lib/site-packages/trio/_tests/test_scheduler_determinism.py +47 -0
  135. package/lib/site-packages/trio/_tests/test_signals.py +186 -0
  136. package/lib/site-packages/trio/_tests/test_socket.py +1253 -0
  137. package/lib/site-packages/trio/_tests/test_ssl.py +1371 -0
  138. package/lib/site-packages/trio/_tests/test_subprocess.py +767 -0
  139. package/lib/site-packages/trio/_tests/test_sync.py +735 -0
  140. package/lib/site-packages/trio/_tests/test_testing.py +682 -0
  141. package/lib/site-packages/trio/_tests/test_testing_raisesgroup.py +1128 -0
  142. package/lib/site-packages/trio/_tests/test_threads.py +1173 -0
  143. package/lib/site-packages/trio/_tests/test_timeouts.py +281 -0
  144. package/lib/site-packages/trio/_tests/test_tracing.py +88 -0
  145. package/lib/site-packages/trio/_tests/test_trio.py +8 -0
  146. package/lib/site-packages/trio/_tests/test_unix_pipes.py +288 -0
  147. package/lib/site-packages/trio/_tests/test_util.py +349 -0
  148. package/lib/site-packages/trio/_tests/test_wait_for_object.py +225 -0
  149. package/lib/site-packages/trio/_tests/test_windows_pipes.py +112 -0
  150. package/lib/site-packages/trio/_tests/tools/__init__.py +0 -0
  151. package/lib/site-packages/trio/_tests/tools/test_gen_exports.py +179 -0
  152. package/lib/site-packages/trio/_tests/tools/test_mypy_annotate.py +140 -0
  153. package/lib/site-packages/trio/_tests/tools/test_sync_requirements.py +80 -0
  154. package/lib/site-packages/trio/_tests/type_tests/check_wraps.py +9 -0
  155. package/lib/site-packages/trio/_tests/type_tests/open_memory_channel.py +4 -0
  156. package/lib/site-packages/trio/_tests/type_tests/path.py +140 -0
  157. package/lib/site-packages/trio/_tests/type_tests/subprocesses.py +23 -0
  158. package/lib/site-packages/trio/_tests/type_tests/task_status.py +29 -0
  159. package/lib/site-packages/trio/_threads.py +610 -0
  160. package/lib/site-packages/trio/_timeouts.py +197 -0
  161. package/lib/site-packages/trio/_tools/__init__.py +0 -0
  162. package/lib/site-packages/trio/_tools/gen_exports.py +401 -0
  163. package/lib/site-packages/trio/_tools/mypy_annotate.py +126 -0
  164. package/lib/site-packages/trio/_tools/sync_requirements.py +98 -0
  165. package/lib/site-packages/trio/_tools/windows_ffi_build.py +220 -0
  166. package/lib/site-packages/trio/_unix_pipes.py +197 -0
  167. package/lib/site-packages/trio/_util.py +385 -0
  168. package/lib/site-packages/trio/_version.py +3 -0
  169. package/lib/site-packages/trio/_wait_for_object.py +67 -0
  170. package/lib/site-packages/trio/_windows_pipes.py +144 -0
  171. package/lib/site-packages/trio/abc.py +23 -0
  172. package/lib/site-packages/trio/from_thread.py +13 -0
  173. package/lib/site-packages/trio/lowlevel.py +95 -0
  174. package/lib/site-packages/trio/py.typed +0 -0
  175. package/lib/site-packages/trio/socket.py +602 -0
  176. package/lib/site-packages/trio/testing/__init__.py +58 -0
  177. package/lib/site-packages/trio/testing/_check_streams.py +570 -0
  178. package/lib/site-packages/trio/testing/_checkpoints.py +69 -0
  179. package/lib/site-packages/trio/testing/_fake_net.py +584 -0
  180. package/lib/site-packages/trio/testing/_memory_streams.py +633 -0
  181. package/lib/site-packages/trio/testing/_network.py +36 -0
  182. package/lib/site-packages/trio/testing/_raises_group.py +1015 -0
  183. package/lib/site-packages/trio/testing/_sequencer.py +87 -0
  184. package/lib/site-packages/trio/testing/_trio_test.py +50 -0
  185. package/lib/site-packages/trio/to_thread.py +4 -0
  186. package/lib/site-packages/trio-0.33.0.dist-info/INSTALLER +1 -0
  187. package/lib/site-packages/trio-0.33.0.dist-info/METADATA +186 -0
  188. package/lib/site-packages/trio-0.33.0.dist-info/RECORD +156 -0
  189. package/lib/site-packages/trio-0.33.0.dist-info/REQUESTED +0 -0
  190. package/lib/site-packages/trio-0.33.0.dist-info/WHEEL +5 -0
  191. package/lib/site-packages/trio-0.33.0.dist-info/entry_points.txt +2 -0
  192. package/lib/site-packages/trio-0.33.0.dist-info/licenses/LICENSE +3 -0
  193. package/lib/site-packages/trio-0.33.0.dist-info/licenses/LICENSE.APACHE2 +202 -0
  194. package/lib/site-packages/trio-0.33.0.dist-info/licenses/LICENSE.MIT +22 -0
  195. package/lib/site-packages/trio-0.33.0.dist-info/top_level.txt +1 -0
  196. package/package.json +1 -1
@@ -0,0 +1,419 @@
1
+ from __future__ import annotations
2
+
3
+ import errno
4
+ import socket as stdlib_socket
5
+ import sys
6
+ from socket import AddressFamily, SocketKind
7
+ from typing import TYPE_CHECKING, cast, overload
8
+
9
+ import attrs
10
+ import pytest
11
+
12
+ import trio
13
+ from trio import (
14
+ SocketListener,
15
+ open_tcp_listeners,
16
+ open_tcp_stream,
17
+ serve_tcp,
18
+ )
19
+ from trio.abc import HostnameResolver, SendStream, SocketFactory
20
+ from trio.testing import open_stream_to_socket_listener
21
+
22
+ from .. import socket as tsocket
23
+ from .._core._tests.tutil import binds_ipv6, slow
24
+
25
+ if sys.version_info < (3, 11):
26
+ from exceptiongroup import BaseExceptionGroup
27
+
28
+ if TYPE_CHECKING:
29
+ from collections.abc import Sequence
30
+
31
+ from typing_extensions import Buffer
32
+
33
+ from trio._socket import AddressFormat
34
+
35
+
36
+ async def test_open_tcp_listeners_basic() -> None:
37
+ listeners = await open_tcp_listeners(0)
38
+ assert isinstance(listeners, list)
39
+ for obj in listeners:
40
+ assert isinstance(obj, SocketListener)
41
+ # Binds to wildcard address by default
42
+ assert obj.socket.family in [tsocket.AF_INET, tsocket.AF_INET6]
43
+ assert obj.socket.getsockname()[0] in ["0.0.0.0", "::"]
44
+
45
+ listener = listeners[0]
46
+ # Make sure the backlog is at least 2
47
+ c1 = await open_stream_to_socket_listener(listener)
48
+ c2 = await open_stream_to_socket_listener(listener)
49
+
50
+ s1 = await listener.accept()
51
+ s2 = await listener.accept()
52
+
53
+ # Note that we don't know which client stream is connected to which server
54
+ # stream
55
+ await s1.send_all(b"x")
56
+ await s2.send_all(b"x")
57
+ assert await c1.receive_some(1) == b"x"
58
+ assert await c2.receive_some(1) == b"x"
59
+
60
+ for resource in [c1, c2, s1, s2, *listeners]:
61
+ await resource.aclose()
62
+
63
+
64
+ async def test_open_tcp_listeners_specific_port_specific_host() -> None:
65
+ # Pick a port
66
+ sock = tsocket.socket()
67
+ await sock.bind(("127.0.0.1", 0))
68
+ host, port = sock.getsockname()
69
+ sock.close()
70
+
71
+ (listener,) = await open_tcp_listeners(port, host=host)
72
+ async with listener:
73
+ assert listener.socket.getsockname() == (host, port)
74
+
75
+
76
+ @binds_ipv6
77
+ @slow
78
+ async def test_open_tcp_listeners_ipv6_v6only() -> None:
79
+ # Check IPV6_V6ONLY is working properly
80
+ (ipv6_listener,) = await open_tcp_listeners(0, host="::1")
81
+ async with ipv6_listener:
82
+ _, port, *_ = ipv6_listener.socket.getsockname()
83
+
84
+ with pytest.raises(
85
+ OSError,
86
+ match=r"(Error|all attempts to) connect(ing)* to (\(')*127\.0\.0\.1(', |:)\d+(\): Connection refused| failed)$",
87
+ ):
88
+ # Windows retries failed connections so this takes seconds
89
+ # (and that's why this is marked @slow)
90
+ await open_tcp_stream("127.0.0.1", port)
91
+
92
+
93
+ async def test_open_tcp_listeners_rebind() -> None:
94
+ (l1,) = await open_tcp_listeners(0, host="127.0.0.1")
95
+ sockaddr1 = l1.socket.getsockname()
96
+
97
+ # Plain old rebinding while it's still there should fail, even if we have
98
+ # SO_REUSEADDR set
99
+ with stdlib_socket.socket() as probe:
100
+ probe.setsockopt(stdlib_socket.SOL_SOCKET, stdlib_socket.SO_REUSEADDR, 1)
101
+ with pytest.raises(
102
+ OSError,
103
+ match=r"(Address (already )?in use|An attempt was made to access a socket in a way forbidden by its access permissions)$",
104
+ ):
105
+ probe.bind(sockaddr1)
106
+
107
+ # Now use the first listener to set up some connections in various states,
108
+ # and make sure that they don't create any obstacle to rebinding a second
109
+ # listener after the first one is closed.
110
+ c_established = await open_stream_to_socket_listener(l1)
111
+ s_established = await l1.accept()
112
+
113
+ c_time_wait = await open_stream_to_socket_listener(l1)
114
+ s_time_wait = await l1.accept()
115
+ # Server-initiated close leaves socket in TIME_WAIT
116
+ await s_time_wait.aclose()
117
+
118
+ await l1.aclose()
119
+ (l2,) = await open_tcp_listeners(sockaddr1[1], host="127.0.0.1")
120
+ sockaddr2 = l2.socket.getsockname()
121
+
122
+ assert sockaddr1 == sockaddr2
123
+ assert s_established.socket.getsockname() == sockaddr2
124
+ assert c_time_wait.socket.getpeername() == sockaddr2
125
+
126
+ for resource in [
127
+ l1,
128
+ l2,
129
+ c_established,
130
+ s_established,
131
+ c_time_wait,
132
+ s_time_wait,
133
+ ]:
134
+ await resource.aclose()
135
+
136
+
137
+ class FakeOSError(OSError):
138
+ pass
139
+
140
+
141
+ @attrs.define(slots=False)
142
+ class FakeSocket(tsocket.SocketType):
143
+ _family: AddressFamily = attrs.field(converter=AddressFamily)
144
+ _type: SocketKind = attrs.field(converter=SocketKind)
145
+ _proto: int
146
+
147
+ closed: bool = False
148
+ poison_listen: bool = False
149
+ backlog: int | None = None
150
+
151
+ @property
152
+ def type(self) -> SocketKind:
153
+ return self._type
154
+
155
+ @property
156
+ def family(self) -> AddressFamily:
157
+ return self._family
158
+
159
+ @property
160
+ def proto(self) -> int: # pragma: no cover
161
+ return self._proto
162
+
163
+ @overload
164
+ def getsockopt(self, /, level: int, optname: int) -> int: ...
165
+
166
+ @overload
167
+ def getsockopt(self, /, level: int, optname: int, buflen: int) -> bytes: ...
168
+
169
+ def getsockopt(
170
+ self,
171
+ /,
172
+ level: int,
173
+ optname: int,
174
+ buflen: int | None = None,
175
+ ) -> int | bytes:
176
+ if (level, optname) == (tsocket.SOL_SOCKET, tsocket.SO_ACCEPTCONN):
177
+ return True
178
+ raise AssertionError() # pragma: no cover
179
+
180
+ @overload
181
+ def setsockopt(self, /, level: int, optname: int, value: int | Buffer) -> None: ...
182
+
183
+ @overload
184
+ def setsockopt(
185
+ self,
186
+ /,
187
+ level: int,
188
+ optname: int,
189
+ value: None,
190
+ optlen: int,
191
+ ) -> None: ...
192
+
193
+ def setsockopt(
194
+ self,
195
+ /,
196
+ level: int,
197
+ optname: int,
198
+ value: int | Buffer | None,
199
+ optlen: int | None = None,
200
+ ) -> None:
201
+ pass
202
+
203
+ async def bind(self, address: AddressFormat) -> None:
204
+ pass
205
+
206
+ def listen(self, /, backlog: int = min(stdlib_socket.SOMAXCONN, 128)) -> None:
207
+ assert self.backlog is None
208
+ assert backlog is not None
209
+ self.backlog = backlog
210
+ if self.poison_listen:
211
+ raise FakeOSError("whoops")
212
+
213
+ def close(self) -> None:
214
+ self.closed = True
215
+
216
+
217
+ @attrs.define(slots=False)
218
+ class FakeSocketFactory(SocketFactory):
219
+ poison_after: int
220
+ sockets: list[tsocket.SocketType] = attrs.Factory(list)
221
+ raise_on_family: dict[AddressFamily, int] = attrs.Factory(dict) # family => errno
222
+
223
+ def socket(
224
+ self,
225
+ family: AddressFamily | int | None = None,
226
+ type_: SocketKind | int | None = None,
227
+ proto: int = 0,
228
+ ) -> tsocket.SocketType:
229
+ assert family is not None
230
+ assert type_ is not None
231
+ if isinstance(family, int) and not isinstance(family, AddressFamily):
232
+ family = AddressFamily(family) # pragma: no cover
233
+ if family in self.raise_on_family:
234
+ raise OSError(self.raise_on_family[family], "nope")
235
+ sock = FakeSocket(family, type_, proto)
236
+ self.poison_after -= 1
237
+ if self.poison_after == 0:
238
+ sock.poison_listen = True
239
+ self.sockets.append(sock)
240
+ return sock
241
+
242
+
243
+ @attrs.define(slots=False)
244
+ class FakeHostnameResolver(HostnameResolver):
245
+ family_addr_pairs: Sequence[tuple[AddressFamily, str]]
246
+
247
+ async def getaddrinfo(
248
+ self,
249
+ host: bytes | None,
250
+ port: bytes | str | int | None,
251
+ family: int = 0,
252
+ type: int = 0,
253
+ proto: int = 0,
254
+ flags: int = 0,
255
+ ) -> list[
256
+ tuple[
257
+ AddressFamily,
258
+ SocketKind,
259
+ int,
260
+ str,
261
+ tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes],
262
+ ]
263
+ ]:
264
+ assert isinstance(port, int)
265
+ return [
266
+ (family, tsocket.SOCK_STREAM, 0, "", (addr, port))
267
+ for family, addr in self.family_addr_pairs
268
+ ]
269
+
270
+ async def getnameinfo(
271
+ self,
272
+ sockaddr: tuple[str, int] | tuple[str, int, int, int],
273
+ flags: int,
274
+ ) -> tuple[str, str]:
275
+ raise NotImplementedError()
276
+
277
+
278
+ async def test_open_tcp_listeners_multiple_host_cleanup_on_error() -> None:
279
+ # If we were trying to bind to multiple hosts and one of them failed, they
280
+ # call get cleaned up before returning
281
+ fsf = FakeSocketFactory(3)
282
+ tsocket.set_custom_socket_factory(fsf)
283
+ tsocket.set_custom_hostname_resolver(
284
+ FakeHostnameResolver(
285
+ [
286
+ (tsocket.AF_INET, "1.1.1.1"),
287
+ (tsocket.AF_INET, "2.2.2.2"),
288
+ (tsocket.AF_INET, "3.3.3.3"),
289
+ ],
290
+ ),
291
+ )
292
+
293
+ with pytest.raises(FakeOSError):
294
+ await open_tcp_listeners(80, host="example.org")
295
+
296
+ assert len(fsf.sockets) == 3
297
+ for sock in fsf.sockets:
298
+ # property only exists on FakeSocket
299
+ assert sock.closed # type: ignore[attr-defined]
300
+
301
+
302
+ async def test_open_tcp_listeners_port_checking() -> None:
303
+ for host in ["127.0.0.1", None]:
304
+ with pytest.raises(TypeError):
305
+ await open_tcp_listeners(None, host=host) # type: ignore[arg-type]
306
+ with pytest.raises(TypeError):
307
+ await open_tcp_listeners(b"80", host=host) # type: ignore[arg-type]
308
+ with pytest.raises(TypeError):
309
+ await open_tcp_listeners("http", host=host) # type: ignore[arg-type]
310
+
311
+
312
+ async def test_serve_tcp() -> None:
313
+ async def handler(stream: SendStream) -> None:
314
+ await stream.send_all(b"x")
315
+
316
+ async with trio.open_nursery() as nursery:
317
+ # nursery.start is incorrectly typed, awaiting #2773
318
+ value = await nursery.start(serve_tcp, handler, 0)
319
+ assert isinstance(value, list)
320
+ listeners = cast("list[SocketListener]", value)
321
+ stream = await open_stream_to_socket_listener(listeners[0])
322
+ async with stream:
323
+ assert await stream.receive_some(1) == b"x"
324
+ nursery.cancel_scope.cancel()
325
+
326
+
327
+ @pytest.mark.parametrize(
328
+ "try_families",
329
+ [{tsocket.AF_INET}, {tsocket.AF_INET6}, {tsocket.AF_INET, tsocket.AF_INET6}],
330
+ )
331
+ @pytest.mark.parametrize(
332
+ "fail_families",
333
+ [{tsocket.AF_INET}, {tsocket.AF_INET6}, {tsocket.AF_INET, tsocket.AF_INET6}],
334
+ )
335
+ async def test_open_tcp_listeners_some_address_families_unavailable(
336
+ try_families: set[AddressFamily],
337
+ fail_families: set[AddressFamily],
338
+ ) -> None:
339
+ fsf = FakeSocketFactory(
340
+ 10,
341
+ raise_on_family=dict.fromkeys(fail_families, errno.EAFNOSUPPORT),
342
+ )
343
+ tsocket.set_custom_socket_factory(fsf)
344
+ tsocket.set_custom_hostname_resolver(
345
+ FakeHostnameResolver([(family, "foo") for family in try_families]),
346
+ )
347
+
348
+ should_succeed = try_families - fail_families
349
+
350
+ if not should_succeed:
351
+ with pytest.raises(OSError, match="This system doesn't support") as exc_info:
352
+ await open_tcp_listeners(80, host="example.org")
353
+
354
+ # open_listeners always creates an exceptiongroup with the
355
+ # unsupported address families, regardless of the value of
356
+ # strict_exception_groups or number of unsupported families.
357
+ assert isinstance(exc_info.value.__cause__, BaseExceptionGroup)
358
+ for subexc in exc_info.value.__cause__.exceptions:
359
+ assert "nope" in str(subexc)
360
+ else:
361
+ listeners = await open_tcp_listeners(80)
362
+ for listener in listeners:
363
+ should_succeed.remove(listener.socket.family)
364
+ assert not should_succeed
365
+
366
+
367
+ async def test_open_tcp_listeners_socket_fails_not_afnosupport() -> None:
368
+ fsf = FakeSocketFactory(
369
+ 10,
370
+ raise_on_family={
371
+ tsocket.AF_INET: errno.EAFNOSUPPORT,
372
+ tsocket.AF_INET6: errno.EINVAL,
373
+ },
374
+ )
375
+ tsocket.set_custom_socket_factory(fsf)
376
+ tsocket.set_custom_hostname_resolver(
377
+ FakeHostnameResolver([(tsocket.AF_INET, "foo"), (tsocket.AF_INET6, "bar")]),
378
+ )
379
+
380
+ with pytest.raises(OSError, match="nope") as exc_info:
381
+ await open_tcp_listeners(80, host="example.org")
382
+ assert exc_info.value.errno == errno.EINVAL
383
+ assert exc_info.value.__cause__ is None
384
+ assert "nope" in str(exc_info.value)
385
+
386
+
387
+ # We used to have an elaborate test that opened a real TCP listening socket
388
+ # and then tried to measure its backlog by making connections to it. And most
389
+ # of the time, it worked. But no matter what we tried, it was always fragile,
390
+ # because it had to do things like use timeouts to guess when the listening
391
+ # queue was full, sometimes the CI hosts go into SYN-cookie mode (where there
392
+ # effectively is no backlog), sometimes the host might not be enough resources
393
+ # to give us the full requested backlog... it was a mess. So now we just check
394
+ # that the backlog argument is passed through correctly.
395
+ async def test_open_tcp_listeners_backlog() -> None:
396
+ fsf = FakeSocketFactory(99)
397
+ tsocket.set_custom_socket_factory(fsf)
398
+ for given, expected in [
399
+ (None, 0xFFFF),
400
+ (99999999, 0xFFFF),
401
+ (10, 10),
402
+ (1, 1),
403
+ ]:
404
+ listeners = await open_tcp_listeners(0, backlog=given)
405
+ assert listeners
406
+ for listener in listeners:
407
+ # `backlog` only exists on FakeSocket
408
+ assert listener.socket.backlog == expected # type: ignore[attr-defined]
409
+
410
+
411
+ async def test_open_tcp_listeners_backlog_float_error() -> None:
412
+ fsf = FakeSocketFactory(99)
413
+ tsocket.set_custom_socket_factory(fsf)
414
+ for should_fail in (0.0, 2.18, 3.15, 9.75):
415
+ with pytest.raises(
416
+ TypeError,
417
+ match=f"backlog must be an int or None, not {should_fail!r}",
418
+ ):
419
+ await open_tcp_listeners(0, backlog=should_fail) # type: ignore[arg-type]