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

Skip to content

Commit e3658a7

Browse files
committed
Issue #19882: tweak docs for socket.close()
1 parent abbc8ca commit e3658a7

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

Doc/library/socket.rst

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,9 @@ The module :mod:`socket` exports the following constants and functions:
701701
Socket Objects
702702
--------------
703703

704-
Socket objects have the following methods. Except for :meth:`makefile` these
705-
correspond to Unix system calls applicable to sockets.
704+
Socket objects have the following methods. Except for
705+
:meth:`~socket.makefile`, these correspond to Unix system calls applicable
706+
to sockets.
706707

707708

708709
.. method:: socket.accept()
@@ -721,9 +722,15 @@ correspond to Unix system calls applicable to sockets.
721722

722723
.. method:: socket.close()
723724

724-
Close the socket. All future operations on the socket object will fail. The
725-
remote end will receive no more data (after queued data is flushed). Sockets are
726-
automatically closed when they are garbage-collected.
725+
Mark the socket closed. The underlying system resource (e.g. a file
726+
descriptor) is also closed when all file objects from :meth:`makefile()`
727+
are closed. Once that happens, all future operations on the socket
728+
object will fail. The remote end will receive no more data (after
729+
queued data is flushed).
730+
731+
Sockets are automatically closed when they are garbage-collected, but
732+
it is recommended to :meth:`close` them explicitly, or to use a
733+
:keyword:`with` statement around them.
727734

728735
.. note::
729736
:meth:`close()` releases the resource associated with a connection but
@@ -829,10 +836,13 @@ correspond to Unix system calls applicable to sockets.
829836
type depends on the arguments given to :meth:`makefile`. These arguments are
830837
interpreted the same way as by the built-in :func:`open` function.
831838

832-
Closing the file object won't close the socket unless there are no remaining
833-
references to the socket. The socket must be in blocking mode; it can have
834-
a timeout, but the file object's internal buffer may end up in a inconsistent
835-
state if a timeout occurs.
839+
The socket must be in blocking mode; it can have a timeout, but the file
840+
object's internal buffer may end up in a inconsistent state if a timeout
841+
occurs.
842+
843+
Closing the file object returned by :meth:`makefile` won't close the
844+
original socket unless all other file objects have been closed and
845+
:meth:`socket.close` has been called on the socket object.
836846

837847
.. note::
838848

0 commit comments

Comments
 (0)