66import sys
77import dis
88import io
9+ import re
910
1011class _C :
1112 def __init__ (self , x ):
@@ -89,26 +90,25 @@ def bug1333982(x=[]):
8990
9091dis_bug1333982 = """\
9192 %-4d 0 LOAD_CONST 1 (0)
92- 3 JUMP_IF_TRUE 33 (to 39)
93- 6 POP_TOP
94- 7 LOAD_GLOBAL 0 (AssertionError)
95- 10 BUILD_LIST 0
96- 13 LOAD_FAST 0 (x)
97- 16 GET_ITER
98- >> 17 FOR_ITER 12 (to 32)
99- 20 STORE_FAST 1 (s)
100- 23 LOAD_FAST 1 (s)
101- 26 LIST_APPEND 2
102- 29 JUMP_ABSOLUTE 17
103-
104- %-4d >> 32 LOAD_CONST 2 (1)
105- 35 BINARY_ADD
106- 36 RAISE_VARARGS 2
107- >> 39 POP_TOP
108-
109- %-4d 40 LOAD_CONST 0 (None)
110- 43 RETURN_VALUE
93+ 3 POP_JUMP_IF_TRUE 35
94+ 6 LOAD_GLOBAL 0 (AssertionError)
95+ 9 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
96+ 12 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
97+ 15 MAKE_FUNCTION 0
98+ 18 LOAD_FAST 0 (x)
99+ 21 GET_ITER
100+ 22 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
101+
102+ %-4d 25 LOAD_CONST 4 (1)
103+ 28 BINARY_ADD
104+ 29 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
105+ 32 RAISE_VARARGS 1
106+
107+ %-4d >> 35 LOAD_CONST 0 (None)
108+ 38 RETURN_VALUE
111109""" % (bug1333982 .__code__ .co_firstlineno + 1 ,
110+ __file__ ,
111+ bug1333982 .__code__ .co_firstlineno + 1 ,
112112 bug1333982 .__code__ .co_firstlineno + 2 ,
113113 bug1333982 .__code__ .co_firstlineno + 3 )
114114
@@ -193,8 +193,13 @@ def get_disassemble_as_string(self, func, lasti=-1):
193193 def do_disassembly_test (self , func , expected ):
194194 lines = self .get_disassembly (func )
195195 expected = expected .splitlines ()
196- if expected != lines :
197- self .fail (
196+ if expected == lines :
197+ return
198+ else :
199+ lines = [re .sub ('0x[0-9A-Fa-f]+' , '0x...' , l ) for l in lines ]
200+ if expected == lines :
201+ return
202+ self .fail (
198203 "events did not match expectation:\n " +
199204 "\n " .join (difflib .ndiff (expected ,
200205 lines )))
@@ -217,18 +222,13 @@ def test_dis(self):
217222 def test_bug_708901 (self ):
218223 self .do_disassembly_test (bug708901 , dis_bug708901 )
219224
220- # Test has been disabled due to change in the way
221- # list comps are handled. The byte code now includes
222- # a memory address and a file location, so they change from
223- # run to run.
224- @unittest .skip ('disabled due to a change in the way list comps are handled' )
225225 def test_bug_1333982 (self ):
226- # XXX: re-enable this test!
227226 # This one is checking bytecodes generated for an `assert` statement,
228227 # so fails if the tests are run with -O. Skip this test then.
228+ if not __debug__ :
229+ self .skipTest ('need asserts, run without -O' )
229230
230- if __debug__ :
231- self .do_disassembly_test (bug1333982 , dis_bug1333982 )
231+ self .do_disassembly_test (bug1333982 , dis_bug1333982 )
232232
233233 def test_big_linenos (self ):
234234 def func (count ):
@@ -451,8 +451,5 @@ def test_pretty_flags_no_flags(self):
451451 self .assertEqual (dis .pretty_flags (0 ), '0x0' )
452452
453453
454- def test_main ():
455- run_unittest (DisTests , CodeInfoTests )
456-
457454if __name__ == "__main__" :
458- test_main ()
455+ unittest . main ()
0 commit comments