Skip to content
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

bpo-32454: socket closefd #5048

Merged
merged 1 commit into from
Jan 27, 2018
Merged

bpo-32454: socket closefd #5048

merged 1 commit into from
Jan 27, 2018

Conversation

tiran
Copy link
Member

@tiran tiran commented Dec 30, 2017

Add close(fd) function to the socket module

Signed-off-by: Christian Heimes christian@python.org

NOTE The PR also documents the previously undocumented socket.close function.

https://bugs.python.org/issue32454

@tiran tiran added the type-feature A feature request or enhancement label Dec 30, 2017
Py_END_ALLOW_THREADS
/* bpo-30319: The peer can already have closed the connection.
Python ignores ECONNRESET on close(). */
if (res < 0 && errno != ECONNRESET) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be !CHECK_ERRNO(ECONNRESET), or use GET_SOCK_ERROR instead of errno.

Winsock is an OS library, so it can't use errno of a particular C runtime library. It uses Windows API [WSA]GetLastError and [WSA]SetLastError.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

Duplicate a socket file descriptor. This is like :func:`os.dup`, but for
sockets. On some platforms (most noticeable Windows) :func:`os.dup`
does not work for socket file descriptors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a versionadded too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket.dup() function exists since 2.6 at least. It just hasn't been documented yet.

@@ -1519,6 +1519,24 @@ def test_unusable_closed_socketio(self):
self.assertRaises(ValueError, fp.writable)
self.assertRaises(ValueError, fp.seekable)

def test_socket_close(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there already a test for dup()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping on this question.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there is no test. I don't have time to write a test case, Would you like me to drop the documentation for dup?

try:
sock.close()
except OSError:
# sock.close() fails with EBADF
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it always does, then how about a assertRaises?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertRaises in the finally block may mask an error in the try block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, Python 3 would display the exception chain.

@tiran
Copy link
Member Author

tiran commented Jan 26, 2018

@eryksun @pitrou I'd like to land this feature in 3.7.

Add close(fd) function to the socket module

Signed-off-by: Christian Heimes <christian@python.org>
@tiran
Copy link
Member Author

tiran commented Jan 26, 2018

I removed the documentation for dup(fd) from this PR.

.. function:: dup(fd)
 
   Duplicate a socket file descriptor. This is like :func:`os.dup`, but for
   sockets. On some platforms (most noticeable Windows) :func:`os.dup`
   does not work for socket file descriptors.

Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Christian!

@tiran tiran merged commit d0e31b9 into python:master Jan 27, 2018
@bedevere-bot
Copy link

@tiran: Please replace # with GH- in the commit message next time. Thanks!

@tiran tiran deleted the socket_closefd branch January 27, 2018 08:54
@tiran
Copy link
Member Author

tiran commented Jan 27, 2018

thx @pitrou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants