-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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-28134: Auto-detect socket values from file descriptor #1349
Conversation
# For user code address family and type values are IntEnum members, but | ||
# for the underlying _socket.socket they're just integers. The | ||
# constructor of _socket.socket converts the given argument to an | ||
# integer automatically. | ||
if fileno is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block is not necessary but makes the code easier to understand for non-C coders
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you propose to remove it? If not, I suggest None for default values rather than −1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, while -1 falling through to socketmodule.c would trigger its default behavior, writing this here in Python would be nicer using the =None idiom.
67264b2
to
a3650c7
Compare
I like this better than my fromfd2() patch (https://bugs.python.org/issue27377). A few comments:
I think querying a file descriptor for this information could be a useful operation without tying it to socket.socket(). I'm going to create a separate pull request that adds socket.fdtype(). The unit tests you have added could check for socket.fdtype before enabling them (e.g. hasattr(socket, 'fdtype')). I have created a HAVE_FDTYPE define that should only be enabled on platforms that support the operation. |
a3650c7
to
c634ce9
Compare
@nascheme my patch has a big catch. On some platforms getsockname() does not work with sockets that are neither bound nor connected. For that reason my tests are currently failing on Windows. IIRC you were running into the same issue with |
0be56ed
to
c375e06
Compare
c375e06
to
cc4cb87
Compare
cc4cb87
to
bf90728
Compare
@@ -4083,7 +4083,7 @@ def get_high_socket_fd(self): | |||
|
|||
def close(self, fd): | |||
if WIN32: | |||
socket.socket(fileno=fd).close() | |||
socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=fd).close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use keyword arguments for all of these.
# For user code address family and type values are IntEnum members, but | ||
# for the underlying _socket.socket they're just integers. The | ||
# constructor of _socket.socket converts the given argument to an | ||
# integer automatically. | ||
if fileno is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, while -1 falling through to socketmodule.c would trigger its default behavior, writing this here in Python would be nicer using the =None idiom.
#else | ||
PyErr_SetFromErrnoWithFilename(PyExc_OSError, "type"); | ||
#endif | ||
return -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any idea if SO_TYPE or SO_PROTOCOL are ever actually undefined on any platforms? If so, it'd be nice if we could make the exception raised as a result of this contain error message text indicating that the type and protocol must be supplied on the given platform.
is this going to go in? |
I have no cycles to review it again but I will say I'd be happy if 3.7 had this functionality. When you try to write Python code that uses systemd inherited sockets, you need something like it. |
The changes may break applications that are handling socket fds wrongly. I'm going to drop a mail on python-dev first. |
533c3e6
to
883e44a
Compare
883e44a
to
e5ff880
Compare
Fix socket(fileno=fd) by auto-detecting the socket's family, type, and proto from the file descriptor. The auto-detection can be overruled by passing in family, type, and proto explicitly. Without the fix, all socket except for TCP/IP over IPv4 are basically broken: >>> s = socket.create_connection(('www.python.org', 443)) >>> s <socket.socket fd=3, family=AddressFamily.AF_INET6, type=SocketKind.SOCK_STREAM, proto=6, laddr=('2003:58:bc4a:3b00:56ee:75ff:fe47:ca7b', 59730, 0, 0), raddr=('2a04:4e42:1b::223', 443, 0, 0)> >>> socket.socket(fileno=s.fileno()) <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('2003:58:bc4a:3b00::%2550471192', 59730, 0, 2550471192), raddr=('2a04:4e42:1b:0:700c:e70b:ff7f:0%2550471192', 443, 0, 2550471192)> Signed-off-by: Christian Heimes <christian@python.org>
e5ff880
to
28b925f
Compare
3.7 deadline is at today... 2018-01-29 ~23:59 Anywhere on Earth (UTC-12:00). merge me maybe? |
Yeah, I'm pressing the big green button now... |
@tiran: Please replace |
You need to add |
What happens if |
Fix socket(fileno=fd) by auto-detecting the socket's family, type,
and proto from the file descriptor. The auto-detection can be overruled
by passing in family, type, and proto explicitly.
Without the fix, all socket except for TCP/IP over IPv4 are basically broken:
Signed-off-by: Christian Heimes christian@python.org
https://bugs.python.org/issue28134