Description
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.