Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_opcodes.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 | 3c9232a684ae |
| children |
| rev | line source |
|---|---|
| 653 | 1 # Python test set -- part 2, opcodes |
| 2 | |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43102
diff
changeset
|
3 from test.support import run_unittest |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
4 import unittest |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
5 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
6 class OpcodeTest(unittest.TestCase): |
| 653 | 7 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
8 def test_try_inside_for_loop(self): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
9 n = 0 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
10 for i in range(10): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
11 n = n+i |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
12 try: 1/0 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
13 except NameError: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
14 except ZeroDivisionError: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
15 except TypeError: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
16 try: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
17 except: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
18 try: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
19 finally: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
20 n = n+i |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
21 if n != 90: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
22 self.fail('try inside for') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
23 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
24 def test_raise_class_exceptions(self): |
| 653 | 25 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
26 class AClass(Exception): pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
27 class BClass(AClass): pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
28 class CClass(Exception): pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
29 class DClass(AClass): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
30 def __init__(self, ignore): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
31 pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
32 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
33 try: raise AClass() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
34 except: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
35 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
36 try: raise AClass() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
37 except AClass: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
38 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
39 try: raise BClass() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
40 except AClass: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
41 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
42 try: raise BClass() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
43 except CClass: self.fail() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
44 except: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
45 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
46 a = AClass() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
47 b = BClass() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
48 |
|
43102
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
49 try: |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
50 raise b |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
51 except AClass as v: |
|
43102
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
52 self.assertEqual(v, b) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
53 else: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
54 self.fail("no exception") |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
55 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
56 # not enough arguments |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
57 ##try: raise BClass, a |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
58 ##except TypeError: pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
59 ##else: self.fail("no exception") |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
60 |
|
43102
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
61 try: raise DClass(a) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
62 except DClass as v: |
|
58473
71acd4482e65
Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
63 self.assertIsInstance(v, DClass) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
64 else: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
65 self.fail("no exception") |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
66 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
67 def test_compare_function_objects(self): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
68 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
69 f = eval('lambda: None') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
70 g = eval('lambda: None') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
71 self.assertNotEqual(f, g) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
72 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
73 f = eval('lambda a: a') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
74 g = eval('lambda a: a') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
75 self.assertNotEqual(f, g) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
76 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
77 f = eval('lambda a=1: a') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
78 g = eval('lambda a=1: a') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
79 self.assertNotEqual(f, g) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
80 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
81 f = eval('lambda: 0') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
82 g = eval('lambda: 1') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
83 self.assertNotEqual(f, g) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
84 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
85 f = eval('lambda: None') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
86 g = eval('lambda a: None') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
87 self.assertNotEqual(f, g) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
88 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
89 f = eval('lambda a: None') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
90 g = eval('lambda b: None') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
91 self.assertNotEqual(f, g) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
92 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
93 f = eval('lambda a: None') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
94 g = eval('lambda a=None: None') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
95 self.assertNotEqual(f, g) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
96 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
97 f = eval('lambda a=0: None') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
98 g = eval('lambda a=1: None') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
99 self.assertNotEqual(f, g) |
| 2302 | 100 |
|
51872
3722ce458318
Merged revisions 69811,69947 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47278
diff
changeset
|
101 def test_modulo_of_string_subclasses(self): |
|
3722ce458318
Merged revisions 69811,69947 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47278
diff
changeset
|
102 class MyString(str): |
|
3722ce458318
Merged revisions 69811,69947 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47278
diff
changeset
|
103 def __mod__(self, value): |
|
3722ce458318
Merged revisions 69811,69947 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47278
diff
changeset
|
104 return 42 |
|
3722ce458318
Merged revisions 69811,69947 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47278
diff
changeset
|
105 self.assertEqual(MyString() % 3, 42) |
|
3722ce458318
Merged revisions 69811,69947 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47278
diff
changeset
|
106 |
| 2302 | 107 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
108 def test_main(): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
109 run_unittest(OpcodeTest) |
|
5946
9a56452c7e8e
Add test for function comparisons
Guido van Rossum <guido@python.org>
parents:
2302
diff
changeset
|
110 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
111 if __name__ == '__main__': |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36988
diff
changeset
|
112 test_main() |
