Mercurial > cpython-lru_cache-weakref
annotate Lib/socket.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 72e3e85bac8b |
| children | 4f2905ea7809 |
| 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 [*] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 gethostname() -- return the current hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 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
|
17 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
|
18 getservbyname() -- map a service name and a protocol name to a port number |
|
54512
f0f84af15957
Typo in socket.py. Thanks Pablo Torres Navarrete.
Mark Dickinson <dickinsm@gmail.com>
parents:
53047
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 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
|
23 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 ssl() -- secure socket layer support (only available if configured) |
|
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 |
|
58112
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
27 create_connection() -- connects to an address, with an optional timeout and |
|
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
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 * |
|
53047
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
49 from functools import partial |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
50 from types import MethodType |
|
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
|
51 |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
21473
diff
changeset
|
52 try: |
|
24663
b265e76bab88
Extend __all__ with the exports list of the _ssl module.
Guido van Rossum <guido@python.org>
parents:
24662
diff
changeset
|
53 import _ssl |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
21473
diff
changeset
|
54 except ImportError: |
|
43008
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
55 # no SSL support |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
56 pass |
|
43008
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
57 else: |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
58 def ssl(sock, keyfile=None, certfile=None): |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
59 # we do an internal import here because the ssl |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
60 # module imports the socket module |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
61 import ssl as _realssl |
|
43325
193020bf52dc
More work on SSL support.
Bill Janssen <janssen@parc.com>
parents:
43008
diff
changeset
|
62 warnings.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.", |
|
43008
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
63 DeprecationWarning, stacklevel=2) |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
64 return _realssl.sslwrap_simple(sock, keyfile, certfile) |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 |
|
43008
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
66 # we need to import the same constants we used to... |
|
43325
193020bf52dc
More work on SSL support.
Bill Janssen <janssen@parc.com>
parents:
43008
diff
changeset
|
67 from _ssl import SSLError as sslerror |
|
43008
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
68 from _ssl import \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
69 RAND_add, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
70 RAND_egd, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
71 RAND_status, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
72 SSL_ERROR_ZERO_RETURN, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
73 SSL_ERROR_WANT_READ, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
74 SSL_ERROR_WANT_WRITE, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
75 SSL_ERROR_WANT_X509_LOOKUP, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
76 SSL_ERROR_SYSCALL, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
77 SSL_ERROR_SSL, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
78 SSL_ERROR_WANT_CONNECT, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
79 SSL_ERROR_EOF, \ |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
80 SSL_ERROR_INVALID_ERROR_CODE |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
81 |
|
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
82 import os, sys, warnings |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
84 try: |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
85 from cStringIO import StringIO |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
86 except ImportError: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
87 from StringIO import StringIO |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
88 |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
89 try: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
90 import errno |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
91 except ImportError: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
92 errno = None |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
93 EBADF = getattr(errno, 'EBADF', 9) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
94 EINTR = getattr(errno, 'EINTR', 4) |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
95 |
|
52936
5b01fe513f92
Merged revisions 71329 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48195
diff
changeset
|
96 __all__ = ["getfqdn", "create_connection"] |
| 16613 | 97 __all__.extend(os._get_exports_list(_socket)) |
|
43008
4771639fc049
This contains a number of things:
Bill Janssen <janssen@parc.com>
parents:
42828
diff
changeset
|
98 |
| 16613 | 99 |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
100 _realsocket = socket |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 # WSA error codes |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
103 if sys.platform.lower().startswith("win"): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 errorTab = {} |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 errorTab[10004] = "The operation was interrupted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
106 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
|
107 errorTab[10013] = "Permission denied." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 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
|
109 errorTab[10022] = "An invalid operation was attempted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
110 errorTab[10035] = "The socket operation would block" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
111 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
|
112 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
|
113 errorTab[10054] = "The connection has been reset." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 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
|
115 errorTab[10060] = "The operation timed out." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 errorTab[10061] = "Connection refused." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 errorTab[10063] = "The name is too long." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 errorTab[10064] = "The host is down." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 errorTab[10065] = "The host is unreachable." |
|
17071
b45ae8c467aa
add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents:
16949
diff
changeset
|
120 __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
|
121 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 def getfqdn(name=''): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 """Get fully qualified domain name from name. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
126 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
127 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
|
128 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
129 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
|
130 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
|
131 from gethostname() is returned. |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
132 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 name = name.strip() |
|
14014
3d68d5bccd40
updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
14007
diff
changeset
|
134 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
|
135 name = gethostname() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 hostname, aliases, ipaddrs = gethostbyaddr(name) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 except error: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 pass |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 aliases.insert(0, hostname) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 for name in aliases: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
143 if '.' in name: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
144 break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 name = hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 return name |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 |
|
19364
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
150 _socketmethods = ( |
|
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
151 'bind', 'connect', 'connect_ex', 'fileno', 'listen', |
|
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
152 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', |
|
28888
d3164a304598
Regain throughput lost with the _socketobject wrapper. The
Skip Montanaro <skip@pobox.com>
parents:
28818
diff
changeset
|
153 'sendall', 'setblocking', |
|
44478
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
44474
diff
changeset
|
154 'settimeout', 'gettimeout', 'shutdown') |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
44474
diff
changeset
|
155 |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
44474
diff
changeset
|
156 if os.name == "nt": |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
44474
diff
changeset
|
157 _socketmethods = _socketmethods + ('ioctl',) |
|
19364
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
158 |
|
29066
90342f395721
Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents:
28890
diff
changeset
|
159 if sys.platform == "riscos": |
|
90342f395721
Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents:
28890
diff
changeset
|
160 _socketmethods = _socketmethods + ('sleeptaskw',) |
|
90342f395721
Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents:
28890
diff
changeset
|
161 |
|
38976
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
162 # All the method names that must be delegated to either the real socket |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
163 # object or the _closedsocket object. |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
164 _delegate_methods = ("recv", "recvfrom", "recv_into", "recvfrom_into", |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
165 "send", "sendto") |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
166 |
|
24666
4fbf534211b9
The _socketobject class has no need for a __del__ method: all it did was
Guido van Rossum <guido@python.org>
parents:
24665
diff
changeset
|
167 class _closedsocket(object): |
|
4fbf534211b9
The _socketobject class has no need for a __del__ method: all it did was
Guido van Rossum <guido@python.org>
parents:
24665
diff
changeset
|
168 __slots__ = [] |
|
28888
d3164a304598
Regain throughput lost with the _socketobject wrapper. The
Skip Montanaro <skip@pobox.com>
parents:
28818
diff
changeset
|
169 def _dummy(*args): |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
170 raise error(EBADF, 'Bad file descriptor') |
|
38976
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
171 # All _delegate_methods must also be initialized here. |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
172 send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
173 __getattr__ = _dummy |
|
24666
4fbf534211b9
The _socketobject class has no need for a __del__ method: all it did was
Guido van Rossum <guido@python.org>
parents:
24665
diff
changeset
|
174 |
|
42385
e9916a4e06a4
Bug #978833: Close https sockets by releasing the _ssl object.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
175 # Wrapper around platform socket objects. This implements |
|
e9916a4e06a4
Bug #978833: Close https sockets by releasing the _ssl object.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
176 # a platform-independent dup() functionality. The |
|
e9916a4e06a4
Bug #978833: Close https sockets by releasing the _ssl object.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
177 # implementation currently relies on reference counting |
|
e9916a4e06a4
Bug #978833: Close https sockets by releasing the _ssl object.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
178 # to close the underlying socket object. |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
179 class _socketobject(object): |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
180 |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
181 __doc__ = _realsocket.__doc__ |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
182 |
|
38976
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
183 __slots__ = ["_sock", "__weakref__"] + list(_delegate_methods) |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
184 |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
185 def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None): |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
186 if _sock is None: |
|
24662
a61893d7cef2
Oops, stupid tabs. Sorry again.
Guido van Rossum <guido@python.org>
parents:
24661
diff
changeset
|
187 _sock = _realsocket(family, type, proto) |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
188 self._sock = _sock |
|
38976
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
189 for method in _delegate_methods: |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
190 setattr(self, method, getattr(_sock, method)) |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
191 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
192 def close(self): |
|
24666
4fbf534211b9
The _socketobject class has no need for a __del__ method: all it did was
Guido van Rossum <guido@python.org>
parents:
24665
diff
changeset
|
193 self._sock = _closedsocket() |
|
38976
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
194 dummy = self._sock._dummy |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
195 for method in _delegate_methods: |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38580
diff
changeset
|
196 setattr(self, method, dummy) |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
197 close.__doc__ = _realsocket.close.__doc__ |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
198 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
199 def accept(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
200 sock, addr = self._sock.accept() |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
201 return _socketobject(_sock=sock), addr |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
202 accept.__doc__ = _realsocket.accept.__doc__ |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
203 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
204 def dup(self): |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
205 """dup() -> socket object |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
206 |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
207 Return a new socket object connected to the same system resource.""" |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
208 return _socketobject(_sock=self._sock) |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
209 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
210 def makefile(self, mode='r', bufsize=-1): |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
211 """makefile([mode[, bufsize]]) -> file object |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
212 |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
213 Return a regular file object corresponding to the socket. The mode |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
214 and bufsize arguments are as for the built-in open() function.""" |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
215 return _fileobject(self._sock, mode, bufsize) |
|
37136
71c5694e3423
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
37123
diff
changeset
|
216 |
|
37123
251ecec04f1b
Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents:
37069
diff
changeset
|
217 family = property(lambda self: self._sock.family, doc="the socket family") |
|
251ecec04f1b
Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents:
37069
diff
changeset
|
218 type = property(lambda self: self._sock.type, doc="the socket type") |
|
251ecec04f1b
Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents:
37069
diff
changeset
|
219 proto = property(lambda self: self._sock.proto, doc="the socket protocol") |
|
37069
dd49847a60d3
RFE #567972: Socket objects' family, type and proto properties are
Georg Brandl <georg@python.org>
parents:
34733
diff
changeset
|
220 |
|
53047
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
221 def meth(name,self,*args): |
|
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
222 return getattr(self._sock,name)(*args) |
|
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
223 |
|
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
224 for _m in _socketmethods: |
|
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
225 p = partial(meth,_m) |
|
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
226 p.__name__ = _m |
|
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
227 p.__doc__ = getattr(_realsocket,_m).__doc__ |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
228 m = MethodType(p,None,_socketobject) |
|
53047
7e229ca0b92a
remove unpleasant exec
Chris Withers <chris@simplistix.co.uk>
parents:
52936
diff
changeset
|
229 setattr(_socketobject,_m,m) |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
230 |
|
28443
161b8bd8a226
The socket module now always uses the _socketobject wrapper class, even on
Skip Montanaro <skip@pobox.com>
parents:
24944
diff
changeset
|
231 socket = SocketType = _socketobject |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
232 |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
233 class _fileobject(object): |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
234 """Faux file object attached to a socket object.""" |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
235 |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
236 default_bufsize = 8192 |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
237 name = "<socket>" |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
238 |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
239 __slots__ = ["mode", "bufsize", "softspace", |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
240 # "closed" is a property, see below |
|
54519
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
241 "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf", "_wbuf_len", |
|
40684
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
39190
diff
changeset
|
242 "_close"] |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
243 |
|
40684
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
39190
diff
changeset
|
244 def __init__(self, sock, mode='rb', bufsize=-1, close=False): |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
245 self._sock = sock |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
246 self.mode = mode # Not actually used in this version |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
247 if bufsize < 0: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
248 bufsize = self.default_bufsize |
|
24662
a61893d7cef2
Oops, stupid tabs. Sorry again.
Guido van Rossum <guido@python.org>
parents:
24661
diff
changeset
|
249 self.bufsize = bufsize |
|
a61893d7cef2
Oops, stupid tabs. Sorry again.
Guido van Rossum <guido@python.org>
parents:
24661
diff
changeset
|
250 self.softspace = False |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
251 # _rbufsize is the suggested recv buffer size. It is *strictly* |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
252 # obeyed within readline() for recv calls. If it is larger than |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
253 # default_bufsize it will be used for recv calls within read(). |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
254 if bufsize == 0: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
255 self._rbufsize = 1 |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
256 elif bufsize == 1: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
257 self._rbufsize = self.default_bufsize |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
258 else: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
259 self._rbufsize = bufsize |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
260 self._wbufsize = bufsize |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
261 # We use StringIO for the read buffer to avoid holding a list |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
262 # of variously sized string objects which have been known to |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
263 # fragment the heap due to how they are malloc()ed and often |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
264 # realloc()ed down much smaller than their original allocation. |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
265 self._rbuf = StringIO() |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
266 self._wbuf = [] # A list of strings |
|
54519
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
267 self._wbuf_len = 0 |
|
40684
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
39190
diff
changeset
|
268 self._close = close |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
269 |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
270 def _getclosed(self): |
|
31833
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
29465
diff
changeset
|
271 return self._sock is None |
|
24661
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
272 closed = property(_getclosed, doc="True if the file is closed") |
|
2f411f2d791a
Another refactoring. Changed 'socket' from being a factory function
Guido van Rossum <guido@python.org>
parents:
24656
diff
changeset
|
273 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
274 def close(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
275 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
276 if self._sock: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
277 self.flush() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
278 finally: |
|
40684
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
39190
diff
changeset
|
279 if self._close: |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
39190
diff
changeset
|
280 self._sock.close() |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
281 self._sock = None |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
282 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
283 def __del__(self): |
|
29311
ed70779f6402
I saw errors from _fileobject.__del__ about missing self._sock. This
Guido van Rossum <guido@python.org>
parents:
29066
diff
changeset
|
284 try: |
|
ed70779f6402
I saw errors from _fileobject.__del__ about missing self._sock. This
Guido van Rossum <guido@python.org>
parents:
29066
diff
changeset
|
285 self.close() |
|
ed70779f6402
I saw errors from _fileobject.__del__ about missing self._sock. This
Guido van Rossum <guido@python.org>
parents:
29066
diff
changeset
|
286 except: |
|
ed70779f6402
I saw errors from _fileobject.__del__ about missing self._sock. This
Guido van Rossum <guido@python.org>
parents:
29066
diff
changeset
|
287 # close() may fail if __init__ didn't complete |
|
ed70779f6402
I saw errors from _fileobject.__del__ about missing self._sock. This
Guido van Rossum <guido@python.org>
parents:
29066
diff
changeset
|
288 pass |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
289 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
290 def flush(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
291 if self._wbuf: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
292 data = "".join(self._wbuf) |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
293 self._wbuf = [] |
|
54519
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
294 self._wbuf_len = 0 |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
295 buffer_size = max(self._rbufsize, self.default_bufsize) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
296 data_size = len(data) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
297 write_offset = 0 |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
298 view = memoryview(data) |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
299 try: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
300 while write_offset < data_size: |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
301 self._sock.sendall(view[write_offset:write_offset+buffer_size]) |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
302 write_offset += buffer_size |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
303 finally: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
304 if write_offset < data_size: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
305 remainder = data[write_offset:] |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
306 del view, data # explicit free |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
307 self._wbuf.append(remainder) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
308 self._wbuf_len = len(remainder) |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
309 |
| 23844 | 310 def fileno(self): |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
311 return self._sock.fileno() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
312 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
313 def write(self, data): |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
314 data = str(data) # XXX Should really reject non-string non-buffers |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
315 if not data: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
316 return |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
317 self._wbuf.append(data) |
|
54519
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
318 self._wbuf_len += len(data) |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
319 if (self._wbufsize == 0 or |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
320 self._wbufsize == 1 and '\n' in data or |
|
54519
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
321 self._wbuf_len >= self._wbufsize): |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
322 self.flush() |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
323 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
324 def writelines(self, list): |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
325 # XXX We could do better here for very long lists |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
326 # XXX Should really reject non-string non-buffers |
|
54519
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
327 lines = filter(None, map(str, list)) |
|
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
328 self._wbuf_len += sum(map(len, lines)) |
|
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
329 self._wbuf.extend(lines) |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
330 if (self._wbufsize <= 1 or |
|
54519
e80e25e6a187
http://bugs.python.org/issue6117
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54512
diff
changeset
|
331 self._wbuf_len >= self._wbufsize): |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
332 self.flush() |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
333 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
334 def read(self, size=-1): |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
335 # Use max, disallow tiny reads in a loop as they are very inefficient. |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
336 # We never leave read() with any leftover data from a new recv() call |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
337 # in our internal buffer. |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
338 rbufsize = max(self._rbufsize, self.default_bufsize) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
339 # Our use of StringIO rather than lists of string objects returned by |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
340 # recv() minimizes memory usage and fragmentation that occurs when |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
341 # rbufsize is large compared to the typical return value of recv(). |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
342 buf = self._rbuf |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
343 buf.seek(0, 2) # seek end |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
344 if size < 0: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
345 # Read until EOF |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
346 self._rbuf = StringIO() # reset _rbuf. we consume it via buf. |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
347 while True: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
348 try: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
349 data = self._sock.recv(rbufsize) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
350 except error, e: |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
351 if e.args[0] == EINTR: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
352 continue |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
353 raise |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
354 if not data: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
355 break |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
356 buf.write(data) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
357 return buf.getvalue() |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
358 else: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
359 # Read until size bytes or EOF seen, whichever comes first |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
360 buf_len = buf.tell() |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
361 if buf_len >= size: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
362 # Already have size bytes in our buffer? Extract and return. |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
363 buf.seek(0) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
364 rv = buf.read(size) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
365 self._rbuf = StringIO() |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
366 self._rbuf.write(buf.read()) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
367 return rv |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
368 |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
369 self._rbuf = StringIO() # reset _rbuf. we consume it via buf. |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
370 while True: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
371 left = size - buf_len |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
372 # recv() will malloc the amount of memory given as its |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
373 # parameter even though it often returns much less data |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
374 # than that. The returned data string is short lived |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
375 # as we copy it into a StringIO and free it. This avoids |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
376 # fragmentation issues on many platforms. |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
377 try: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
378 data = self._sock.recv(left) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
379 except error, e: |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
380 if e.args[0] == EINTR: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
381 continue |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
382 raise |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
383 if not data: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
384 break |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
385 n = len(data) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
386 if n == size and not buf_len: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
387 # Shortcut. Avoid buffer data copies when: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
388 # - We have no data in our buffer. |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
389 # AND |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
390 # - Our call to recv returned exactly the |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
391 # number of bytes we were asked to read. |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
392 return data |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
393 if n == left: |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
394 buf.write(data) |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
395 del data # explicit free |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
396 break |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
397 assert n <= left, "recv(%d) returned %d bytes" % (left, n) |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
398 buf.write(data) |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
399 buf_len += n |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
400 del data # explicit free |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
401 #assert buf_len == buf.tell() |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
402 return buf.getvalue() |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
403 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
404 def readline(self, size=-1): |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
405 buf = self._rbuf |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
406 buf.seek(0, 2) # seek end |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
407 if buf.tell() > 0: |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
408 # check if we already have it in our buffer |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
409 buf.seek(0) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
410 bline = buf.readline(size) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
411 if bline.endswith('\n') or len(bline) == size: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
412 self._rbuf = StringIO() |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
413 self._rbuf.write(buf.read()) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
414 return bline |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
415 del bline |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
416 if size < 0: |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
417 # Read until \n or EOF, whichever comes first |
|
24665
d25cf94d20b1
OK, one more hack: speed up the case of readline() in unbuffered mode.
Guido van Rossum <guido@python.org>
parents:
24664
diff
changeset
|
418 if self._rbufsize <= 1: |
|
d25cf94d20b1
OK, one more hack: speed up the case of readline() in unbuffered mode.
Guido van Rossum <guido@python.org>
parents:
24664
diff
changeset
|
419 # Speed up unbuffered case |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
420 buf.seek(0) |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
421 buffers = [buf.read()] |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
422 self._rbuf = StringIO() # reset _rbuf. we consume it via buf. |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
423 data = None |
|
24665
d25cf94d20b1
OK, one more hack: speed up the case of readline() in unbuffered mode.
Guido van Rossum <guido@python.org>
parents:
24664
diff
changeset
|
424 recv = self._sock.recv |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
425 while True: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
426 try: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
427 while data != "\n": |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
428 data = recv(1) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
429 if not data: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
430 break |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
431 buffers.append(data) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
432 except error, e: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
433 # The try..except to catch EINTR was moved outside the |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
434 # recv loop to avoid the per byte overhead. |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
435 if e.args[0] == EINTR: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
436 continue |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
437 raise |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
438 break |
|
24665
d25cf94d20b1
OK, one more hack: speed up the case of readline() in unbuffered mode.
Guido van Rossum <guido@python.org>
parents:
24664
diff
changeset
|
439 return "".join(buffers) |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
440 |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
441 buf.seek(0, 2) # seek end |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
442 self._rbuf = StringIO() # reset _rbuf. we consume it via buf. |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
443 while True: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
444 try: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
445 data = self._sock.recv(self._rbufsize) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
446 except error, e: |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
447 if e.args[0] == EINTR: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
448 continue |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
449 raise |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
450 if not data: |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
451 break |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
452 nl = data.find('\n') |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
453 if nl >= 0: |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
454 nl += 1 |
|
48195
65901ffffc6a
Remove buffer() usage in the socket module by just slicing directly on the
Brett Cannon <bcannon@gmail.com>
parents:
47372
diff
changeset
|
455 buf.write(data[:nl]) |
|
65901ffffc6a
Remove buffer() usage in the socket module by just slicing directly on the
Brett Cannon <bcannon@gmail.com>
parents:
47372
diff
changeset
|
456 self._rbuf.write(data[nl:]) |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
457 del data |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
458 break |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
459 buf.write(data) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
460 return buf.getvalue() |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
461 else: |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
462 # Read until size bytes or \n or EOF seen, whichever comes first |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46623
diff
changeset
|
463 buf.seek(0, 2) # seek end |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
464 buf_len = buf.tell() |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
465 if buf_len >= size: |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
466 buf.seek(0) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
467 rv = buf.read(size) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
468 self._rbuf = StringIO() |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
469 self._rbuf.write(buf.read()) |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
470 return rv |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
471 self._rbuf = StringIO() # reset _rbuf. we consume it via buf. |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
472 while True: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
473 try: |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
474 data = self._sock.recv(self._rbufsize) |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
475 except error, e: |
|
56521
9e718d8f71e8
Fix py3k warnings in the socket module
Antoine Pitrou <solipsis@pitrou.net>
parents:
55627
diff
changeset
|
476 if e.args[0] == EINTR: |
|
55627
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
477 continue |
|
fbf8b791ce81
Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents:
54519
diff
changeset
|
478 raise |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
479 if not data: |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
480 break |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
481 left = size - buf_len |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
482 # did we just receive a newline? |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
483 nl = data.find('\n', 0, left) |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
484 if nl >= 0: |
|
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
485 nl += 1 |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
486 # save the excess data to _rbuf |
|
48195
65901ffffc6a
Remove buffer() usage in the socket module by just slicing directly on the
Brett Cannon <bcannon@gmail.com>
parents:
47372
diff
changeset
|
487 self._rbuf.write(data[nl:]) |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
488 if buf_len: |
|
48195
65901ffffc6a
Remove buffer() usage in the socket module by just slicing directly on the
Brett Cannon <bcannon@gmail.com>
parents:
47372
diff
changeset
|
489 buf.write(data[:nl]) |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
490 break |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
491 else: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
492 # Shortcut. Avoid data copy through buf when returning |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
493 # a substring of our first recv(). |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
494 return data[:nl] |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
495 n = len(data) |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
496 if n == size and not buf_len: |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
497 # Shortcut. Avoid data copy through buf when |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
498 # returning exactly all of our first recv(). |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
499 return data |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
500 if n >= left: |
|
48195
65901ffffc6a
Remove buffer() usage in the socket module by just slicing directly on the
Brett Cannon <bcannon@gmail.com>
parents:
47372
diff
changeset
|
501 buf.write(data[:left]) |
|
65901ffffc6a
Remove buffer() usage in the socket module by just slicing directly on the
Brett Cannon <bcannon@gmail.com>
parents:
47372
diff
changeset
|
502 self._rbuf.write(data[left:]) |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
503 break |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
504 buf.write(data) |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
505 buf_len += n |
|
46623
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
506 #assert buf_len == buf.tell() |
|
54606ea9f4c7
This should fix issue2632. A long description of the two competing
Gregory P. Smith <greg@mad-scientist.com>
parents:
45475
diff
changeset
|
507 return buf.getvalue() |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
508 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
509 def readlines(self, sizehint=0): |
|
14682
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
510 total = 0 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
511 list = [] |
|
24664
6a6d02be321c
Another refactoring of read() and readline(), this time based on the
Guido van Rossum <guido@python.org>
parents:
24663
diff
changeset
|
512 while True: |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
513 line = self.readline() |
| 23844 | 514 if not line: |
| 515 break | |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
516 list.append(line) |
|
14682
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
517 total += len(line) |
|
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
518 if sizehint and total >= sizehint: |
|
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
519 break |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
520 return list |
|
24656
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
521 |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
522 # Iterator protocols |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
523 |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
524 def __iter__(self): |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
525 return self |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
526 |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
527 def next(self): |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
528 line = self.readline() |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
529 if not line: |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
530 raise StopIteration |
|
e61b118e9cab
Major restructuring of _fileobject. Hopefully several things now work
Guido van Rossum <guido@python.org>
parents:
24649
diff
changeset
|
531 return line |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
532 |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
533 _GLOBAL_DEFAULT_TIMEOUT = object() |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
534 |
|
58112
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
535 def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, |
|
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
536 source_address=None): |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
537 """Connect to *address* and return the socket object. |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
538 |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
539 Convenience function. Connect to *address* (a 2-tuple ``(host, |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
540 port)``) and return the socket object. Passing the optional |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
541 *timeout* parameter will set the timeout on the socket instance |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
542 before attempting to connect. If no *timeout* is supplied, the |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
543 global default timeout setting returned by :func:`getdefaulttimeout` |
|
58112
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
544 is used. If *source_address* is set it must be a tuple of (host, port) |
|
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
545 for the socket to bind as a source address before making the connection. |
|
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
546 An host of '' or port 0 tells the OS to use the default. |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
547 """ |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41288
diff
changeset
|
548 |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
549 msg = "getaddrinfo returns an empty list" |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
550 host, port = address |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
551 for res in getaddrinfo(host, port, 0, SOCK_STREAM): |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
552 af, socktype, proto, canonname, sa = res |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
553 sock = None |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
554 try: |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
555 sock = socket(af, socktype, proto) |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46684
diff
changeset
|
556 if timeout is not _GLOBAL_DEFAULT_TIMEOUT: |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
557 sock.settimeout(timeout) |
|
58112
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
558 if source_address: |
|
72e3e85bac8b
Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents:
56521
diff
changeset
|
559 sock.bind(source_address) |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
560 sock.connect(sa) |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
561 return sock |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41288
diff
changeset
|
562 |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
563 except error, msg: |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
564 if sock is not None: |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
565 sock.close() |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
566 |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
41285
diff
changeset
|
567 raise error, msg |
