Skip to content

Commit 89feabc

Browse files
author
Skip Montanaro
committed
The socket module now always uses the _socketobject wrapper class, even on
platforms which have dup(2). The makefile() method is built directly on top of the socket without duplicating the file descriptor, allowing timeouts to work properly. Includes a new test case (urllibnet) which requires the network resource. Closes bug 707074.
1 parent a942b99 commit 89feabc

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

Lib/socket.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@
5959
__all__.extend(os._get_exports_list(_ssl))
6060

6161
_realsocket = socket
62-
_needwrapper = False
6362
if (sys.platform.lower().startswith("win")
6463
or (hasattr(os, 'uname') and os.uname()[0] == "BeOS")
6564
or sys.platform=="riscos"):
6665

67-
_needwrapper = True
68-
6966
if _have_ssl:
7067
_realssl = ssl
7168
def ssl(sock, keyfile=None, certfile=None):
@@ -180,8 +177,7 @@ def makefile(self, mode='r', bufsize=-1):
180177
exec _s % (_m, _m, _m, _m)
181178
del _m, _s
182179

183-
if _needwrapper:
184-
socket = SocketType = _socketobject
180+
socket = SocketType = _socketobject
185181

186182
class _fileobject(object):
187183
"""Faux file object attached to a socket object."""

Lib/test/test_urllibnet.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
import unittest
4+
from test import test_support
5+
6+
import socket
7+
import urllib2
8+
import sys
9+
10+
class URLTimeoutTest(unittest.TestCase):
11+
12+
TIMEOUT = 10.0
13+
14+
def setUp(self):
15+
socket.setdefaulttimeout(self.TIMEOUT)
16+
17+
def tearDown(self):
18+
socket.setdefaulttimeout(None)
19+
20+
def testURLread(self):
21+
f = urllib2.urlopen("http://www.python.org/")
22+
x = f.read()
23+
24+
def test_main():
25+
test_support.requires('network')
26+
27+
suite = unittest.TestSuite()
28+
suite.addTest(unittest.makeSuite(URLTimeoutTest))
29+
test_support.run_suite(suite)
30+
31+
if __name__ == "__main__":
32+
test_main()

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ Extension modules
5959
Subsumed the times() function into repeat().
6060
Added chain() and cycle().
6161

62+
- The socket module now always uses the _socketobject wrapper class, even on
63+
platforms which have dup(2). The makefile() method is built directly
64+
on top of the socket without duplicating the file descriptor, allowing
65+
timeouts to work properly.
66+
6267
Library
6368
-------
6469

0 commit comments

Comments
 (0)