Skip to content

Commit fceab5a

Browse files
committed
Merged revisions 60080-60089,60091-60093 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r60080 | andrew.kuchling | 2008-01-19 17:26:13 +0100 (Sat, 19 Jan 2008) | 2 lines Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call .handle_timeout() method when no requests are received within the timeout period. ........ r60081 | andrew.kuchling | 2008-01-19 17:34:09 +0100 (Sat, 19 Jan 2008) | 1 line Add item ........ r60082 | christian.heimes | 2008-01-19 17:39:27 +0100 (Sat, 19 Jan 2008) | 2 lines Disabled test_xmlrpc:test_404. It's causing lots of false alarms. I also disabled a test in test_ssl which requires network access to svn.python.org. This fixes a bug Skip has reported a while ago. ........ r60083 | georg.brandl | 2008-01-19 18:38:53 +0100 (Sat, 19 Jan 2008) | 2 lines Clarify thread.join() docs. python#1873. ........ r60084 | georg.brandl | 2008-01-19 19:02:46 +0100 (Sat, 19 Jan 2008) | 2 lines python#1782: don't leak in error case in PyModule_AddXxxConstant. Patch by Hrvoje Nik?\197?\161i?\196?\135. ........ r60085 | andrew.kuchling | 2008-01-19 19:08:52 +0100 (Sat, 19 Jan 2008) | 1 line Sort two names into position ........ r60086 | andrew.kuchling | 2008-01-19 19:18:41 +0100 (Sat, 19 Jan 2008) | 2 lines Patch #976880: add mmap .rfind() method, and 'end' paramter to .find(). Contributed by John Lenton. ........ r60087 | facundo.batista | 2008-01-19 19:38:19 +0100 (Sat, 19 Jan 2008) | 5 lines Fix #1693149. Now you can pass several modules separated by coma to trace.py in the same --ignore-module option. Thanks Raghuram Devarakonda. ........ r60088 | facundo.batista | 2008-01-19 19:45:46 +0100 (Sat, 19 Jan 2008) | 3 lines Comment in NEWS regarding the change in trace.py. ........ r60089 | skip.montanaro | 2008-01-19 19:47:24 +0100 (Sat, 19 Jan 2008) | 2 lines missing from r60088 checkin. ........ r60091 | andrew.kuchling | 2008-01-19 20:14:05 +0100 (Sat, 19 Jan 2008) | 1 line Add item ........ r60092 | georg.brandl | 2008-01-19 20:27:05 +0100 (Sat, 19 Jan 2008) | 4 lines Fix python#1679: "0x" was taken as a valid integer literal. Fixes the tokenizer, tokenize.py and int() to reject this. Patches by Malte Helmert. ........ r60093 | georg.brandl | 2008-01-19 20:48:19 +0100 (Sat, 19 Jan 2008) | 3 lines Fix python#1146: TextWrap vs words 1-character shorter than the width. Patch by Quentin Gallet-Gilles. ........
1 parent 2336bdd commit fceab5a

21 files changed

+301
-40
lines changed

Doc/library/mmap.rst

+13-4
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ Memory-mapped file objects support the following methods:
137137
an exception being raised.
138138

139139

140-
.. method:: mmap.find(string[, start])
140+
.. method:: mmap.find(string[, start[, end]])
141141

142-
Returns the lowest index in the object where the substring *string* is found.
143-
Returns ``-1`` on failure. *start* is the index at which the search begins, and
144-
defaults to zero.
142+
Returns the lowest index in the object where the substring *string* is found,
143+
such that *string* is contained in the range [*start*, *end*]. Optional
144+
arguments *start* and *end* are interpreted as in slice notation.
145+
Returns ``-1`` on failure.
145146

146147

147148
.. method:: mmap.flush([offset, size])
@@ -186,6 +187,14 @@ Memory-mapped file objects support the following methods:
186187
:exc:`TypeError` exception.
187188

188189

190+
.. method:: mmap.rfind(string[, start[, end]])
191+
192+
Returns the highest index in the object where the substring *string* is
193+
found, such that *string* is contained in the range [*start*,
194+
*end*]. Optional arguments *start* and *end* are interpreted as in slice
195+
notation. Returns ``-1`` on failure.
196+
197+
189198
.. method:: mmap.seek(pos[, whence])
190199

191200
Set the file's current position. *whence* argument is optional and defaults to

Doc/library/socketserver.rst

+15-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ to behave autonomously; the default is :const:`False`, meaning that Python will
4444
not exit until all threads created by :class:`ThreadingMixIn` have exited.
4545

4646
Server classes have the same external methods and attributes, no matter what
47-
network protocol they use:
47+
network protocol they use.
4848

4949

5050
Server Creation Notes
@@ -193,6 +193,13 @@ The server classes support the following class variables:
193193
The type of socket used by the server; :const:`socket.SOCK_STREAM` and
194194
:const:`socket.SOCK_DGRAM` are two possible values.
195195

196+
.. data:: timeout
197+
198+
Timeout duration, measured in seconds, or :const:`None` if no timeout is desired.
199+
If no incoming requests are received within the timeout period,
200+
the :meth:`handle_timeout` method is called and then the server resumes waiting for
201+
requests.
202+
196203
There are various server methods that can be overridden by subclasses of base
197204
server classes like :class:`TCPServer`; these methods aren't useful to external
198205
users of the server object.
@@ -220,6 +227,13 @@ users of the server object.
220227
method raises an exception. The default action is to print the traceback to
221228
standard output and continue handling further requests.
222229

230+
.. function:: handle_timeout()
231+
232+
This function is called when the :attr:`timeout` attribute has been set to a
233+
value other than :const:`None` and the timeout period has passed with no
234+
requests being received. The default action for forking servers is
235+
to collect the status of any child processes that have exited, while
236+
in threading servers this method does nothing.
223237

224238
.. function:: process_request(request, client_address)
225239

Doc/library/threading.rst

+7-6
Original file line numberDiff line numberDiff line change
@@ -615,18 +615,19 @@ impossible to detect the termination of alien threads.
615615

616616
When the *timeout* argument is present and not ``None``, it should be a floating
617617
point number specifying a timeout for the operation in seconds (or fractions
618-
thereof). As :meth:`join` always returns ``None``, you must call
619-
:meth:`isAlive` to decide whether a timeout happened.
618+
thereof). As :meth:`join` always returns ``None``, you must call :meth:`isAlive`
619+
after :meth:`join` to decide whether a timeout happened -- if the thread is
620+
still alive, the :meth:`join` call timed out.
620621

621622
When the *timeout* argument is not present or ``None``, the operation will block
622623
until the thread terminates.
623624

624625
A thread can be :meth:`join`\ ed many times.
625626

626-
:meth:`join` may throw a :exc:`RuntimeError`, if an attempt is made to join the
627-
current thread as that would cause a deadlock. It is also an error to
628-
:meth:`join` a thread before it has been started and attempts to do so raises
629-
same exception.
627+
:meth:`join` raises a :exc:`RuntimeError` if an attempt is made to join
628+
the current thread as that would cause a deadlock. It is also an error to
629+
:meth:`join` a thread before it has been started and attempts to do so
630+
raises the same exception.
630631

631632

632633
.. method:: Thread.getName()

Doc/library/trace.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ The following command-line arguments are supported:
6464
stdout for each file processed.
6565

6666
:option:`--ignore-module`
67-
Ignore the named module and its submodules (if it is a package). May be given
67+
Accepts comma separated list of module names. Ignore each of the named
68+
module and its submodules (if it is a package). May be given
6869
multiple times.
6970

7071
:option:`--ignore-dir`
71-
Ignore all modules and packages in the named directory and subdirectories. May
72-
be given multiple times.
72+
Ignore all modules and packages in the named directory and subdirectories
73+
(multiple directories can be joined by os.pathsep). May be given multiple
74+
times.
7375

7476

7577
.. _trace-api:

Doc/whatsnew/2.6.rst

+14
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,13 @@ complete list of changes, or look through the CVS logs for all the details.
960960

961961
.. Patch #1490190
962962
963+
* :class:`mmap` objects now have a :meth:`rfind` method that finds
964+
a substring, beginning at the end of the string and searching
965+
backwards. The :meth:`find` method
966+
also gained a *end* parameter containing the index at which to stop
967+
the forward search.
968+
(Contributed by John Lenton.)
969+
963970
* The :mod:`new` module has been removed from Python 3.0.
964971
Importing it therefore
965972
triggers a warning message when Python is running in 3.0-warning
@@ -1102,6 +1109,13 @@ complete list of changes, or look through the CVS logs for all the details.
11021109
(Contributed by Alberto Bertogli.)
11031110

11041111
.. Patch #1646
1112+
1113+
* The base classes in the :mod:`SocketServer` module now support
1114+
calling a :meth:`handle_timeout` method after a span of inactivity
1115+
specified by the server's :attr:`timeout` attribute. (Contributed
1116+
by Michael Pomraning.)
1117+
1118+
.. Patch #742598
11051119
11061120
* A new variable in the :mod:`sys` module,
11071121
:attr:`float_info`, is an object

Lib/SocketServer.py

+41-4
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class BaseServer:
158158
- server_bind()
159159
- server_activate()
160160
- get_request() -> request, client_address
161+
- handle_timeout()
161162
- verify_request(request, client_address)
162163
- server_close()
163164
- process_request(request, client_address)
@@ -171,6 +172,7 @@ class BaseServer:
171172
Class variables that may be overridden by derived classes or
172173
instances:
173174
175+
- timeout
174176
- address_family
175177
- socket_type
176178
- allow_reuse_address
@@ -182,6 +184,8 @@ class BaseServer:
182184
183185
"""
184186

187+
timeout = None
188+
185189
def __init__(self, server_address, RequestHandlerClass):
186190
"""Constructor. May be extended, do not override."""
187191
self.server_address = server_address
@@ -204,8 +208,9 @@ def serve_forever(self):
204208
# finishing a request is fairly arbitrary. Remember:
205209
#
206210
# - handle_request() is the top-level call. It calls
207-
# get_request(), verify_request() and process_request()
208-
# - get_request() is different for stream or datagram sockets
211+
# await_request(), verify_request() and process_request()
212+
# - get_request(), called by await_request(), is different for
213+
# stream or datagram sockets
209214
# - process_request() is the place that may fork a new process
210215
# or create a new thread to finish the request
211216
# - finish_request() instantiates the request handler class;
@@ -214,7 +219,7 @@ def serve_forever(self):
214219
def handle_request(self):
215220
"""Handle one request, possibly blocking."""
216221
try:
217-
request, client_address = self.get_request()
222+
request, client_address = self.await_request()
218223
except socket.error:
219224
return
220225
if self.verify_request(request, client_address):
@@ -224,6 +229,28 @@ def handle_request(self):
224229
self.handle_error(request, client_address)
225230
self.close_request(request)
226231

232+
def await_request(self):
233+
"""Call get_request or handle_timeout, observing self.timeout.
234+
235+
Returns value from get_request() or raises socket.timeout exception if
236+
timeout was exceeded.
237+
"""
238+
if self.timeout is not None:
239+
# If timeout == 0, you're responsible for your own fd magic.
240+
import select
241+
fd_sets = select.select([self], [], [], self.timeout)
242+
if not fd_sets[0]:
243+
self.handle_timeout()
244+
raise socket.timeout("Listening timed out")
245+
return self.get_request()
246+
247+
def handle_timeout(self):
248+
"""Called if no new request arrives within self.timeout.
249+
250+
Overridden by ForkingMixIn.
251+
"""
252+
pass
253+
227254
def verify_request(self, request, client_address):
228255
"""Verify the request. May be overridden.
229256
@@ -289,6 +316,7 @@ class TCPServer(BaseServer):
289316
- server_bind()
290317
- server_activate()
291318
- get_request() -> request, client_address
319+
- handle_timeout()
292320
- verify_request(request, client_address)
293321
- process_request(request, client_address)
294322
- close_request(request)
@@ -301,6 +329,7 @@ class TCPServer(BaseServer):
301329
Class variables that may be overridden by derived classes or
302330
instances:
303331
332+
- timeout
304333
- address_family
305334
- socket_type
306335
- request_queue_size (only for stream sockets)
@@ -405,11 +434,12 @@ class ForkingMixIn:
405434

406435
"""Mix-in class to handle each request in a new process."""
407436

437+
timeout = 300
408438
active_children = None
409439
max_children = 40
410440

411441
def collect_children(self):
412-
"""Internal routine to wait for died children."""
442+
"""Internal routine to wait for children that have exited."""
413443
while self.active_children:
414444
if len(self.active_children) < self.max_children:
415445
options = os.WNOHANG
@@ -424,6 +454,13 @@ def collect_children(self):
424454
if not pid: break
425455
self.active_children.remove(pid)
426456

457+
def handle_timeout(self):
458+
"""Wait for zombies after self.timeout seconds of inactivity.
459+
460+
May be extended, do not override.
461+
"""
462+
self.collect_children()
463+
427464
def process_request(self, request, client_address):
428465
"""Fork a new subprocess to process the request."""
429466
self.collect_children()

Lib/test/test_builtin.py

+5
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,11 @@ def test_int(self):
743743
self.assertEqual(int('0O123', 8), 83)
744744
self.assertEqual(int('0B100', 2), 4)
745745

746+
# Bug 1679: "0x" is not a valid hex literal
747+
self.assertRaises(ValueError, int, "0x", 16)
748+
self.assertRaises(ValueError, int, "0x", 0)
749+
750+
746751
# SF bug 1334662: int(string, base) wrong answers
747752
# Various representations of 2**32 evaluated to 0
748753
# rather than 2**32 in previous versions

Lib/test/test_grammar.py

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def testPlainIntegers(self):
3232
self.assertEquals(0o377, 255)
3333
self.assertEquals(2147483647, 0o17777777777)
3434
self.assertEquals(0b1001, 9)
35+
# "0x" is not a valid literal
36+
self.assertRaises(SyntaxError, eval, "0x")
3537
from sys import maxsize
3638
if maxsize == 2147483647:
3739
self.assertEquals(-2147483647-1, -0o20000000000)

Lib/test/test_mmap.py

+36
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,42 @@ def test_tougher_find(self):
252252
self.assertEqual(m.find(slice + b'x'), -1)
253253
m.close()
254254

255+
def test_find_end(self):
256+
# test the new 'end' parameter works as expected
257+
f = open(TESTFN, 'w+')
258+
data = 'one two ones'
259+
n = len(data)
260+
f.write(data)
261+
f.flush()
262+
m = mmap.mmap(f.fileno(), n)
263+
f.close()
264+
265+
self.assertEqual(m.find('one'), 0)
266+
self.assertEqual(m.find('ones'), 8)
267+
self.assertEqual(m.find('one', 0, -1), 0)
268+
self.assertEqual(m.find('one', 1), 8)
269+
self.assertEqual(m.find('one', 1, -1), 8)
270+
self.assertEqual(m.find('one', 1, -2), -1)
271+
272+
273+
def test_rfind(self):
274+
# test the new 'end' parameter works as expected
275+
f = open(TESTFN, 'w+')
276+
data = 'one two ones'
277+
n = len(data)
278+
f.write(data)
279+
f.flush()
280+
m = mmap.mmap(f.fileno(), n)
281+
f.close()
282+
283+
self.assertEqual(m.rfind('one'), 8)
284+
self.assertEqual(m.rfind('one '), 0)
285+
self.assertEqual(m.rfind('one', 0, -1), 8)
286+
self.assertEqual(m.rfind('one', 0, -2), 0)
287+
self.assertEqual(m.rfind('one', 1, -1), 8)
288+
self.assertEqual(m.rfind('one', 1, -2), -1)
289+
290+
255291
def test_double_close(self):
256292
# make sure a double close doesn't crash on Solaris (Bug# 665913)
257293
f = open(TESTFN, 'wb+')

Lib/test/test_socket.py

-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ def testHostnameRes(self):
288288

289289
def testRefCountGetNameInfo(self):
290290
# Testing reference count for getnameinfo
291-
import sys
292291
if hasattr(sys, "getrefcount"):
293292
try:
294293
# On some versions, this loses a reference

Lib/test/test_ssl.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ def handle_error(prefix):
3838

3939
class BasicTests(unittest.TestCase):
4040

41+
def testSSLconnect(self):
42+
if not test_support.is_resource_enabled('network'):
43+
return
44+
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
45+
cert_reqs=ssl.CERT_NONE)
46+
s.connect(("svn.python.org", 443))
47+
c = s.getpeercert()
48+
if c:
49+
raise test_support.TestFailed("Peer cert %s shouldn't be here!")
50+
s.close()
51+
52+
# this should fail because we have no verification certs
53+
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
54+
cert_reqs=ssl.CERT_REQUIRED)
55+
try:
56+
s.connect(("svn.python.org", 443))
57+
except ssl.SSLError:
58+
pass
59+
finally:
60+
s.close()
61+
4162
def testCrucialConstants(self):
4263
ssl.PROTOCOL_SSLv2
4364
ssl.PROTOCOL_SSLv23
@@ -81,7 +102,6 @@ def testDERtoPEM(self):
81102
class NetworkedTests(unittest.TestCase):
82103

83104
def testConnect(self):
84-
85105
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
86106
cert_reqs=ssl.CERT_NONE)
87107
s.connect(("svn.python.org", 443))

Lib/test/test_textwrap.py

+13
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,19 @@ def test_break_long(self):
385385
' o'],
386386
subsequent_indent = ' '*15)
387387

388+
# bug 1146. Prevent a long word to be wrongly wrapped when the
389+
# preceding word is exactly one character shorter than the width
390+
self.check_wrap(self.text, 12,
391+
['Did you say ',
392+
'"supercalifr',
393+
'agilisticexp',
394+
'ialidocious?',
395+
'" How *do*',
396+
'you spell',
397+
'that odd',
398+
'word,',
399+
'anyways?'])
400+
388401
def test_nobreak_long(self):
389402
# Test with break_long_words disabled
390403
self.wrapper.break_long_words = 0

Lib/test/test_xmlrpc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ def test_simple1(self):
347347
# protocol error; provide additional information in test output
348348
self.fail("%s\n%s" % (e, e.headers))
349349

350-
def test_404(self):
350+
# [ch] The test 404 is causing lots of false alarms.
351+
def XXXtest_404(self):
351352
# send POST with httplib, it should return 404 header and
352353
# 'Not Found' message.
353354
conn = httplib.HTTPConnection('localhost', PORT)

0 commit comments

Comments
 (0)