@@ -224,9 +224,9 @@ async def test_from_stdlib_socket():
224
224
class MySocket (stdlib_socket .socket ):
225
225
pass
226
226
227
- mysock = MySocket ()
228
- with pytest .raises (TypeError ):
229
- tsocket .from_stdlib_socket (mysock )
227
+ with MySocket () as mysock :
228
+ with pytest .raises (TypeError ):
229
+ tsocket .from_stdlib_socket (mysock )
230
230
231
231
232
232
async def test_from_fd ():
@@ -292,12 +292,15 @@ async def test_sniff_sockopts():
292
292
# check family / type for correctness:
293
293
assert tsocket_socket .family == socket .family
294
294
assert tsocket_socket .type == socket .type
295
+ tsocket_socket .detach ()
295
296
296
297
# fromfd constructor
297
298
tsocket_from_fd = tsocket .fromfd (socket .fileno (), AF_INET , SOCK_STREAM )
298
299
# check family / type for correctness:
299
300
assert tsocket_from_fd .family == socket .family
300
301
assert tsocket_from_fd .type == socket .type
302
+ tsocket_from_fd .close ()
303
+
301
304
socket .close ()
302
305
303
306
@@ -482,73 +485,78 @@ class Addresses:
482
485
async def test_SocketType_resolve (socket_type , addrs ):
483
486
v6 = socket_type == tsocket .AF_INET6
484
487
485
- # For some reason the stdlib special-cases "" to pass NULL to getaddrinfo
486
- # They also error out on None, but whatever, None is much more consistent,
487
- # so we accept it too.
488
- for null in [None , "" ]:
489
- sock = tsocket .socket (family = socket_type )
490
- got = await sock ._resolve_local_address ((null , 80 ))
491
- assert got == (addrs .bind_all , 80 , * addrs .extra )
492
- got = await sock ._resolve_remote_address ((null , 80 ))
493
- assert got == (addrs .localhost , 80 , * addrs .extra )
494
-
495
- # AI_PASSIVE only affects the wildcard address, so for everything else
496
- # _resolve_local_address and _resolve_remote_address should work the same:
497
- for resolver in ["_resolve_local_address" , "_resolve_remote_address" ]:
498
-
499
- async def res (* args ):
500
- return await getattr (sock , resolver )(* args )
501
-
502
- assert await res ((addrs .arbitrary , "http" )) == (
503
- addrs .arbitrary ,
504
- 80 ,
505
- * addrs .extra ,
506
- )
507
- if v6 :
508
- assert await res (("1::2" , 80 , 1 )) == ("1::2" , 80 , 1 , 0 )
509
- assert await res (("1::2" , 80 , 1 , 2 )) == ("1::2" , 80 , 1 , 2 )
510
-
511
- # V4 mapped addresses resolved if V6ONLY is False
512
- sock .setsockopt (tsocket .IPPROTO_IPV6 , tsocket .IPV6_V6ONLY , False )
513
- assert await res (("1.2.3.4" , "http" )) == ("::ffff:1.2.3.4" , 80 , 0 , 0 ,)
514
-
515
- # Check the <broadcast> special case, because why not
516
- assert await res (("<broadcast>" , 123 )) == (addrs .broadcast , 123 , * addrs .extra ,)
517
-
518
- # But not if it's true (at least on systems where getaddrinfo works
519
- # correctly)
520
- if v6 and not gai_without_v4mapped_is_buggy ():
521
- sock .setsockopt (tsocket .IPPROTO_IPV6 , tsocket .IPV6_V6ONLY , True )
522
- with pytest .raises (tsocket .gaierror ) as excinfo :
523
- await res (("1.2.3.4" , 80 ))
524
- # Windows, macOS
525
- expected_errnos = {tsocket .EAI_NONAME }
526
- # Linux
527
- if hasattr (tsocket , "EAI_ADDRFAMILY" ):
528
- expected_errnos .add (tsocket .EAI_ADDRFAMILY )
529
- assert excinfo .value .errno in expected_errnos
530
-
531
- # A family where we know nothing about the addresses, so should just
532
- # pass them through. This should work on Linux, which is enough to
533
- # smoke test the basic functionality...
534
- try :
535
- netlink_sock = tsocket .socket (
536
- family = tsocket .AF_NETLINK , type = tsocket .SOCK_DGRAM
488
+ with tsocket .socket (family = socket_type ) as sock :
489
+ # For some reason the stdlib special-cases "" to pass NULL to
490
+ # getaddrinfo They also error out on None, but whatever, None is much
491
+ # more consistent, so we accept it too.
492
+ for null in [None , "" ]:
493
+ got = await sock ._resolve_local_address ((null , 80 ))
494
+ assert got == (addrs .bind_all , 80 , * addrs .extra )
495
+ got = await sock ._resolve_remote_address ((null , 80 ))
496
+ assert got == (addrs .localhost , 80 , * addrs .extra )
497
+
498
+ # AI_PASSIVE only affects the wildcard address, so for everything else
499
+ # _resolve_local_address and _resolve_remote_address should work the same:
500
+ for resolver in ["_resolve_local_address" , "_resolve_remote_address" ]:
501
+
502
+ async def res (* args ):
503
+ return await getattr (sock , resolver )(* args )
504
+
505
+ assert await res ((addrs .arbitrary , "http" )) == (
506
+ addrs .arbitrary ,
507
+ 80 ,
508
+ * addrs .extra ,
537
509
)
538
- except (AttributeError , OSError ):
539
- pass
540
- else :
541
- assert await getattr (netlink_sock , resolver )("asdf" ) == "asdf"
542
-
543
- with pytest .raises (ValueError ):
544
- await res ("1.2.3.4" )
545
- with pytest .raises (ValueError ):
546
- await res (("1.2.3.4" ,))
547
- with pytest .raises (ValueError ):
548
510
if v6 :
549
- await res (("1.2.3.4" , 80 , 0 , 0 , 0 ))
511
+ assert await res (("1::2" , 80 , 1 )) == ("1::2" , 80 , 1 , 0 )
512
+ assert await res (("1::2" , 80 , 1 , 2 )) == ("1::2" , 80 , 1 , 2 )
513
+
514
+ # V4 mapped addresses resolved if V6ONLY is False
515
+ sock .setsockopt (tsocket .IPPROTO_IPV6 , tsocket .IPV6_V6ONLY , False )
516
+ assert await res (("1.2.3.4" , "http" )) == ("::ffff:1.2.3.4" , 80 , 0 , 0 ,)
517
+
518
+ # Check the <broadcast> special case, because why not
519
+ assert await res (("<broadcast>" , 123 )) == (
520
+ addrs .broadcast ,
521
+ 123 ,
522
+ * addrs .extra ,
523
+ )
524
+
525
+ # But not if it's true (at least on systems where getaddrinfo works
526
+ # correctly)
527
+ if v6 and not gai_without_v4mapped_is_buggy ():
528
+ sock .setsockopt (tsocket .IPPROTO_IPV6 , tsocket .IPV6_V6ONLY , True )
529
+ with pytest .raises (tsocket .gaierror ) as excinfo :
530
+ await res (("1.2.3.4" , 80 ))
531
+ # Windows, macOS
532
+ expected_errnos = {tsocket .EAI_NONAME }
533
+ # Linux
534
+ if hasattr (tsocket , "EAI_ADDRFAMILY" ):
535
+ expected_errnos .add (tsocket .EAI_ADDRFAMILY )
536
+ assert excinfo .value .errno in expected_errnos
537
+
538
+ # A family where we know nothing about the addresses, so should just
539
+ # pass them through. This should work on Linux, which is enough to
540
+ # smoke test the basic functionality...
541
+ try :
542
+ netlink_sock = tsocket .socket (
543
+ family = tsocket .AF_NETLINK , type = tsocket .SOCK_DGRAM
544
+ )
545
+ except (AttributeError , OSError ):
546
+ pass
550
547
else :
551
- await res (("1.2.3.4" , 80 , 0 , 0 ))
548
+ assert await getattr (netlink_sock , resolver )("asdf" ) == "asdf"
549
+ netlink_sock .close ()
550
+
551
+ with pytest .raises (ValueError ):
552
+ await res ("1.2.3.4" )
553
+ with pytest .raises (ValueError ):
554
+ await res (("1.2.3.4" ,))
555
+ with pytest .raises (ValueError ):
556
+ if v6 :
557
+ await res (("1.2.3.4" , 80 , 0 , 0 , 0 ))
558
+ else :
559
+ await res (("1.2.3.4" , 80 , 0 , 0 ))
552
560
553
561
554
562
async def test_SocketType_unresolved_names ():
@@ -923,8 +931,9 @@ async def check_AF_UNIX(path):
923
931
with tsocket .socket (family = tsocket .AF_UNIX ) as csock :
924
932
await csock .connect (path )
925
933
ssock , _ = await lsock .accept ()
926
- await csock .send (b"x" )
927
- assert await ssock .recv (1 ) == b"x"
934
+ with ssock :
935
+ await csock .send (b"x" )
936
+ assert await ssock .recv (1 ) == b"x"
928
937
929
938
# Can't use tmpdir fixture, because we can exceed the maximum AF_UNIX path
930
939
# length on macOS.
0 commit comments