You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
assignee='https://github.com/tiran'closed_at=<Date2007-11-16.01:25:50.005>created_at=<Date2007-11-03.15:24:15.366>labels= ['type-bug']
title='fromfd() and dup() for _socket on WIndows'updated_at=<Date2012-05-09.08:34:16.531>user='https://bugs.python.org/roudkerk'
I've created a clean patch and tested it. It works as promised, great
work! Somebody with more Windows Fu than me should verify it before it's
applied.
I'd like to see this applied, HOWEVER I'm confused. I don't see any
mention of fromfd in the patch except in a comment. Now, in 3.0,
fromfd() is implemented in socket.py using os.dup(); but in 2.6 i don't
see it there either. What's going in?
I am hoping that this will help bpo-1439 along as well.
Never mind. I must've searched for fromdf. Crys, I think this is good
to go into 2.6 and be merged into 3.0. Then in 3.0 we can simplify Bill
Janssen's patch further.
Neal, Thomas, what do you think about the patch? Your knowledge of the
Windows API is greater than mine. Is the duplicate_socket() function ok?
I don't want to apply a patch I don't fully understand.
+#ifdef MS_WINDOWS
+/* On Windows a socket is really a handle not an fd */
+static SOCKET
+duplicate_socket(SOCKET handle)
+{
+ HANDLE newhandle;
+
+ if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
+ GetCurrentProcess(), &newhandle,
+ 0, FALSE, DUPLICATE_SAME_ACCESS))
+ {
+ WSASetLastError(WSAEBADF);
+ return INVALID_SOCKET;
+ }
+ return (SOCKET)newhandle;
+}
+#define dup(fd) duplicate_socket(fd)
+#define SOCKETCLOSE closesocket
+#define NO_MAKEFILE /* socket handles can't be treated like file handles */
+#endif
Neal, Thomas, what do you think about the patch? Your knowledge of the
Windows API is greater than mine. Is the duplicate_socket() function ok?
I don't want to apply a patch I don't fully understand.
+#ifdef MS_WINDOWS
+/* On Windows a socket is really a handle not an fd */
+static SOCKET
+duplicate_socket(SOCKET handle)
+{
HANDLE newhandle;
if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
GetCurrentProcess(), &newhandle,
0, FALSE, DUPLICATE_SAME_ACCESS))
{
WSASetLastError(WSAEBADF);
return INVALID_SOCKET;
}
return (SOCKET)newhandle;
+}
+#define dup(fd) duplicate_socket(fd)
+#define SOCKETCLOSE closesocket
+#define NO_MAKEFILE /* socket handles can't be treated like file handles */
+#endif
Not much time, so only one comment:
Is it wise to call WSASetLastError(WSAEBADF) instead of calling GetLastError()?
I'm disappointed that this doesn't implement socket.fromfd() properly.
Perhaps fromfd() needs to be implemented in socketmodule.c?
On Windows socket.fromfd() is not possible. Socket handlers and file
descriptors are different types using different API methods. Other
examples for the discrepancy are select.select() and select.poll(). On
Windows they don't work on file.
On Windows socket.fromfd() is not possible. Socket handlers and file
descriptors are different types using different API methods. Other
examples for the discrepancy are select.select() and select.poll(). On
Windows they don't work on file.
I know, but, I was thinking of it taking the fileno() of some other
socket object as argument.
In any case I think the test should not be based on whether os.name ==
'nt' but on the presence of something in the _socket module.
Crys, did you actually run the tests for the 3.0 port of the patch? I
think accept() is broken, as it checks for _can_dup_socket and will
attempt to call os.dup().
I'm thinking that we should change the C class to not create new objects
on accept() and dup() -- instead, there should be methods _accept() and
_dup() that return new file descriptors. Then the subclass can
implement accept() and dup() properly without needing to call dup()
again, and the _base slot will no longer be needed. Tell you what, I'll
try this and submit a patch for testing on Windows later.
Currently on Windows set_error() make use of a large array which maps
socket error numbers to error messages.
This patch (against socketmodule.c from Python 2.6) removes that
array and just lets PyErr_SetExcFromWindowsErr()
generate the message by using the Win32 function FormatMessage().
It is orthogonal from the other patches discussed here.
Thanks! roudkerk's patch is submitted as revision 59004.
BTW I need to go back to the drawing board for the rest of the socket
patch here. Using dup() in makefile() doesn't work for the
ssl.SSLSocket class. Maybe the explicit reference counting is the best
we can get.
I'm curious what happened with this issue. It says closed+accepted but
it doesn't appear to be checked in. Was there a fatal problem
implementing this feature on Windows? Is it hung up on the inability
to dup SSL sockets?
I'm highly interested in deploying Python web servers using FastCGI,
SCGI, WSGI, etc on the Windows server platform. I wrote a simple
Windows fromfd() patch for Python 2.1 which was successfully used by my
organization for many years. Now we are trying to move to a newer
version of Python and still facing the need to patch this in. Just
wondering what happened with this feature and perhaps there is something
I can do to help move it along again. Many Python projects like flup,
python-scgi, etc would be able to support Windows via this feature.
(PS if this question is more appropriately raised on a mailing list
rather than the issue tracker, I apologize in advance.)
On Sun, Oct 04, 2009 at 05:39:26PM +0000, Preston Landers wrote:
I'm curious what happened with this issue. It says closed+accepted but
it doesn't appear to be checked in.
If you see the report, the last message indicates that it is checked in revision 59004.
Preston-Landers commented on Oct 19, 2009
Preston-Landersmannequin
Mannequin
More actions
Hmm... revision 59004 appears to be unrelated to the main issue at hand.
As far as I can tell now the status is this: the dup() and fromfd()
support appears to be present in Python 3000 for Windows - at least the
py3k branch. I didn't check the releases. But it's not present in 2.6
or trunk. I guess as far as I'm concerned that's fine. I'm going to use
a patch to 2.6.3 to implement it for my purposes anyway. Thanks.
ArthurKantor commented on May 9, 2012
ArthurKantormannequin
Mannequin
More actions
Hi guys
It appears, that this patch was meant to go into both the 2.x and 3.x series, but it never made into 2.x
Activity
roudkerk commentedon Nov 3, 2007
The patch adds support for _socket.fromfd() and _socket.socket.dup() on
Windows. It uses the Win32 DuplicateHandle() function.
The patch is to socketmodule.c an test_socket.py.
tiran commentedon Nov 9, 2007
I've created a clean patch and tested it. It works as promised, great
work! Somebody with more Windows Fu than me should verify it before it's
applied.
gvanrossum commentedon Nov 14, 2007
I'd like to see this applied, HOWEVER I'm confused. I don't see any
mention of fromfd in the patch except in a comment. Now, in 3.0,
fromfd() is implemented in socket.py using os.dup(); but in 2.6 i don't
see it there either. What's going in?
I am hoping that this will help bpo-1439 along as well.
gvanrossum commentedon Nov 14, 2007
Never mind. I must've searched for fromdf. Crys, I think this is good
to go into 2.6 and be merged into 3.0. Then in 3.0 we can simplify Bill
Janssen's patch further.
tiran commentedon Nov 14, 2007
Neal, Thomas, what do you think about the patch? Your knowledge of the
Windows API is greater than mine. Is the duplicate_socket() function ok?
I don't want to apply a patch I don't fully understand.
+#ifdef MS_WINDOWS
+/* On Windows a socket is really a handle not an fd */
+static SOCKET
+duplicate_socket(SOCKET handle)
+{
+ HANDLE newhandle;
+
+ if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
+ GetCurrentProcess(), &newhandle,
+ 0, FALSE, DUPLICATE_SAME_ACCESS))
+ {
+ WSASetLastError(WSAEBADF);
+ return INVALID_SOCKET;
+ }
+ return (SOCKET)newhandle;
+}
+#define dup(fd) duplicate_socket(fd)
+#define SOCKETCLOSE closesocket
+#define NO_MAKEFILE /* socket handles can't be treated like file handles */
+#endif
tiran commentedon Nov 14, 2007
Port of the socket_fromfd.patch to py3k
theller commentedon Nov 14, 2007
Christian Heimes schrieb:
Not much time, so only one comment:
Is it wise to call WSASetLastError(WSAEBADF) instead of calling GetLastError()?
Thomas
gvanrossum commentedon Nov 14, 2007
I'm disappointed that this doesn't implement socket.fromfd() properly.
Perhaps fromfd() needs to be implemented in socketmodule.c?
Also, the whole mess with the _base slot can be gotten rid of;
accept() can use the dup() method instead.
tiran commentedon Nov 14, 2007
On Windows socket.fromfd() is not possible. Socket handlers and file
descriptors are different types using different API methods. Other
examples for the discrepancy are select.select() and select.poll(). On
Windows they don't work on file.
gvanrossum commentedon Nov 14, 2007
I know, but, I was thinking of it taking the fileno() of some other
socket object as argument.
In any case I think the test should not be based on whether os.name ==
'nt' but on the presence of something in the _socket module.
gvanrossum commentedon Nov 14, 2007
Crys, did you actually run the tests for the 3.0 port of the patch? I
think accept() is broken, as it checks for _can_dup_socket and will
attempt to call os.dup().
I'm thinking that we should change the C class to not create new objects
on accept() and dup() -- instead, there should be methods _accept() and
_dup() that return new file descriptors. Then the subclass can
implement accept() and dup() properly without needing to call dup()
again, and the _base slot will no longer be needed. Tell you what, I'll
try this and submit a patch for testing on Windows later.
gvanrossum commentedon Nov 15, 2007
Here's a new version along the lines I described. Please test on
Windows. It works on Linux and OSX. What this includes:
the dup-on-windows implementation by R Oudkerk, with the
GetLastError() suggestion from Thomas Heller.
Replace the C implementations of dup() and accept() with _dup() and
_accept() that return file descriptors instead of socket objects.
Implement accept() and dup() in Python using _accept() and _dup().
Get rid of socket.fromfd(). You can use socket.socket(..., fileno=...)
instead (it doesn't dup though).
gvanrossum commentedon Nov 15, 2007
New version of socket.diff. The only change is better docstrings for
accept() and dup().
6 remaining items
gvanrossum commentedon Nov 15, 2007
I'm hoping that Bill can submit his SSL changes first.
If we make _dup() a module-level function, we can implement fromfd()
in Python. I'll do this now.
gvanrossum commentedon Nov 15, 2007
socket2.diff makes dup() a module-level function in _socket (and hence
in socket.py) and uses that to implement fromfd() in socket.py.
No changes otherwise compared to py3k_another_socket.patch.
gvanrossum commentedon Nov 15, 2007
socket3.diff builds on socket2.diff:
Rename socket class to Socket; add socket() as a factory function.
Implement roudkerk's suggestion of using a duplicate socket in
makefile() to get rid of the manual reference counting. Yay!
Note: this dinterferes with Bill Janssen's bpo-1451. Whichever is
checked in last must fix issues with the other.
gvanrossum commentedon Nov 15, 2007
Forgot to upload socket3.diff.
tiran commentedon Nov 15, 2007
I've yet another idea for a tiny improvement. Instead of doing
I prefer
It removes some confusing magic and makes error checking on Windows
slightly easier.
gvanrossum commentedon Nov 15, 2007
Hold on, socket3.diff breaks four unit tests:
test_ftplib test_poplib test_smtplib test_urllib2net
But that doesn't allow fromfd to work. I found some real use cases for
fromfd where the fd is passed in through some other means:
http://www.google.com/codesearch?as_q=socket%5C.fromfd&btnG=Search+Code&as_lang=python
roudkerk commentedon Nov 16, 2007
Currently on Windows set_error() make use of a large array which maps
socket error numbers to error messages.
This patch (against socketmodule.c from Python 2.6) removes that
array and just lets PyErr_SetExcFromWindowsErr()
generate the message by using the Win32 function FormatMessage().
It is orthogonal from the other patches discussed here.
gvanrossum commentedon Nov 16, 2007
Thanks! roudkerk's patch is submitted as revision 59004.
BTW I need to go back to the drawing board for the rest of the socket
patch here. Using dup() in makefile() doesn't work for the
ssl.SSLSocket class. Maybe the explicit reference counting is the best
we can get.
gvanrossum commentedon Nov 16, 2007
I've submitted socket2.diff plus small changes to ssl.py.
This seems the best I can do given that I don't think I can make dup()
work on ssl sockets.
Preston-Landers commentedon Oct 4, 2009
I'm curious what happened with this issue. It says closed+accepted but
it doesn't appear to be checked in. Was there a fatal problem
implementing this feature on Windows? Is it hung up on the inability
to dup SSL sockets?
I'm highly interested in deploying Python web servers using FastCGI,
SCGI, WSGI, etc on the Windows server platform. I wrote a simple
Windows fromfd() patch for Python 2.1 which was successfully used by my
organization for many years. Now we are trying to move to a newer
version of Python and still facing the need to patch this in. Just
wondering what happened with this feature and perhaps there is something
I can do to help move it along again. Many Python projects like flup,
python-scgi, etc would be able to support Windows via this feature.
(PS if this question is more appropriately raised on a mailing list
rather than the issue tracker, I apologize in advance.)
orsenthil commentedon Oct 17, 2009
On Sun, Oct 04, 2009 at 05:39:26PM +0000, Preston Landers wrote:
If you see the report, the last message indicates that it is checked in revision 59004.
Preston-Landers commentedon Oct 19, 2009
Hmm... revision 59004 appears to be unrelated to the main issue at hand.
As far as I can tell now the status is this: the dup() and fromfd()
support appears to be present in Python 3000 for Windows - at least the
py3k branch. I didn't check the releases. But it's not present in 2.6
or trunk. I guess as far as I'm concerned that's fine. I'm going to use
a patch to 2.6.3 to implement it for my purposes anyway. Thanks.
ArthurKantor commentedon May 9, 2012
Hi guys
It appears, that this patch was meant to go into both the 2.x and 3.x series, but it never made into 2.x
Are there still plans to apply it to 2.7?
(apologies if I'm asking this in the wrong forum)