annotate Lib/test/test_structseq.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 89e405e6a7a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
62877
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
1 import os
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
2 import time
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
3 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 44674
diff changeset
4 from test import support
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
5
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
7 class StructSeqTest(unittest.TestCase):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
8
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
9 def test_tuple(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
10 t = time.gmtime()
62878
0e782a6e6bb7 use assert method
Benjamin Peterson <benjamin@python.org>
parents: 62877
diff changeset
11 self.assertIsInstance(t, tuple)
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
12 astuple = tuple(t)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
13 self.assertEqual(len(t), len(astuple))
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
14 self.assertEqual(t, astuple)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
15
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
16 # Check that slicing works the same way; at one point, slicing t[i:j] with
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
17 # 0 < i < j could produce NULLs in the result.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 30247
diff changeset
18 for i in range(-len(t), len(t)):
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
19 self.assertEqual(t[i:], astuple[i:])
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 30247
diff changeset
20 for j in range(-len(t), len(t)):
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
21 self.assertEqual(t[i:j], astuple[i:j])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
22
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 30247
diff changeset
23 for j in range(-len(t), len(t)):
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
24 self.assertEqual(t[:j], astuple[:j])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
25
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
26 self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
27 self.assertRaises(IndexError, t.__getitem__, len(t))
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 30247
diff changeset
28 for i in range(-len(t), len(t)-1):
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
29 self.assertEqual(t[i], astuple[i])
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
30
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
31 def test_repr(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
32 t = time.gmtime()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
33 self.assertTrue(repr(t))
44674
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
34 t = time.gmtime(0)
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
35 self.assertEqual(repr(t),
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
36 "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
1154158a199f Merged revisions 59933-59951 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
37 "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
62877
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
38 # os.stat() gives a complicated struct sequence.
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
39 st = os.stat(__file__)
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
40 rep = repr(st)
86218
89e405e6a7a9 Issue #19209: fix structseq test
Christian Heimes <christian@cheimes.de>
parents: 77423
diff changeset
41 self.assertTrue(rep.startswith("os.stat_result"))
62877
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
42 self.assertIn("st_mode=", rep)
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
43 self.assertIn("st_ino=", rep)
e4bda4a520ca fix repr of complicated structseqs #9206
Benjamin Peterson <benjamin@python.org>
parents: 62876
diff changeset
44 self.assertIn("st_dev=", rep)
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
45
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
46 def test_concat(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
47 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
48 t2 = t1 + tuple(t1)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 30247
diff changeset
49 for i in range(len(t1)):
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
50 self.assertEqual(t2[i], t2[i+len(t1)])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
51
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
52 def test_repeat(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
53 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
54 t2 = 3 * t1
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 30247
diff changeset
55 for i in range(len(t1)):
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
56 self.assertEqual(t2[i], t2[i+len(t1)])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
57 self.assertEqual(t2[i], t2[i+2*len(t1)])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
58
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
59 def test_contains(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
60 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
61 for item in t1:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
62 self.assertIn(item, t1)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
63 self.assertNotIn(-42, t1)
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
64
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
65 def test_hash(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
66 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
67 self.assertEqual(hash(t1), hash(tuple(t1)))
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
68
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
69 def test_cmp(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
70 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
71 t2 = type(t1)(t1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
72 self.assertEqual(t1, t2)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
73 self.assertTrue(not (t1 < t2))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
74 self.assertTrue(t1 <= t2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
75 self.assertTrue(not (t1 > t2))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
76 self.assertTrue(t1 >= t2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
77 self.assertTrue(not (t1 != t2))
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
78
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
79 def test_fields(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
80 t = time.gmtime()
77423
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62905
diff changeset
81 self.assertEqual(len(t), t.n_sequence_fields)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62905
diff changeset
82 self.assertEqual(t.n_unnamed_fields, 0)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62905
diff changeset
83 self.assertEqual(t.n_fields, time._STRUCT_TM_ITEMS)
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
84
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
85 def test_constructor(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
86 t = time.struct_time
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
87
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
88 self.assertRaises(TypeError, t)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
89 self.assertRaises(TypeError, t, None)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
90 self.assertRaises(TypeError, t, "123")
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
91 self.assertRaises(TypeError, t, "123", dict={})
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
92 self.assertRaises(TypeError, t, "123456789", dict=None)
22163
42862169541f backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 20781
diff changeset
93
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
94 s = "123456789"
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
95 self.assertEqual("".join(t(s)), s)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
96
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
97 def test_eviltuple(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
98 class Exc(Exception):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
99 pass
22163
42862169541f backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 20781
diff changeset
100
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
101 # Devious code could crash structseqs' contructors
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
102 class C:
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
103 def __getitem__(self, i):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
104 raise Exc
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
105 def __len__(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
106 return 9
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
107
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
108 self.assertRaises(Exc, time.struct_time, C())
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
109
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
110 def test_reduce(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
111 t = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
112 x = t.__reduce__()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
113
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
114 def test_extended_getslice(self):
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
115 # Test extended slicing by comparing with list slicing.
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
116 t = time.gmtime()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
117 L = list(t)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
118 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
119 for start in indices:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
120 for stop in indices:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
121 # Skip step 0 (invalid)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
122 for step in indices[1:]:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
123 self.assertEqual(list(t[start:stop:step]),
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
124 L[start:stop:step])
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 41642
diff changeset
125
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
126 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 44674
diff changeset
127 support.run_unittest(StructSeqTest)
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
128
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
129 if __name__ == "__main__":
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
130 test_main()