Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 380f7f2

Browse files
committed
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines Speed up this test by about 99%. Remove sleeps and replace with events. (This may fail on some slow platforms, but we can fix those cases which should be relatively isolated and easier to find now.) Move two test cases that didn't require a server to be started to a separate TestCase. These tests were taking 3 seconds which is what the timeout was set to. ........ r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line The contains function raised a gcc warning. The new code is copied straight from py3k. ........ r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines Add a timing flag to Trace so you can see where slowness occurs like waiting for socket timeouts in test_smtplib :-). ........ r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too. ........ r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines #2194: fix some typos. ........ r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line Add itertools.combinations(). ........ r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line One too many decrefs. ........ r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line Larger test range ........ r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line Simply the sample code for combinations(). ........
1 parent 1041f74 commit 380f7f2

11 files changed

Lines changed: 510 additions & 245 deletions

File tree

Doc/library/itertools.rst

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,20 @@ loops that truncate the stream.
9595

9696
def combinations(iterable, r):
9797
pool = tuple(iterable)
98-
if pool:
99-
n = len(pool)
100-
vec = range(r)
101-
yield tuple(pool[i] for i in vec)
102-
while 1:
103-
for i in reversed(range(r)):
104-
if vec[i] == i + n-r:
105-
continue
106-
vec[i] += 1
107-
for j in range(i+1, r):
108-
vec[j] = vec[j-1] + 1
109-
yield tuple(pool[i] for i in vec)
98+
n = len(pool)
99+
assert 0 <= r <= n
100+
vec = range(r)
101+
yield tuple(pool[i] for i in vec)
102+
while 1:
103+
for i in reversed(range(r)):
104+
if vec[i] != i + n - r:
110105
break
111-
else:
112-
return
106+
else:
107+
return
108+
vec[i] += 1
109+
for j in range(i+1, r):
110+
vec[j] = vec[j-1] + 1
111+
yield tuple(pool[i] for i in vec)
113112

114113
.. versionadded:: 2.6
115114

Doc/library/trace.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Programming Interface
8080
---------------------
8181

8282

83-
.. class:: Trace([count=1[, trace=1[, countfuncs=0[, countcallers=0[, ignoremods=()[, ignoredirs=()[, infile=None[, outfile=None]]]]]]]])
83+
.. class:: Trace([count=1[, trace=1[, countfuncs=0[, countcallers=0[, ignoremods=()[, ignoredirs=()[, infile=None[, outfile=None[, timing=False]]]]]]]]])
8484

8585
Create an object to trace execution of a single statement or expression. All
8686
parameters are optional. *count* enables counting of line numbers. *trace*
@@ -89,7 +89,8 @@ Programming Interface
8989
*ignoremods* is a list of modules or packages to ignore. *ignoredirs* is a list
9090
of directories whose modules or packages should be ignored. *infile* is the
9191
file from which to read stored count information. *outfile* is a file in which
92-
to write updated count information.
92+
to write updated count information. *timing* enables a timestamp relative
93+
to when tracing was started to be displayed.
9394

9495

9596
.. method:: Trace.run(cmd)

Doc/using/unix.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package on all others. However there are certain features you might want to use
2020
that are not available on your distro's package. You can easily compile the
2121
latest version of Python from source.
2222

23-
In the event Python doesn't come preinstalled and isn't in the repositories as
23+
In the event that Python doesn't come preinstalled and isn't in the repositories as
2424
well, you can easily make packages for your own distro. Have a look at the
2525
following links:
2626

Doc/using/windows.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ has compiled Windows installers (MSI packages) with every `release
2121
<http://www.python.org/download/releases/>`_ for many years.
2222

2323
With ongoing development of Python, some platforms that used to be supported
24-
earlier are not longer supported (due to the lack of users or developers).
24+
earlier are no longer supported (due to the lack of users or developers).
2525
Check :pep:`11` for details on all unsupported platforms.
2626

2727
* DOS and Windows 3.x are deprecated since Python 2.0 and code specific to these

Lib/test/test_getargs2.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from test import test_support
3+
from _testcapi import getargs_keywords
34

45
import warnings
56
warnings.filterwarnings("ignore",
@@ -248,9 +249,57 @@ def __getitem__(self, n):
248249
raise ValueError
249250
self.assertRaises(TypeError, getargs_tuple, 1, seq())
250251

252+
class Keywords_TestCase(unittest.TestCase):
253+
def test_positional_args(self):
254+
# using all positional args
255+
self.assertEquals(
256+
getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10),
257+
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
258+
)
259+
def test_mixed_args(self):
260+
# positional and keyword args
261+
self.assertEquals(
262+
getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10),
263+
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
264+
)
265+
def test_keyword_args(self):
266+
# all keywords
267+
self.assertEquals(
268+
getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10),
269+
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
270+
)
271+
def test_optional_args(self):
272+
# missing optional keyword args, skipping tuples
273+
self.assertEquals(
274+
getargs_keywords(arg1=(1,2), arg2=3, arg5=10),
275+
(1, 2, 3, -1, -1, -1, -1, -1, -1, 10)
276+
)
277+
def test_required_args(self):
278+
# required arg missing
279+
try:
280+
getargs_keywords(arg1=(1,2))
281+
except TypeError as err:
282+
self.assertEquals(str(err), "Required argument 'arg2' (pos 2) not found")
283+
else:
284+
self.fail('TypeError should have been raised')
285+
def test_too_many_args(self):
286+
try:
287+
getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111)
288+
except TypeError as err:
289+
self.assertEquals(str(err), "function takes at most 5 arguments (6 given)")
290+
else:
291+
self.fail('TypeError should have been raised')
292+
def test_invalid_keyword(self):
293+
# extraneous keyword arg
294+
try:
295+
getargs_keywords((1,2),3,arg5=10,arg666=666)
296+
except TypeError as err:
297+
self.assertEquals(str(err), "'arg666' is an invalid keyword argument for this function")
298+
else:
299+
self.fail('TypeError should have been raised')
251300

252301
def test_main():
253-
tests = [Signed_TestCase, Unsigned_TestCase, Tuple_TestCase]
302+
tests = [Signed_TestCase, Unsigned_TestCase, Tuple_TestCase, Keywords_TestCase]
254303
try:
255304
from _testcapi import getargs_L, getargs_K
256305
except ImportError:

Lib/test/test_itertools.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def take(n, seq):
4444
'Convenience function for partially consuming a long of infinite iterable'
4545
return list(islice(seq, n))
4646

47+
def fact(n):
48+
'Factorial'
49+
return reduce(operator.mul, range(1, n+1), 1)
50+
4751
class TestBasicOps(unittest.TestCase):
4852
def test_chain(self):
4953
self.assertEqual(list(chain('abc', 'def')), list('abcdef'))
@@ -52,6 +56,26 @@ def test_chain(self):
5256
self.assertEqual(take(4, chain('abc', 'def')), list('abcd'))
5357
self.assertRaises(TypeError, chain, 2, 3)
5458

59+
def test_combinations(self):
60+
self.assertRaises(TypeError, combinations, 'abc') # missing r argument
61+
self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments
62+
self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative
63+
self.assertRaises(ValueError, combinations, 'abc', 32) # r is too big
64+
self.assertEqual(list(combinations(range(4), 3)),
65+
[(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
66+
for n in range(8):
67+
values = [5*x-12 for x in range(n)]
68+
for r in range(n+1):
69+
result = list(combinations(values, r))
70+
self.assertEqual(len(result), fact(n) / fact(r) / fact(n-r)) # right number of combs
71+
self.assertEqual(len(result), len(set(result))) # no repeats
72+
self.assertEqual(result, sorted(result)) # lexicographic order
73+
for c in result:
74+
self.assertEqual(len(c), r) # r-length combinations
75+
self.assertEqual(len(set(c)), r) # no duplicate elements
76+
self.assertEqual(list(c), sorted(c)) # keep original ordering
77+
self.assert_(all(e in values for e in c)) # elements taken from input iterable
78+
5579
def test_count(self):
5680
self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
5781
self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])

Lib/test/test_smtplib.py

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
PORT = None
1919

2020
def server(evt, buf):
21+
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22+
serv.settimeout(1)
23+
serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
24+
serv.bind(("", 0))
25+
global PORT
26+
PORT = serv.getsockname()[1]
27+
serv.listen(5)
28+
evt.set()
2129
try:
22-
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23-
serv.settimeout(3)
24-
serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
25-
serv.bind(("", 0))
26-
global PORT
27-
PORT = serv.getsockname()[1]
28-
serv.listen(5)
2930
conn, addr = serv.accept()
3031
except socket.timeout:
3132
pass
@@ -38,7 +39,6 @@ def server(evt, buf):
3839
buf = buf[sent:]
3940

4041
n -= 1
41-
time.sleep(0.01)
4242

4343
conn.close()
4444
finally:
@@ -52,16 +52,8 @@ def setUp(self):
5252
self.evt = threading.Event()
5353
servargs = (self.evt, b"220 Hola mundo\n")
5454
threading.Thread(target=server, args=servargs).start()
55-
56-
# wait until server thread has assigned a port number
57-
n = 500
58-
while PORT is None and n > 0:
59-
time.sleep(0.01)
60-
n -= 1
61-
62-
# wait a little longer (sometimes connections are refused
63-
# on slow machines without this additional wait)
64-
time.sleep(0.5)
55+
self.evt.wait()
56+
self.evt.clear()
6557

6658
def tearDown(self):
6759
self.evt.wait()
@@ -76,29 +68,12 @@ def testBasic2(self):
7668
smtp = smtplib.SMTP("%s:%s" % (HOST, PORT))
7769
smtp.sock.close()
7870

79-
def testNotConnected(self):
80-
# Test various operations on an unconnected SMTP object that
81-
# should raise exceptions (at present the attempt in SMTP.send
82-
# to reference the nonexistent 'sock' attribute of the SMTP object
83-
# causes an AttributeError)
84-
smtp = smtplib.SMTP()
85-
self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
86-
self.assertRaises(smtplib.SMTPServerDisconnected,
87-
smtp.send, 'test msg')
88-
8971
def testLocalHostName(self):
9072
# check that supplied local_hostname is used
9173
smtp = smtplib.SMTP(HOST, PORT, local_hostname="testhost")
9274
self.assertEqual(smtp.local_hostname, "testhost")
9375
smtp.sock.close()
9476

95-
def testNonnumericPort(self):
96-
# check that non-numeric port raises socket.error
97-
self.assertRaises(socket.error, smtplib.SMTP,
98-
"localhost", "bogus")
99-
self.assertRaises(socket.error, smtplib.SMTP,
100-
"localhost:bogus")
101-
10277
def testTimeoutDefault(self):
10378
# default
10479
smtp = smtplib.SMTP(HOST, PORT)
@@ -128,6 +103,7 @@ def debugging_server(server_class, serv_evt, client_evt):
128103
serv = server_class(("", 0), ('nowhere', -1))
129104
global PORT
130105
PORT = serv.getsockname()[1]
106+
serv_evt.set()
131107

132108
try:
133109
if hasattr(select, 'poll'):
@@ -150,12 +126,12 @@ def debugging_server(server_class, serv_evt, client_evt):
150126
except socket.timeout:
151127
pass
152128
finally:
153-
# allow some time for the client to read the result
154-
time.sleep(0.5)
155-
serv.close()
129+
if not client_evt.isSet():
130+
# allow some time for the client to read the result
131+
time.sleep(0.5)
132+
serv.close()
156133
asyncore.close_all()
157134
PORT = None
158-
time.sleep(0.5)
159135
serv_evt.set()
160136

161137
MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
@@ -181,14 +157,8 @@ def setUp(self):
181157
threading.Thread(target=debugging_server, args=serv_args).start()
182158

183159
# wait until server thread has assigned a port number
184-
n = 500
185-
while PORT is None and n > 0:
186-
time.sleep(0.01)
187-
n -= 1
188-
189-
# wait a little longer (sometimes connections are refused
190-
# on slow machines without this additional wait)
191-
time.sleep(0.5)
160+
self.serv_evt.wait()
161+
self.serv_evt.clear()
192162

193163
def tearDown(self):
194164
# indicate that the client is finished
@@ -258,6 +228,26 @@ def testSend(self):
258228
self.assertEqual(self.output.getvalue(), mexpect)
259229

260230

231+
class NonConnectingTests(TestCase):
232+
233+
def testNotConnected(self):
234+
# Test various operations on an unconnected SMTP object that
235+
# should raise exceptions (at present the attempt in SMTP.send
236+
# to reference the nonexistent 'sock' attribute of the SMTP object
237+
# causes an AttributeError)
238+
smtp = smtplib.SMTP()
239+
self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
240+
self.assertRaises(smtplib.SMTPServerDisconnected,
241+
smtp.send, 'test msg')
242+
243+
def testNonnumericPort(self):
244+
# check that non-numeric port raises socket.error
245+
self.assertRaises(socket.error, smtplib.SMTP,
246+
"localhost", "bogus")
247+
self.assertRaises(socket.error, smtplib.SMTP,
248+
"localhost:bogus")
249+
250+
261251
# test response of client to a non-successful HELO message
262252
class BadHELOServerTests(TestCase):
263253

@@ -269,16 +259,8 @@ def setUp(self):
269259
self.evt = threading.Event()
270260
servargs = (self.evt, b"199 no hello for you!\n")
271261
threading.Thread(target=server, args=servargs).start()
272-
273-
# wait until server thread has assigned a port number
274-
n = 500
275-
while PORT is None and n > 0:
276-
time.sleep(0.01)
277-
n -= 1
278-
279-
# wait a little longer (sometimes connections are refused
280-
# on slow machines without this additional wait)
281-
time.sleep(0.5)
262+
self.evt.wait()
263+
self.evt.clear()
282264

283265
def tearDown(self):
284266
self.evt.wait()
@@ -355,14 +337,8 @@ def setUp(self):
355337
threading.Thread(target=debugging_server, args=serv_args).start()
356338

357339
# wait until server thread has assigned a port number
358-
n = 500
359-
while PORT is None and n > 0:
360-
time.sleep(0.01)
361-
n -= 1
362-
363-
# wait a little longer (sometimes connections are refused
364-
# on slow machines without this additional wait)
365-
time.sleep(0.5)
340+
self.serv_evt.wait()
341+
self.serv_evt.clear()
366342

367343
def tearDown(self):
368344
# indicate that the client is finished
@@ -430,6 +406,7 @@ def testEXPN(self):
430406

431407
def test_main(verbose=None):
432408
test_support.run_unittest(GeneralTests, DebuggingServerTests,
409+
NonConnectingTests,
433410
BadHELOServerTests, SMTPSimTests)
434411

435412
if __name__ == '__main__':

0 commit comments

Comments
 (0)