Skip to content

Support for try-finally. #12

Description

@jriehl

Meta doesn't support try-finally.

Expected (please note I am using a personal version of the Numba dump() utility which has not been merged upstream at time of this report):

>>> from meta.decompiler import decompile_func
>>> from numba.utils import dump
>>> def testfn():
...     try:
...         init()
...     finally:
...         deinit()
... 
>>> dump(decompile_func(testfn), False)
('FunctionDef',
 {'args': ('arguments',
           {'args': [], 'defaults': [], 'kwarg': None, 'vararg': None}),
  'body': [('TryFinally',
            {'body': [('Expr',
                       {'value': ('Call',
                                  {'args': [],
                                   'func': ('Name',
                                            {'ctx': ('Load', {}),
                                             'id': 'init'}),
                                   'keywords': [],
                                   'kwargs': None,
                                   'starargs': None})})],
             'finalbody': [('Expr',
                            {'value': ('Call',
                                       {'args': [],
                                        'func': ('Name',
                                                 {'ctx': ('Load',
                                                          {}),
                                                  'id': 'deinit'}),
                                        'keywords': [],
                                        'kwargs': None,
                                        'starargs': None})})]})],
  'decorator_list': [],
  'name': 'testfn'})

Got:

>>> dump(decompile_func(testfn), False)
('FunctionDef',
 {'args': ('arguments',
           {'args': [], 'defaults': [], 'kwarg': None, 'vararg': None}),
  'body': [('Expr',
            {'value': ('Call',
                       {'args': [],
                        'func': ('Name',
                                 {'ctx': ('Load', {}), 'id': 'init'}),
                        'keywords': [],
                        'kwargs': None,
                        'starargs': None})}),
           ('Return',
            {'value': ('Name', {'ctx': ('Load', {}), 'id': 'None'})})],
  'decorator_list': [],
  'name': 'testfn'})

Worse still, having an except clause causes meta to blow up:

>>> def testfn2():
...     try:
...         init()
...         do_something()
...     except Exception as e:
...         handle(e)
...     finally:
...         deinit()
... 
>>> dump(decompile_func(testfn2), False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/__init__.py", line 37, in decompile_func
    ast_node = make_function(code, defaults=[], lineno=code.co_firstlineno)
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/instructions.py", line 86, in make_function
    stmnts = instructions.stmnt()
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/instructions.py", line 310, in stmnt
    self.visit(instr)
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/instructions.py", line 324, in visit
    method(instr)
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/control_flow_instructions.py", line 305, in SETUP_FINALLY
    self.do_try_except_block(try_except_block)
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/control_flow_instructions.py", line 262, in do_try_except_block
    try_except = self.decompile_block(block).stmnt()
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/instructions.py", line 310, in stmnt
    self.visit(instr)
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/instructions.py", line 324, in visit
    method(instr)
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/control_flow_instructions.py", line 346, in SETUP_EXCEPT
    end, handlers = self.split_handlers(handlers_blocks)
  File "/home/jriehl/.virtualenvs/numba/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/decompiler/control_flow_instructions.py", line 204, in split_handlers
    assert except_instrs[-2].opname == 'POP_TOP'
AssertionError

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions