This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket.close recommended but not demonstrated in same-page example code
Type: enhancement Stage: patch review
Components: Documentation Versions: Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: lisroach Nosy List: AdamMitchell, Nathaniel Manista, Todd.Rovito, docs@python, lisroach, r.david.murray, rhettinger
Priority: normal Keywords: patch

Created on 2017-10-09 19:27 by Nathaniel Manista, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 4181 open python-dev, 2017-10-30 23:05
Messages (7)
msg303993 - (view) Author: Nathaniel Manista (Nathaniel Manista) Date: 2017-10-09 19:27
https://docs.python.org/3.7/library/socket.html#socket.socket.close says "it is recommended to close() [sockets] explicitly, or to use a with statement around them", but in the example code on the same page at https://docs.python.org/3.7/library/socket.html#example correct use of the .close() method seems to be missing.
msg303995 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-09 19:32
Which example?  (It might be easiest to just generate a PR with your suggested improvement.)
msg304514 - (view) Author: Nathaniel Manista (Nathaniel Manista) Date: 2017-10-17 19:03
As I am learning from the examples, I don't have the confidence to propose a fix to them. :-P

For the IPv4-and-IPv6 "Echo server program": should s be closed at some point after "conn, addr = s.accept()"? Should s be closed on the line immediately after "conn, addr = s.accept()", should it be closed on the last line (after the "with conn:" context is exited), or is either acceptable?

Should the "very simple network sniffer with raw sockets on Windows" have a with statement or "s.close()"? Should the "communicate to a CAN network using the raw socket protocol" example have a with statement or "s.close()"?

Of course it's possible that I'm misunderstanding one or more things.
msg304552 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-18 02:05
I think in the echo examples the 'with conn' block should be indented and a
'with s:' added around it.

The network sniffer should probably use a with statement with the
created socket.

The CAN example ends only on ctrl-C, and could go into a fast spin
loop on error, so it probably needs a more extensive rewrite.
msg304553 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-18 02:12
Heh, of course the socket server also only ends with ctl-C.  And I misread the CAN example, it won't spin because the read isn't wrapped in a try/except.  So yes, that should use a with on the socket as well, since the with will close the socket on a ctl-C, unlike a close statement after the loop which will never be reached.
msg304766 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-10-22 21:28
Lisa, would you care to wrap this one up?
msg305259 - (view) Author: Adam Mitchell (AdamMitchell) * Date: 2017-10-30 23:26
I submitted a pull request, #4181, to fix this issue. I am now waiting for my contributor agreement to be approved.
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75920
2017-10-30 23:28:15Todd.Rovitosetnosy: + Todd.Rovito
2017-10-30 23:26:09AdamMitchellsetnosy: + AdamMitchell
messages: + msg305259
2017-10-30 23:05:47python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request4152
2017-10-22 21:28:01rhettingersetassignee: docs@python -> lisroach

messages: + msg304766
nosy: + lisroach, rhettinger
2017-10-18 02:12:55r.david.murraysetmessages: + msg304553
2017-10-18 02:05:13r.david.murraysetmessages: + msg304552
2017-10-17 19:03:04Nathaniel Manistasetmessages: + msg304514
2017-10-09 19:32:36r.david.murraysetnosy: + r.david.murray
messages: + msg303995
2017-10-09 19:27:58Nathaniel Manistacreate