annotate Lib/test/bytecode_helper.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 ce8dd299cdc4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
83659
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
1 """bytecode_helper - support tools for testing correct bytecode generation"""
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
2
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
3 import unittest
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
4 import dis
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
5 import io
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
6
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
7 _UNSPECIFIED = object()
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
8
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
9 class BytecodeTestCase(unittest.TestCase):
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
10 """Custom assertion methods for inspecting bytecode."""
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
11
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
12 def get_disassembly_as_string(self, co):
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
13 s = io.StringIO()
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
14 dis.dis(co, file=s)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
15 return s.getvalue()
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
16
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
17 def assertInBytecode(self, x, opname, argval=_UNSPECIFIED):
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
18 """Returns instr if op is found, otherwise throws AssertionError"""
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
19 for instr in dis.get_instructions(x):
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
20 if instr.opname == opname:
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
21 if argval is _UNSPECIFIED or instr.argval == argval:
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
22 return instr
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
23 disassembly = self.get_disassembly_as_string(x)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
24 if argval is _UNSPECIFIED:
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
25 msg = '%s not found in bytecode:\n%s' % (opname, disassembly)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
26 else:
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
27 msg = '(%s,%r) not found in bytecode:\n%s'
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
28 msg = msg % (opname, argval, disassembly)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
29 self.fail(msg)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
30
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
31 def assertNotInBytecode(self, x, opname, argval=_UNSPECIFIED):
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
32 """Throws AssertionError if op is found"""
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
33 for instr in dis.get_instructions(x):
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
34 if instr.opname == opname:
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
35 disassembly = self.get_disassembly_as_string(co)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
36 if opargval is _UNSPECIFIED:
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
37 msg = '%s occurs in bytecode:\n%s' % (opname, disassembly)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
38 elif instr.argval == argval:
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
39 msg = '(%s,%r) occurs in bytecode:\n%s'
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
40 msg = msg % (opname, argval, disassembly)
84d1a0e32d3b Issue #11816: Add missing test helper
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
41 self.fail(msg)