From c58337731af5d5c9c9a3f0e7c268805e1afec4d8 Mon Sep 17 00:00:00 2001 From: Benoit Chesneau Date: Tue, 26 Nov 2019 09:53:13 +0100 Subject: [PATCH] Revert "socketfromfd: remove python 2 compatibility" This reverts commit 404a7120234e2b1119f4e8a3662c542e4d8700c8. --- gunicorn/socketfromfd.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/gunicorn/socketfromfd.py b/gunicorn/socketfromfd.py index a7f7b899..71b40d3c 100644 --- a/gunicorn/socketfromfd.py +++ b/gunicorn/socketfromfd.py @@ -95,8 +95,15 @@ def fromfd(fd, keep_fd=True): family = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_DOMAIN) typ = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_TYPE) proto = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_PROTOCOL) - s - if keep_fd: - return socket.fromfd(fd, family, typ, proto) + if sys.version_info.major == 2: + # Python 2 has no fileno argument and always duplicates the fd + sockobj = socket.fromfd(fd, family, typ, proto) + sock = socket.socket(None, None, None, _sock=sockobj) + if not keep_fd: + os.close(fd) + return sock else: - return socket.socket(family, typ, proto, fileno=fd) + if keep_fd: + return socket.fromfd(fd, family, typ, proto) + else: + return socket.socket(family, typ, proto, fileno=fd)