Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List comprehensions inside of a lambda print only [ #492

Open
JorianWoltjer opened this issue Mar 19, 2024 · 4 comments
Open

List comprehensions inside of a lambda print only [ #492

JorianWoltjer opened this issue Mar 19, 2024 · 4 comments
Labels
Python 3.8 volunteer wanted Volunteer wanted to fix if a bug or to implement if a new feature. Works on decompyle3

Comments

@JorianWoltjer
Copy link

JorianWoltjer commented Mar 19, 2024

Description

List comprehensions inside of a lambda print a single [ bracket instead of the list comprehension, resulting in invalid syntax.

How to Reproduce

The following is a simple list comprehension in a lambda function:

# fmt:off
a = lambda n: [i + 1 for i in range(n)]

Stored in a file called poc.py, it can be compiled into a .pyc file with the following commands:

python3 -c 'import py_compile; print(py_compile.compile("poc.py"))'

uncompyle6 __pycache__/poc.cpython-38.pyc 

It decompiles to the following code on the latest 3.9.1 version:

# Size of source mod 2**32: 50 bytes
a = lambda n: [

# okay decompiling __pycache__/poc.cpython-38.pyc

This happens while a regular function using def containing a list comprehension does work perfectly:

def a(n):
    return [i + 1 for i in range(n)]

Expected behavior

Write the whole list comprehension just like a function using def would. This would be the expected output for the example above:

a = lambda n: [i + 1 for i in range(n)]

Environment

$ uncompyle6 --version
uncompyle6, version 3.9.1
$ pydisasm --version
pydisasm, version 6.1.0
$ python3 -c "import sys; print(sys.version)"
3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0]
$ cat /etc/issue
Ubuntu 20.04.6 LTS

Workarounds

Ignore the missing list comprehension code in the output, because it continues to print the code after it. This won't make the code inside the list comprehension visible though.

@rocky
Copy link
Owner

rocky commented Mar 19, 2024

This is a bug in the print action routines (probably uncompyle6/semantics/gencomp.py) that take the AST produced and format and print the results. You can see the trees that get created by adding the -T option to uncompyle6.

A like kind of thing that happens is that the wrong child of a node is looked at in the generation routine.

This works in decompyle3, so one fix would be to backport code from there here.

Are you up for fixing this bug?

@rocky rocky added the volunteer wanted Volunteer wanted to fix if a bug or to implement if a new feature. label Mar 19, 2024
@JorianWoltjer
Copy link
Author

Thanks for the comment, I had not seen decompyle3 before and it does seem to work there! I'll try switching to that repository as I am working on Python 3.8.

I'm personally not up to the task of fixing it, especially now that I have a clean workaround, but maybe a backport can happen one day indeed.

@mosadegh22
Copy link

One of the problems when decompile in list Comprehension and result is :
parents = (lambda .0: [ Nodes.getParent(x) for x in .0 ])(nodes)
and In some cases, it outputs an empty file
In one case, a 60KB file became 120MB.had repeated
in python 3.7 and decompyle6

@rocky
Copy link
Owner

rocky commented Mar 27, 2024

@mosadegh22 file a proper bug report in decompyle3 and I might investigate when I am able.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Python 3.8 volunteer wanted Volunteer wanted to fix if a bug or to implement if a new feature. Works on decompyle3
Projects
None yet
Development

No branches or pull requests

3 participants