New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context manager of socket.socket is not documented #69099
Comments
socket.socket has a context manager to automatically close the socket with the However it is not documented, unlike socket.create_connection. |
IMO the change notice for create_connection() should be moved to apply to the “socket” class, perhaps the “Socket Objects” section. bpo-9794 looks like context manager support was added specifically for create_connection(), however any socket object, no matter what function is used to create it, should be usable the same way for free. Also, we should explicitly document that exiting the context manager (“with” statement) invokes close(), since that affects how the underlying socket is closed when makefile() has been used. |
Here is my proposed patch |
socket-context.patch looks good to me. There is no need to add a NEWS entry for this. |
The patch looks good. It would also be cool if you can add a short code snippet somewhere: sock = socket.socket()
with sock:
sock.bind(('127.0.0.1', 8080))
sock.listen()
... |
I reviewed the patch.
The socket module has examples. Why not modifying these examples to promote the context manager protocol? Example: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s
# use with to ensure that the socket is closed, especially on error
with s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close() The second "with conn:" is maybe overkill. What do you think? For a client connection, usually I prefer to explicitly close the socket (even if I use "with conn:") to get exception on my ".close()" line, instead of getting an exception from the context manager, which is harder to understand. |
Thanks for the reviews. In this new patch, I modified two existing examples, but did not add any new example. Does that work for you Yury? Also modified example code for the socketserver module. Victor: IMO the “with conn” in the example is not overkill. It ensures the client connection socket is cleaned up, which is completely independent of the server listening socket s. What exceptions can you get out of conn.close()? I can only think of unusual programming errors like EBADF. I would prefer to remove close() as being redundant with the context manager. |
FWIW I discovered that socket.close() or __exit__() does not actually raise exceptions for cases like EBADF, see bpo-26685. |
New changeset d5f7980dd654 by Martin Panter in branch '3.5': New changeset 711201953505 by Martin Panter in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: