Skip to content

Nested if assertion #16

Description

@kichik

The following example results in an AssertionError.
If you remove the code in the outer "if", it works just fine.

import meta

def g():
    pass

def f(x, y):
    if x == y:
        g() # <== offender
        if x != y:
            g()

f_ast = meta.decompiler.decompile_func(f)
meta.asttools.print_ast(f_ast)

Result:

Traceback (most recent call last):
  File "nested_ifs.py", line 12, in <module>
    f_ast = meta.decompiler.decompile_func(f)
  File "C:\Python27x64\lib\site-packages\meta\decompiler\__init__.py", line 37, in decompile_func
    ast_node = make_function(code, defaults=[], lineno=code.co_firstlineno)
  File "C:\Python27x64\lib\site-packages\meta\decompiler\instructions.py", line 70, in make_function
    stmnts = instructions.stmnt()
  File "C:\Python27x64\lib\site-packages\meta\decompiler\instructions.py", line 283, in stmnt
    self.visit(instr)
  File "C:\Python27x64\lib\site-packages\meta\decompiler\instructions.py", line 297, in visit
    method(instr)
  File "C:\Python27x64\lib\site-packages\meta\decompiler\control_flow_instructions.py", line 792, in POP_JUMP_IF_FALSE
    self.make_if(instr, left, and_block)
  File "C:\Python27x64\lib\site-packages\meta\decompiler\control_flow_instructions.py", line 715, in make_if
    hi = self.process_logic(block[:idx + 1])
  File "C:\Python27x64\lib\site-packages\meta\decompiler\control_flow_instructions.py", line 654, in process_logic
    right = self.process_logic(logic_block[1:])
  File "C:\Python27x64\lib\site-packages\meta\decompiler\control_flow_instructions.py", line 675, in process_logic
    assert len(stmnts) == 1
AssertionError

It seems nested "if" is being optimized into one condition.
I think this might be the cause. It tries to produce the following, but tries to shove the outer "if" code in the condition as well.

if x == y and x != y:
    g()

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