Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ecbf2de

Browse files
committed
Merged revisions 84825-84826,84830 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r84825 | antoine.pitrou | 2010-09-15 10:39:25 +0200 (mer., 15 sept. 2010) | 3 lines Add a comment explaining why SocketIO is needed ........ r84826 | antoine.pitrou | 2010-09-15 11:32:45 +0200 (mer., 15 sept. 2010) | 3 lines Improve docs for socket.makefile() and SocketIO ........ r84830 | antoine.pitrou | 2010-09-15 13:12:57 +0200 (mer., 15 sept. 2010) | 3 lines Reverted unwanted change in r84826 ........
1 parent 984ce40 commit ecbf2de

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

Doc/library/socket.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,9 @@ correspond to Unix system calls applicable to sockets.
597597
arguments are interpreted the same way as by the built-in :func:`open`
598598
function.
599599

600-
The returned file object references a :cfunc:`dup`\ ped version of the
601-
socket file descriptor, so the file object and socket object may be
602-
closed or garbage-collected independently. The socket must be in
603-
blocking mode (it can not have a timeout).
600+
Closing the file object won't close the socket unless there are no
601+
remaining references to the socket. The socket must be in blocking mode
602+
(it can not have a timeout).
604603

605604

606605
.. method:: socket.recv(bufsize[, flags])

Lib/socket.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ class SocketIO(io.RawIOBase):
195195
the raw I/O interface on top of a socket object.
196196
"""
197197

198+
# One might wonder why not let FileIO do the job instead. There are two
199+
# main reasons why FileIO is not adapted:
200+
# - it wouldn't work under Windows (where you can't used read() and
201+
# write() on a socket handle)
202+
# - it wouldn't work with socket timeouts (FileIO would ignore the
203+
# timeout and consider the socket non-blocking)
204+
198205
# XXX More docs
199206

200207
def __init__(self, sock, mode):
@@ -209,22 +216,40 @@ def __init__(self, sock, mode):
209216
self._writing = "w" in mode
210217

211218
def readinto(self, b):
219+
"""Read up to len(b) bytes into the writable buffer *b* and return
220+
the number of bytes read. If the socket is non-blocking and no bytes
221+
are available, None is returned.
222+
223+
If *b* is non-empty, a 0 return value indicates that the connection
224+
was shutdown at the other end.
225+
"""
212226
self._checkClosed()
213227
self._checkReadable()
214228
return self._sock.recv_into(b)
215229

216230
def write(self, b):
231+
"""Write the given bytes or bytearray object *b* to the socket
232+
and return the number of bytes written. This can be less than
233+
len(b) if not all data could be written. If the socket is
234+
non-blocking and no bytes could be written None is returned.
235+
"""
217236
self._checkClosed()
218237
self._checkWritable()
219238
return self._sock.send(b)
220239

221240
def readable(self):
241+
"""True if the SocketIO is open for reading.
242+
"""
222243
return self._reading and not self.closed
223244

224245
def writable(self):
246+
"""True if the SocketIO is open for writing.
247+
"""
225248
return self._writing and not self.closed
226249

227250
def fileno(self):
251+
"""Return the file descriptor of the underlying socket.
252+
"""
228253
self._checkClosed()
229254
return self._sock.fileno()
230255

@@ -237,6 +262,9 @@ def mode(self):
237262
return self._mode
238263

239264
def close(self):
265+
"""Close the SocketIO object. This doesn't close the underlying
266+
socket, except if all references to it have disappeared.
267+
"""
240268
if self.closed:
241269
return
242270
io.RawIOBase.close(self)

0 commit comments

Comments
 (0)