annotate Lib/socket.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents c5751f01b09b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 # Wrapper module for _socket, providing some additional facilities
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 # implemented in Python.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 """\
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 This module provides socket operations and some related functions.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14935
diff changeset
7 On other systems, it only supports IP. Functions specific for a
14935
13488539f02b Mention in the module's doc string that other functions of the socket API
Martin v. Löwis <martin@v.loewis.de>
parents: 14682
diff changeset
8 socket are available as methods of the socket object.
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 Functions:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 socket() -- create a new socket object
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32160
diff changeset
13 socketpair() -- create a pair of new socket objects [*]
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 fromfd() -- create a socket object from an open file descriptor [*]
76150
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
15 fromshare() -- create a socket object from data received from socket.share() [*]
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 gethostname() -- return the current hostname
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 gethostbyname() -- map a hostname to its IP number
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 gethostbyaddr() -- map an IP number or hostname to DNS info
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 getservbyname() -- map a service name and a protocol name to a port number
54514
7234a83a8b19 Merged revisions 73138 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53072
diff changeset
20 getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 htons(), htonl() -- convert 16, 32 bit int from host to network byte order
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23844
diff changeset
25 socket.getdefaulttimeout() -- get the default timeout value
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23844
diff changeset
26 socket.setdefaulttimeout() -- set the default timeout value
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
27 create_connection() -- connects to an address, with an optional timeout and
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
28 optional source address.
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30 [*] not available on all platforms!
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 Special objects:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 SocketType -- type object for socket objects
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 error -- exception raised for I/O errors
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 28443
diff changeset
36 has_ipv6 -- boolean value indicating if IPv6 is supported
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 Integer constants:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 Many other constants may be defined; these may be used in calls to
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 the setsockopt() and getsockopt() methods.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 """
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46
21971
fce7278d46b0 Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents: 21962
diff changeset
47 import _socket
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 from _socket import *
21971
fce7278d46b0 Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents: 21962
diff changeset
49
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
50 import os, sys, io
85481
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
51 from enum import IntEnum
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
28890
39f4b97ce828 Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents: 28888
diff changeset
53 try:
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
54 import errno
84442
7769c4d72806 Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents: 84114
diff changeset
55 except ImportError:
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
56 errno = None
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
57 EBADF = getattr(errno, 'EBADF', 9)
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
58 EAGAIN = getattr(errno, 'EAGAIN', 11)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
59 EWOULDBLOCK = getattr(errno, 'EWOULDBLOCK', 11)
28890
39f4b97ce828 Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents: 28888
diff changeset
60
53072
7e978ca42930 Merged revisions 70980,71059,71225,71234,71241,71243,71249,71251,71255,71266,71299,71329,71397-71398,71486 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50927
diff changeset
61 __all__ = ["getfqdn", "create_connection"]
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15872
diff changeset
62 __all__.extend(os._get_exports_list(_socket))
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42974
diff changeset
63
85481
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
64 # Set up the socket.AF_* socket.SOCK_* constants as members of IntEnums for
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
65 # nicer string representations.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
66 # Note that _socket only knows about the integer values. The public interface
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
67 # in this module understands the enums and translates them back from integers
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
68 # where needed (e.g. .family property of a socket object).
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
69 AddressFamily = IntEnum('AddressFamily',
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
70 {name: value for name, value in globals().items()
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
71 if name.isupper() and name.startswith('AF_')})
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
72 globals().update(AddressFamily.__members__)
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
73
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
74 SocketType = IntEnum('SocketType',
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
75 {name: value for name, value in globals().items()
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
76 if name.isupper() and name.startswith('SOCK_')})
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
77 globals().update(SocketType.__members__)
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
78
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
79 def _intenum_converter(value, enum_klass):
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
80 """Convert a numeric family value to an IntEnum member.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
81
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
82 If it's not a known member, return the numeric value itself.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
83 """
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
84 try:
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
85 return enum_klass(value)
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
86 except ValueError:
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
87 return value
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42974
diff changeset
88
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42974
diff changeset
89 _realsocket = socket
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15872
diff changeset
90
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 # WSA error codes
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92 if sys.platform.lower().startswith("win"):
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93 errorTab = {}
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 errorTab[10004] = "The operation was interrupted."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 errorTab[10009] = "A bad file handle was passed."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 errorTab[10013] = "Permission denied."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 errorTab[10022] = "An invalid operation was attempted."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99 errorTab[10035] = "The socket operation would block"
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 errorTab[10036] = "A blocking operation is already in progress."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101 errorTab[10048] = "The network address is in use."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 errorTab[10054] = "The connection has been reset."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 errorTab[10058] = "The network has been shut down."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 errorTab[10060] = "The operation timed out."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 errorTab[10061] = "Connection refused."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 errorTab[10063] = "The name is too long."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107 errorTab[10064] = "The host is down."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 errorTab[10065] = "The host is unreachable."
17071
b45ae8c467aa add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents: 16949
diff changeset
109 __all__.append("errorTab")
24663
b265e76bab88 Extend __all__ with the exports list of the _ssl module.
Guido van Rossum <guido@python.org>
parents: 24662
diff changeset
110
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
112 class socket(_socket.socket):
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
113
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
114 """A subclass of _socket.socket adding the makefile() method."""
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
115
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
116 __slots__ = ["__weakref__", "_io_refs", "_closed"]
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
117
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
118 def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None):
85481
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
119 # For user code address family and type values are IntEnum members, but
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
120 # for the underlying _socket.socket they're just integers. The
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
121 # constructor of _socket.socket converts the given argument to an
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
122 # integer automatically.
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
123 _socket.socket.__init__(self, family, type, proto, fileno)
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
124 self._io_refs = 0
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
125 self._closed = False
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
126
64574
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
127 def __enter__(self):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
128 return self
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
129
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
130 def __exit__(self, *args):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
131 if not self._closed:
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
132 self.close()
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
133
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
134 def __repr__(self):
83230
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
135 """Wrap __repr__() to reveal the real class name and socket
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
136 address(es).
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
137 """
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
138 closed = getattr(self, '_closed', False)
87074
c5751f01b09b Provide a more readable representation of socket on repr().
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 85481
diff changeset
139 s = "<%s.%s%s fd=%i, family=%s, type=%s, proto=%i" \
83230
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
140 % (self.__class__.__module__,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
141 self.__class__.__name__,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
142 " [closed]" if closed else "",
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
143 self.fileno(),
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
144 self.family,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
145 self.type,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
146 self.proto)
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
147 if not closed:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
148 try:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
149 laddr = self.getsockname()
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
150 if laddr:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
151 s += ", laddr=%s" % str(laddr)
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
152 except error:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
153 pass
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
154 try:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
155 raddr = self.getpeername()
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
156 if raddr:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
157 s += ", raddr=%s" % str(raddr)
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
158 except error:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
159 pass
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
160 s += '>'
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
161 return s
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
162
68774
5e13e5e6df30 Issue #11127: Raise a TypeError when trying to pickle a socket object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
163 def __getstate__(self):
5e13e5e6df30 Issue #11127: Raise a TypeError when trying to pickle a socket object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
164 raise TypeError("Cannot serialize socket object")
5e13e5e6df30 Issue #11127: Raise a TypeError when trying to pickle a socket object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
165
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
166 def dup(self):
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
167 """dup() -> socket object
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
168
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
169 Duplicate the socket. Return a new socket object connected to the same
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
170 system resource. The new socket is non-inheritable.
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
171 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
172 fd = dup(self.fileno())
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
173 sock = self.__class__(self.family, self.type, self.proto, fileno=fd)
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
174 sock.settimeout(self.gettimeout())
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
175 return sock
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
176
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
177 def accept(self):
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
178 """accept() -> (socket object, address info)
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
179
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
180 Wait for an incoming connection. Return a new socket
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
181 representing the connection, and the address of the client.
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
182 For IP sockets, the address info is a pair (hostaddr, port).
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
183 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
184 fd, addr = self._accept()
67382
ed0259230611 Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents: 67344
diff changeset
185 sock = socket(self.family, self.type, self.proto, fileno=fd)
ed0259230611 Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents: 67344
diff changeset
186 # Issue #7995: if no default timeout is set and the listening
ed0259230611 Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents: 67344
diff changeset
187 # socket had a (non-zero) timeout, force the new socket in blocking
ed0259230611 Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents: 67344
diff changeset
188 # mode to override platform-specific socket flags inheritance.
ed0259230611 Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents: 67344
diff changeset
189 if getdefaulttimeout() is None and self.gettimeout():
ed0259230611 Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents: 67344
diff changeset
190 sock.setblocking(True)
ed0259230611 Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents: 67344
diff changeset
191 return sock, addr
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
192
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
193 def makefile(self, mode="r", buffering=None, *,
65259
3619c1214007 Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64766
diff changeset
194 encoding=None, errors=None, newline=None):
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
195 """makefile(...) -> an I/O stream connected to the socket
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
196
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
197 The arguments are as for io.open() after the filename,
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
198 except the only mode characters supported are 'r', 'w' and 'b'.
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
199 The semantics are similar too. (XXX refactor to share code?)
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
200 """
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
201 for c in mode:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
202 if c not in {"r", "w", "b"}:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
203 raise ValueError("invalid mode %r (only r, w, b allowed)")
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
204 writing = "w" in mode
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
205 reading = "r" in mode or not writing
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
206 assert reading or writing
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
207 binary = "b" in mode
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
208 rawmode = ""
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
209 if reading:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
210 rawmode += "r"
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
211 if writing:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
212 rawmode += "w"
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
213 raw = SocketIO(self, rawmode)
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
214 self._io_refs += 1
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
215 if buffering is None:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
216 buffering = -1
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
217 if buffering < 0:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
218 buffering = io.DEFAULT_BUFFER_SIZE
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
219 if buffering == 0:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
220 if not binary:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
221 raise ValueError("unbuffered streams must be binary")
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
222 return raw
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
223 if reading and writing:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
224 buffer = io.BufferedRWPair(raw, raw, buffering)
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
225 elif reading:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
226 buffer = io.BufferedReader(raw, buffering)
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
227 else:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
228 assert writing
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
229 buffer = io.BufferedWriter(raw, buffering)
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
230 if binary:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
231 return buffer
65259
3619c1214007 Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64766
diff changeset
232 text = io.TextIOWrapper(buffer, encoding, errors, newline)
42301
f940e470e550 (1) SF patch# 1755214 by Amaury Forgeot d'Arc which makes some tests
Guido van Rossum <guido@python.org>
parents: 41809
diff changeset
233 text.mode = mode
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
234 return text
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
235
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
236 def _decref_socketios(self):
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
237 if self._io_refs > 0:
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
238 self._io_refs -= 1
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
239 if self._closed:
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
240 self.close()
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
241
64332
62930cb05ed8 Issue #808164: Fixed socket.close to avoid references to globals, to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 62903
diff changeset
242 def _real_close(self, _ss=_socket.socket):
64333
03268bf9a256 reformat comment
Benjamin Peterson <benjamin@python.org>
parents: 64332
diff changeset
243 # This function should not reference any globals. See issue #808164.
64332
62930cb05ed8 Issue #808164: Fixed socket.close to avoid references to globals, to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 62903
diff changeset
244 _ss.close(self)
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
245
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
246 def close(self):
64333
03268bf9a256 reformat comment
Benjamin Peterson <benjamin@python.org>
parents: 64332
diff changeset
247 # This function should not reference any globals. See issue #808164.
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
248 self._closed = True
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
249 if self._io_refs <= 0:
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
250 self._real_close()
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
251
76033
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
252 def detach(self):
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
253 """detach() -> file descriptor
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
254
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
255 Close the socket object without closing the underlying file descriptor.
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
256 The object cannot be used after this call, but the file descriptor
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
257 can be reused for other purposes. The file descriptor is returned.
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
258 """
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
259 self._closed = True
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
260 return super().detach()
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
261
85481
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
262 @property
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
263 def family(self):
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
264 """Read-only access to the address family for this socket.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
265 """
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
266 return _intenum_converter(super().family, AddressFamily)
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
267
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
268 @property
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
269 def type(self):
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
270 """Read-only access to the socket type.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
271 """
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
272 return _intenum_converter(super().type, SocketType)
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
273
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
274 if os.name == 'nt':
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
275 def get_inheritable(self):
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
276 return os.get_handle_inheritable(self.fileno())
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
277 def set_inheritable(self, inheritable):
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
278 os.set_handle_inheritable(self.fileno(), inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
279 else:
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
280 def get_inheritable(self):
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
281 return os.get_inheritable(self.fileno())
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
282 def set_inheritable(self, inheritable):
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
283 os.set_inheritable(self.fileno(), inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
284 get_inheritable.__doc__ = "Get the inheritable flag of the socket"
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
285 set_inheritable.__doc__ = "Set the inheritable flag of the socket"
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84442
diff changeset
286
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
287 def fromfd(fd, family, type, proto=0):
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
288 """ fromfd(fd, family, type[, proto]) -> socket object
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
289
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
290 Create a socket object from a duplicate of the given file
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
291 descriptor. The remaining arguments are the same as for socket().
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
292 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
293 nfd = dup(fd)
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
294 return socket(family, type, proto, nfd)
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
295
76150
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
296 if hasattr(_socket.socket, "share"):
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
297 def fromshare(info):
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
298 """ fromshare(info) -> socket object
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
299
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
300 Create a socket object from a the bytes object returned by
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
301 socket.share(pid).
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
302 """
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
303 return socket(0, 0, 0, info)
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
304
64747
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
305 if hasattr(_socket, "socketpair"):
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
306
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
307 def socketpair(family=None, type=SOCK_STREAM, proto=0):
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
308 """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
309
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
310 Create a pair of socket objects from the sockets returned by the platform
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
311 socketpair() function.
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
312 The arguments are the same as for socket() except the default family is
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
313 AF_UNIX if defined on the platform; otherwise, the default is AF_INET.
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
314 """
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
315 if family is None:
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
316 try:
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
317 family = AF_UNIX
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
318 except NameError:
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
319 family = AF_INET
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
320 a, b = _socket.socketpair(family, type, proto)
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
321 a = socket(family, type, proto, a.detach())
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
322 b = socket(family, type, proto, b.detach())
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
323 return a, b
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
324
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
325
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
326 _blocking_errnos = { EAGAIN, EWOULDBLOCK }
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
327
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
328 class SocketIO(io.RawIOBase):
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
329
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
330 """Raw I/O implementation for stream sockets.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
331
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
332 This class supports the makefile() method on sockets. It provides
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
333 the raw I/O interface on top of a socket object.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
334 """
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
335
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
336 # One might wonder why not let FileIO do the job instead. There are two
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
337 # main reasons why FileIO is not adapted:
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
338 # - it wouldn't work under Windows (where you can't used read() and
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
339 # write() on a socket handle)
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
340 # - it wouldn't work with socket timeouts (FileIO would ignore the
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
341 # timeout and consider the socket non-blocking)
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
342
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
343 # XXX More docs
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
344
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
345 def __init__(self, sock, mode):
49800
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
346 if mode not in ("r", "w", "rw", "rb", "wb", "rwb"):
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
347 raise ValueError("invalid mode: %r" % mode)
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
348 io.RawIOBase.__init__(self)
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
349 self._sock = sock
49800
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
350 if "b" not in mode:
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
351 mode += "b"
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
352 self._mode = mode
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
353 self._reading = "r" in mode
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
354 self._writing = "w" in mode
68159
17d1c6cede7e Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents: 67382
diff changeset
355 self._timeout_occurred = False
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
356
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
357 def readinto(self, b):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
358 """Read up to len(b) bytes into the writable buffer *b* and return
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
359 the number of bytes read. If the socket is non-blocking and no bytes
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
360 are available, None is returned.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
361
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
362 If *b* is non-empty, a 0 return value indicates that the connection
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
363 was shutdown at the other end.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
364 """
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
365 self._checkClosed()
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
366 self._checkReadable()
68159
17d1c6cede7e Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents: 67382
diff changeset
367 if self._timeout_occurred:
81039
7d69d04522e3 Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79027
diff changeset
368 raise OSError("cannot read from timed out object")
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
369 while True:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
370 try:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
371 return self._sock.recv_into(b)
68159
17d1c6cede7e Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents: 67382
diff changeset
372 except timeout:
17d1c6cede7e Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents: 67382
diff changeset
373 self._timeout_occurred = True
17d1c6cede7e Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents: 67382
diff changeset
374 raise
73085
90f81045613a Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents: 68774
diff changeset
375 except InterruptedError:
90f81045613a Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents: 68774
diff changeset
376 continue
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
377 except error as e:
73085
90f81045613a Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents: 68774
diff changeset
378 if e.args[0] in _blocking_errnos:
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
379 return None
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
380 raise
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
381
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
382 def write(self, b):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
383 """Write the given bytes or bytearray object *b* to the socket
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
384 and return the number of bytes written. This can be less than
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
385 len(b) if not all data could be written. If the socket is
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
386 non-blocking and no bytes could be written None is returned.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
387 """
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
388 self._checkClosed()
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
389 self._checkWritable()
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
390 try:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
391 return self._sock.send(b)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
392 except error as e:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
393 # XXX what about EINTR?
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
394 if e.args[0] in _blocking_errnos:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
395 return None
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
396 raise
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
397
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
398 def readable(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
399 """True if the SocketIO is open for reading.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
400 """
79026
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
401 if self.closed:
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
402 raise ValueError("I/O operation on closed socket.")
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
403 return self._reading
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
404
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
405 def writable(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
406 """True if the SocketIO is open for writing.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
407 """
79026
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
408 if self.closed:
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
409 raise ValueError("I/O operation on closed socket.")
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
410 return self._writing
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
411
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
412 def seekable(self):
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
413 """True if the SocketIO is open for seeking.
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
414 """
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
415 if self.closed:
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
416 raise ValueError("I/O operation on closed socket.")
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
417 return super().seekable()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
418
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
419 def fileno(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
420 """Return the file descriptor of the underlying socket.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
421 """
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
422 self._checkClosed()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
423 return self._sock.fileno()
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
424
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
425 @property
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
426 def name(self):
67344
f4a808f5a66d Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65707
diff changeset
427 if not self.closed:
f4a808f5a66d Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65707
diff changeset
428 return self.fileno()
f4a808f5a66d Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65707
diff changeset
429 else:
f4a808f5a66d Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65707
diff changeset
430 return -1
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
431
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
432 @property
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
433 def mode(self):
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
434 return self._mode
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
435
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
436 def close(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
437 """Close the SocketIO object. This doesn't close the underlying
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
438 socket, except if all references to it have disappeared.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
439 """
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
440 if self.closed:
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
441 return
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
442 io.RawIOBase.close(self)
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
443 self._sock._decref_socketios()
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
444 self._sock = None
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
445
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
446
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
447 def getfqdn(name=''):
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
448 """Get fully qualified domain name from name.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
449
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
450 An empty argument is interpreted as meaning the local host.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
451
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
452 First the hostname returned by gethostbyaddr() is checked, then
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
453 possibly existing aliases. In case no FQDN is available, hostname
34733
e83309d43a82 Fix test for socket.getfqdn() to also include the name returned by
Brett Cannon <bcannon@gmail.com>
parents: 33063
diff changeset
454 from gethostname() is returned.
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
455 """
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
456 name = name.strip()
14014
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 14007
diff changeset
457 if not name or name == '0.0.0.0':
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
458 name = gethostname()
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
459 try:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
460 hostname, aliases, ipaddrs = gethostbyaddr(name)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
461 except error:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
462 pass
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
463 else:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
464 aliases.insert(0, hostname)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
465 for name in aliases:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
466 if '.' in name:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
467 break
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
468 else:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
469 name = hostname
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
470 return name
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
471
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
472
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
473 _GLOBAL_DEFAULT_TIMEOUT = object()
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
474
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
475 def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
476 source_address=None):
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
477 """Connect to *address* and return the socket object.
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
478
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
479 Convenience function. Connect to *address* (a 2-tuple ``(host,
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
480 port)``) and return the socket object. Passing the optional
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
481 *timeout* parameter will set the timeout on the socket instance
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
482 before attempting to connect. If no *timeout* is supplied, the
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
483 global default timeout setting returned by :func:`getdefaulttimeout`
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
484 is used. If *source_address* is set it must be a tuple of (host, port)
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
485 for the socket to bind as a source address before making the connection.
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
486 An host of '' or port 0 tells the OS to use the default.
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
487 """
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
488
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
489 host, port = address
64536
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
490 err = None
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
491 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
492 af, socktype, proto, canonname, sa = res
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
493 sock = None
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
494 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
495 sock = socket(af, socktype, proto)
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44305
diff changeset
496 if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
497 sock.settimeout(timeout)
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
498 if source_address:
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
499 sock.bind(source_address)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
500 sock.connect(sa)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
501 return sock
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
502
64536
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
503 except error as _:
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
504 err = _
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
505 if sock is not None:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
506 sock.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
507
64536
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
508 if err is not None:
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
509 raise err
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
510 else:
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
511 raise error("getaddrinfo returns an empty list")
85481
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
512
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
513 def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
514 """Resolve host and port into list of address info entries.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
515
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
516 Translate the host/port argument into a sequence of 5-tuples that contain
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
517 all the necessary arguments for creating a socket connected to that service.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
518 host is a domain name, a string representation of an IPv4/v6 address or
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
519 None. port is a string service name such as 'http', a numeric port number or
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
520 None. By passing None as the value of host and port, you can pass NULL to
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
521 the underlying C API.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
522
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
523 The family, type and proto arguments can be optionally specified in order to
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
524 narrow the list of addresses returned. Passing zero as a value for each of
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
525 these arguments selects the full range of results.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
526 """
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
527 # We override this function since we want to translate the numeric family
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
528 # and socket type values to enum constants.
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
529 addrlist = []
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
530 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
531 af, socktype, proto, canonname, sa = res
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
532 addrlist.append((_intenum_converter(af, AddressFamily),
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
533 _intenum_converter(socktype, SocketType),
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
534 proto, canonname, sa))
038543d34166 Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Eli Bendersky <eliben@gmail.com>
parents: 85420
diff changeset
535 return addrlist