CensoredUsername / unrpyc

A ren'py script decompiler
Other
864 stars 157 forks source link

Indentation of some Python code is wrong #32

Closed jackmcbarn closed 9 years ago

jackmcbarn commented 9 years ago

Minimal test case:

init:
    python:
        foo = [
            'bar'
        ]

When compiled, this is what the relevant part of the dump looks like:

[<renpy.ast.Init 
    .block = [<renpy.ast.Python 
        .code = <renpy.ast.PyCode 
            .source = u"""
foo = [
            'bar'
        ]
"""

        >
    >]
>]

When decompiled, this is the result:

init:
    python:
        foo = [
                    'bar'
                ]
CensoredUsername commented 9 years ago

This seems to be caused by ren'py's logical line behaviour. While it is possible to detect that such changes have occured, correcting for it during decompilation is hard since the original indentation level has been lost (this cannot be derived from the current decompiler indentation level since that might be different from how it was in the original code, due to things like the screen "has" statement

The only relatively clean way to clean this up would be to first parse the python to detect logical lines properly, then per logical line, if there's a newline in one, detect the minimum indentation in the trailing lines, and strip that from all the trailing lines.

CensoredUsername commented 9 years ago

In the end it turned out better to just don't bother with the line breaks in logical lines, this way the result will always be the same through a decompile-recompile cycle.