1
1
import os as _os
2
- import socket as _stdlib_socket
3
2
import sys as _sys
3
+ import socket as _stdlib_socket
4
4
from functools import wraps as _wraps
5
5
6
6
import idna as _idna
9
9
from ._threads import run_sync_in_worker_thread
10
10
from ._util import fspath
11
11
12
- __all__ = []
13
-
14
- ################################################################
15
- # misc utilities
16
- ################################################################
17
-
18
-
19
- def _reexport (name ):
20
- globals ()[name ] = getattr (_stdlib_socket , name )
21
- __all__ .append (name )
22
-
23
-
24
- def _add_to_all (obj ):
25
- __all__ .append (obj .__name__ )
26
- return obj
27
-
28
12
29
13
# Usage:
30
14
#
@@ -62,54 +46,13 @@ async def __aexit__(self, etype, value, tb):
62
46
# CONSTANTS
63
47
################################################################
64
48
65
- # Hopefully will show up in 3.7:
66
- # https://github.com/python/cpython/pull/477
67
- if not hasattr (_stdlib_socket , "TCP_NOTSENT_LOWAT" ): # pragma: no branch
68
- if _sys .platform == "darwin" :
69
- TCP_NOTSENT_LOWAT = 0x201
70
- __all__ .append ("TCP_NOTSENT_LOWAT" )
71
- elif _sys .platform == "linux" :
72
- TCP_NOTSENT_LOWAT = 25
73
- __all__ .append ("TCP_NOTSENT_LOWAT" )
74
-
75
- for _name in _stdlib_socket .__dict__ .keys ():
76
- if _name == _name .upper ():
77
- _reexport (_name )
78
-
79
- if _sys .platform == "win32" :
80
- # See https://github.com/python-trio/trio/issues/39
81
- # (you can still get it from stdlib socket, of course, if you want it)
82
- globals ().pop ("SO_REUSEADDR" , None )
83
- __all__ .remove ("SO_REUSEADDR" )
84
-
49
+ try :
50
+ from socket import IPPROTO_IPV6
51
+ except ImportError :
85
52
# As of at least 3.6, python on Windows is missing IPPROTO_IPV6
86
53
# https://bugs.python.org/issue29515
87
- if not hasattr ( _stdlib_socket , "IPPROTO_IPV6" ): # pragma: no branch
54
+ if _sys . platform == "win32" :
88
55
IPPROTO_IPV6 = 41
89
- __all__ .append ("IPPROTO_IPV6" )
90
-
91
- ################################################################
92
- # Simple re-exports
93
- ################################################################
94
-
95
- for _name in [
96
- "gaierror" ,
97
- "herror" ,
98
- "gethostname" ,
99
- "ntohs" ,
100
- "htonl" ,
101
- "htons" ,
102
- "inet_aton" ,
103
- "inet_ntoa" ,
104
- "inet_pton" ,
105
- "inet_ntop" ,
106
- "sethostname" ,
107
- "if_nameindex" ,
108
- "if_nametoindex" ,
109
- "if_indextoname" ,
110
- ]:
111
- if hasattr (_stdlib_socket , _name ):
112
- _reexport (_name )
113
56
114
57
################################################################
115
58
# Overrides
@@ -119,7 +62,6 @@ async def __aexit__(self, etype, value, tb):
119
62
_socket_factory = _core .RunVar ("socket_factory" )
120
63
121
64
122
- @_add_to_all
123
65
def set_custom_hostname_resolver (hostname_resolver ):
124
66
"""Set a custom hostname resolver.
125
67
@@ -152,7 +94,6 @@ def set_custom_hostname_resolver(hostname_resolver):
152
94
return old
153
95
154
96
155
- @_add_to_all
156
97
def set_custom_socket_factory (socket_factory ):
157
98
"""Set a custom socket object factory.
158
99
@@ -187,7 +128,6 @@ def set_custom_socket_factory(socket_factory):
187
128
_NUMERIC_ONLY = _stdlib_socket .AI_NUMERICHOST | _stdlib_socket .AI_NUMERICSERV
188
129
189
130
190
- @_add_to_all
191
131
async def getaddrinfo (host , port , family = 0 , type = 0 , proto = 0 , flags = 0 ):
192
132
"""Look up a numeric address given a name.
193
133
@@ -249,7 +189,6 @@ def numeric_only_failure(exc):
249
189
)
250
190
251
191
252
- @_add_to_all
253
192
async def getnameinfo (sockaddr , flags ):
254
193
"""Look up a name given a numeric address.
255
194
@@ -269,7 +208,6 @@ async def getnameinfo(sockaddr, flags):
269
208
)
270
209
271
210
272
- @_add_to_all
273
211
async def getprotobyname (name ):
274
212
"""Look up a protocol number by name. (Rarely used.)
275
213
@@ -289,7 +227,6 @@ async def getprotobyname(name):
289
227
################################################################
290
228
291
229
292
- @_add_to_all
293
230
def from_stdlib_socket (sock ):
294
231
"""Convert a standard library :func:`socket.socket` object into a trio
295
232
socket object.
@@ -299,7 +236,6 @@ def from_stdlib_socket(sock):
299
236
300
237
301
238
@_wraps (_stdlib_socket .fromfd , assigned = (), updated = ())
302
- @_add_to_all
303
239
def fromfd (* args , ** kwargs ):
304
240
"""Like :func:`socket.fromfd`, but returns a trio socket object.
305
241
@@ -310,13 +246,11 @@ def fromfd(*args, **kwargs):
310
246
if hasattr (_stdlib_socket , "fromshare" ):
311
247
312
248
@_wraps (_stdlib_socket .fromshare , assigned = (), updated = ())
313
- @_add_to_all
314
249
def fromshare (* args , ** kwargs ):
315
250
return from_stdlib_socket (_stdlib_socket .fromshare (* args , ** kwargs ))
316
251
317
252
318
253
@_wraps (_stdlib_socket .socketpair , assigned = (), updated = ())
319
- @_add_to_all
320
254
def socketpair (* args , ** kwargs ):
321
255
"""Like :func:`socket.socketpair`, but returns a pair of trio socket
322
256
objects.
@@ -327,7 +261,6 @@ def socketpair(*args, **kwargs):
327
261
328
262
329
263
@_wraps (_stdlib_socket .socket , assigned = (), updated = ())
330
- @_add_to_all
331
264
def socket (
332
265
family = _stdlib_socket .AF_INET ,
333
266
type = _stdlib_socket .SOCK_STREAM ,
@@ -374,7 +307,26 @@ def real_socket_type(type_num):
374
307
return type_num & _SOCK_TYPE_MASK
375
308
376
309
377
- @_add_to_all
310
+ def _make_simple_sock_method_wrapper (methname , wait_fn , maybe_avail = False ):
311
+ fn = getattr (_stdlib_socket .socket , methname )
312
+
313
+ @_wraps (fn , assigned = ("__name__" ,), updated = ())
314
+ async def wrapper (self , * args , ** kwargs ):
315
+ return await self ._nonblocking_helper (fn , args , kwargs , wait_fn )
316
+
317
+ wrapper .__doc__ = (
318
+ """Like :meth:`socket.socket.{}`, but async.
319
+
320
+ """ .format (methname )
321
+ )
322
+ if maybe_avail :
323
+ wrapper .__doc__ += (
324
+ "Only available on platforms where :meth:`socket.socket.{}` "
325
+ "is available." .format (methname )
326
+ )
327
+ return wrapper
328
+
329
+
378
330
class SocketType :
379
331
def __init__ (self ):
380
332
raise TypeError (
@@ -603,25 +555,6 @@ async def _nonblocking_helper(self, fn, args, kwargs, wait_fn):
603
555
except BlockingIOError :
604
556
pass
605
557
606
- def _make_simple_sock_method_wrapper (methname , wait_fn , maybe_avail = False ):
607
- fn = getattr (_stdlib_socket .socket , methname )
608
-
609
- @_wraps (fn , assigned = ("__name__" ,), updated = ())
610
- async def wrapper (self , * args , ** kwargs ):
611
- return await self ._nonblocking_helper (fn , args , kwargs , wait_fn )
612
-
613
- wrapper .__doc__ = (
614
- """Like :meth:`socket.socket.{}`, but async.
615
-
616
- """ .format (methname )
617
- )
618
- if maybe_avail :
619
- wrapper .__doc__ += (
620
- "Only available on platforms where :meth:`socket.socket.{}` "
621
- "is available." .format (methname )
622
- )
623
- return wrapper
624
-
625
558
################################################################
626
559
# accept
627
560
################################################################
0 commit comments