Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Lib/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
EAGAIN = getattr(errno, 'EAGAIN', 11)
EWOULDBLOCK = getattr(errno, 'EWOULDBLOCK', 11)

_SOCK_NONBLOCK = getattr(_socket, 'SOCK_NONBLOCK', 0)

__all__ = ["fromfd", "getfqdn", "create_connection",
"AddressFamily", "SocketKind"]
__all__.extend(os._get_exports_list(_socket))
Expand Down Expand Up @@ -203,10 +205,12 @@ def accept(self):
For IP sockets, the address info is a pair (hostaddr, port).
"""
fd, addr = self._accept()
# If our type has the SOCK_NONBLOCK flag, we shouldn't pass it onto the
# new socket. We do not currently allow passing SOCK_NONBLOCK to
# accept4, so the returned socket is always blocking.
type = self.type & ~globals().get("SOCK_NONBLOCK", 0)
type = self.type
if _SOCK_NONBLOCK:
Copy link
Copy Markdown
Contributor

@asvetlov asvetlov Dec 14, 2017

Choose a reason for hiding this comment

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

Either _SOCK_NONBLOCK should be None if not present in socket -- then if _SOCK_NONBLOCK is not None: will be very fast check.
Or drop if: type & ~0 works pretty well

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with Andrew, I don't think that the if is needed.

# If our type has the SOCK_NONBLOCK flag, we shouldn't pass it onto
# the new socket. We do not currently allow passing SOCK_NONBLOCK
# to accept4, so the returned socket is always blocking.
type = type & ~_SOCK_NONBLOCK
sock = socket(self.family, type, self.proto, fileno=fd)
# Issue #7995: if no default timeout is set and the listening
# socket had a (non-zero) timeout, force the new socket in blocking
Expand Down