[issue31739] socket.close recommended but not demonstrated in same-page example code
New submission from Nathaniel Manista <nathaniel@google.com>: 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. ---------- assignee: docs@python components: Documentation messages: 303993 nosy: Nathaniel Manista, docs@python priority: normal severity: normal status: open title: socket.close recommended but not demonstrated in same-page example code type: enhancement versions: Python 3.6, Python 3.7 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
R. David Murray <rdmurray@bitdance.com> added the comment: Which example? (It might be easiest to just generate a PR with your suggested improvement.) ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
Nathaniel Manista <nathaniel@google.com> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
R. David Murray <rdmurray@bitdance.com> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
R. David Murray <rdmurray@bitdance.com> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: Lisa, would you care to wrap this one up? ---------- assignee: docs@python -> lisroach nosy: +lisroach, rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
Change by Roundup Robot <devnull@psf.upfronthosting.co.za>: ---------- keywords: +patch pull_requests: +4152 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
Adam Mitchell <adam0500@me.com> added the comment: I submitted a pull request, #4181, to fix this issue. I am now waiting for my contributor agreement to be approved. ---------- nosy: +AdamMitchell _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
Change by Todd Rovito <rovitotv@rovitotv.org>: ---------- nosy: +Todd.Rovito _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue31739> _______________________________________
participants (6)
-
Adam Mitchell
-
Nathaniel Manista
-
R. David Murray
-
Raymond Hettinger
-
Roundup Robot
-
Todd Rovito