@@ -167,6 +167,7 @@ def _find_labels_and_gotos(code):
167
167
for_exits = []
168
168
excepts = []
169
169
finallies = []
170
+ last_block = None
170
171
171
172
opname0 = oparg0 = offset0 = None
172
173
opname1 = oparg1 = offset1 = None # the main one we're looking at each loop iteration
@@ -187,7 +188,7 @@ def replace_block(old_block, new_block):
187
188
188
189
def pop_block ():
189
190
if block_stack :
190
- block_stack .pop ()
191
+ return block_stack .pop ()
191
192
else :
192
193
_warn_bug ("can't pop block" )
193
194
@@ -198,14 +199,14 @@ def pop_block_of_type(type):
198
199
replace_block (block_stack [- 1 ], (type , block_stack [- 1 ][1 ]))
199
200
else :
200
201
_warn_bug ("mismatched block type" )
201
- pop_block ()
202
+ return pop_block ()
202
203
203
204
for opname4 , oparg4 , offset4 in _parse_instructions (code .co_code , 3 ):
204
205
endoffset1 = offset2
205
206
206
207
# check for special offsets
207
208
if for_exits and offset1 == for_exits [- 1 ]:
208
- pop_block ()
209
+ last_block = pop_block ()
209
210
for_exits .pop ()
210
211
if excepts and offset1 == excepts [- 1 ]:
211
212
block_counter += 1
@@ -243,14 +244,17 @@ def pop_block_of_type(type):
243
244
block_stack .append ((opname1 , block_counter ))
244
245
for_exits .append (endoffset1 + oparg1 )
245
246
elif opname1 == 'POP_BLOCK' :
246
- pop_block ()
247
+ last_block = pop_block ()
247
248
elif opname1 == 'POP_EXCEPT' :
248
- pop_block_of_type ('<EXCEPT>' )
249
+ last_block = pop_block_of_type ('<EXCEPT>' )
249
250
elif opname1 == 'END_FINALLY' :
250
251
if opname0 != 'JUMP_FORWARD' : # hack for dummy end-finally in except block (correct fix would be a jump-aware reading of instructions!)
251
- pop_block_of_type ('<FINALLY>' )
252
- elif opname1 in ('WITH_CLEANUP' , 'WITH_CLEANUP_START' ) and _BYTECODE .has_setup_with :
253
- block_stack .append (('<FINALLY>' , - 1 )) # temporary block to match END_FINALLY
252
+ last_block = pop_block_of_type ('<FINALLY>' )
253
+ elif opname1 in ('WITH_CLEANUP' , 'WITH_CLEANUP_START' ):
254
+ if _BYTECODE .has_setup_with :
255
+ block_stack .append (('<FINALLY>' , - 1 )) # temporary block to match END_FINALLY
256
+ else :
257
+ replace_block (last_block , ('SETUP_WITH' ,) + last_block [1 :]) # python 2.6 - finally was actually with
254
258
255
259
opname0 , oparg0 , offset0 = opname1 , oparg1 , offset1
256
260
opname1 , oparg1 , offset1 = opname2 , oparg2 , offset2
0 commit comments