Skip to content

Commit

Permalink
Merge pull request #8 from akaptur/unify-block-unwinding
Browse files Browse the repository at this point in the history
Unify block unwinding
  • Loading branch information
nedbat committed Apr 5, 2014
2 parents 9b67ccc + b39560c commit 02d3359
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions byterun/pyvm2.py
Expand Up @@ -154,9 +154,18 @@ def run_code(self, code, f_globals=None, f_locals=None):
return val

def unwind_block(self, block):
while len(self.stack) > block.level:
if block.type == 'except-handler':
offset = 3
else:
offset = 0

while len(self.stack) > block.level + offset:
self.pop()

if block.type == 'except-handler':
tb, value, exctype = self.popn(3)
self.last_exception = exctype, value, tb

def run_frame(self, frame):
"""Run a frame until it returns (somehow).
Expand Down Expand Up @@ -256,11 +265,6 @@ def run_frame(self, frame):
break

self.pop_block()

if block.type == 'except-handler':
self.unwind_except_handler(block)
continue

self.unwind_block(block)

if block.type == 'loop' and why == 'break':
Expand Down Expand Up @@ -317,13 +321,7 @@ def run_frame(self, frame):

return self.return_value

def unwind_except_handler(self, block):
while len(self.stack) > block.level + 3:
self.pop()
exctype = self.pop()
value = self.pop()
tb = self.pop()
self.last_exception = exctype, value, tb


## Stack manipulation

Expand Down Expand Up @@ -722,7 +720,7 @@ def byte_END_FINALLY(self):
if why == 'silenced': # PY3
block = self.pop_block()
assert block.type == 'except-handler'
self.unwind_except_handler(block)
self.unwind_block(block)
why = None
elif v is None:
why = None
Expand Down Expand Up @@ -815,7 +813,7 @@ def byte_POP_EXCEPT(self):
block = self.pop_block()
if block.type != 'except-handler':
raise Exception("popped block is not an except handler")
self.unwind_except_handler(block)
self.unwind_block(block)

def byte_SETUP_WITH(self, dest):
ctxmgr = self.pop()
Expand Down

0 comments on commit 02d3359

Please sign in to comment.