Compare commits

..

1 Commits

Author SHA1 Message Date
rocky
5b738cdbe1 Not working: sync with decompyle3 2024-03-02 04:22:32 -05:00
57 changed files with 513 additions and 1229 deletions

View File

@@ -43,9 +43,9 @@ jobs:
- run:
command: | # Use pip to install dependengcies
sudo pip install --user --upgrade setuptools
# Until the next release
sudo pip install git+https://github.com/rocky/python-xdis#egg=xdis
pip install --user -e .
# Not sure why "pip install -e" doesn't work above
# pip install click spark-parser xdis
pip install --user -r requirements-dev.txt
# Save dependency cache

2
.github/FUNDING.yml vendored
View File

@@ -6,7 +6,7 @@ open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: rocky
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

View File

@@ -22,9 +22,9 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
# Until the next xdis release
pip install git+https://github.com/rocky/python-xdis#egg=xdis
pip install -e .
# Not sure why "pip install -e" doesn't work above
# pip install click spark-parser xdis
pip install -r requirements-dev.txt
- name: Test uncompyle6
run: |

View File

@@ -21,8 +21,9 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
# Until the next xdis release
pip install git+https://github.com/rocky/python-xdis#egg=xdis
pip install -e .
# pip install click spark-parser xdis
pip install -r requirements-dev.txt
- name: Test uncompyle6
run: |

View File

@@ -22,9 +22,9 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
# Until the next xdis release
pip install git+https://github.com/rocky/python-xdis#egg=xdis
pip install -e .
# Not sure why "pip install -e" doesn't work above
# pip install click spark-parser xdis
pip install -r requirements-dev.txt
- name: Test uncompyle6
run: |

1
.gitignore vendored
View File

@@ -17,7 +17,6 @@
/dist
/how-to-make-a-release.txt
/nose-*.egg
/pycharm-venv
/tmp
/uncompyle6.egg-info
/unpyc

20
NEWS.md
View File

@@ -1,23 +1,3 @@
3.9.1: 2024-05-15
=================
Lots of changes major changes. track xdis API has changes.
Separate Phases more clearly:
* disassembly
* tokenization
* parsing
* abstracting to AST (more is done in newer projects)
* printing
Although we do not decompile bytecode greater than 3.8, code supports running from up to 3.12.
Many bugs fixed.
A lot of Linting and coding style modernization.
Work done in preparation for Blackhat Asia 2024
3.9.0: 2022-12-22
=================

359
PKG-INFO
View File

@@ -1,355 +1,10 @@
Metadata-Version: 1.1
Metadata-Version: 2.0
Name: uncompyle6
Version: 3.9.1
Summary: Python cross-version byte-code decompiler
Home-page: https://github.com/rocky/python-uncompyle6/
Author: Rocky Bernstein, Hartmut Goebel, John Aycock, and others
Version: 2.0.1
Summary: Python byte-code to source-code converter
Home-page: http://github.com/rocky/python-uncompyle6
Author: Rocky
Author-email: rb@dustyfeet.com
License: GPL3
Description: |buildstatus| |Pypi Installs| |Latest Version| |Supported Python Versions|
|packagestatus|
.. contents::
uncompyle6
==========
A native Python cross-version decompiler and fragment decompiler.
The successor to decompyle, uncompyle, and uncompyle2.
Introduction
------------
*uncompyle6* translates Python bytecode back into equivalent Python
source code. It accepts bytecodes from Python version 1.0 to version
3.8, spanning over 24 years of Python releases. We include Dropbox's
Python 2.5 bytecode and some PyPy bytecodes.
Why this?
---------
Ok, I'll say it: this software is amazing. It is more than your
normal hacky decompiler. Using compiler_ technology, the program
creates a parse tree of the program from the instructions; nodes at
the upper levels that look a little like what might come from a Python
AST. So we can really classify and understand what's going on in
sections of Python bytecode.
Building on this, another thing that makes this different from other
CPython bytecode decompilers is the ability to deparse just
*fragments* of source code and give source-code information around a
given bytecode offset.
I use the tree fragments to deparse fragments of code *at run time*
inside my trepan_ debuggers_. For that, bytecode offsets are recorded
and associated with fragments of the source code. This purpose,
although compatible with the original intention, is yet a little bit
different. See this_ for more information.
Python fragment deparsing given an instruction offset is useful in
showing stack traces and can be incorporated into any program that
wants to show a location in more detail than just a line number at
runtime. This code can be also used when source-code information does
not exist and there is just bytecode. Again, my debuggers make use of
this.
There were (and still are) a number of decompyle, uncompyle,
uncompyle2, uncompyle3 forks around. Many of them come basically from
the same code base, and (almost?) all of them are no longer actively
maintained. One was really good at decompiling Python 1.5-2.3, another
really good at Python 2.7, but that only. Another handles Python 3.2
only; another patched that and handled only 3.3. You get the
idea. This code pulls all of these forks together and *moves
forward*. There is some serious refactoring and cleanup in this code
base over those old forks. Even more experimental refactoring is going
on in decompyle3_.
This demonstrably does the best in decompiling Python across all
Python versions. And even when there is another project that only
provides decompilation for subset of Python versions, we generally do
demonstrably better for those as well.
How can we tell? By taking Python bytecode that comes distributed with
that version of Python and decompiling these. Among those that
successfully decompile, we can then make sure the resulting programs
are syntactically correct by running the Python interpreter for that
bytecode version. Finally, in cases where the program has a test for
itself, we can run the check on the decompiled code.
We use an automated processes to find bugs. In the issue trackers for
other decompilers, you will find a number of bugs we've found along
the way. Very few to none of them are fixed in the other decompilers.
Requirements
------------
The code in the git repository can be run from Python 2.4 to the
latest Python version, with the exception of Python 3.0 through
3.2. Volunteers are welcome to address these deficiencies if there a
desire to do so.
The way it does this though is by segregating consecutive Python versions into
git branches:
master
Python 3.6 and up (uses type annotations)
python-3.3-to-3.5
Python 3.3 through 3.5 (Generic Python 3)
python-2.4
Python 2.4 through 2.7 (Generic Python 2)
PyPy 3-2.4 and later works as well.
The bytecode files it can read have been tested on Python
bytecodes from versions 1.4, 2.1-2.7, and 3.0-3.8 and later PyPy
versions.
Installation
------------
You can install from PyPI using the name ``uncompyle6``::
pip install uncompyle6
To install from source code, this project uses setup.py, so it follows the standard Python routine::
$ pip install -e . # set up to run from source tree
or::
$ python setup.py install # may need sudo
A GNU Makefile is also provided so :code:`make install` (possibly as root or
sudo) will do the steps above.
Running Tests
-------------
::
make check
A GNU makefile has been added to smooth over setting running the right
command, and running tests from fastest to slowest.
If you have remake_ installed, you can see the list of all tasks
including tests via :code:`remake --tasks`
Usage
-----
Run
::
$ uncompyle6 *compiled-python-file-pyc-or-pyo*
For usage help:
::
$ uncompyle6 -h
Verification
------------
In older versions of Python it was possible to verify bytecode by
decompiling bytecode, and then compiling using the Python interpreter
for that bytecode version. Having done this, the bytecode produced
could be compared with the original bytecode. However as Python's code
generation got better, this no longer was feasible.
If you want Python syntax verification of the correctness of the
decompilation process, add the :code:`--syntax-verify` option. However since
Python syntax changes, you should use this option if the bytecode is
the right bytecode for the Python interpreter that will be checking
the syntax.
You can also cross compare the results with another version of
`uncompyle6` since there are sometimes regressions in decompiling
specific bytecode as the overall quality improves.
For Python 3.7 and 3.8, the code in decompyle3_ is generally
better.
Or try specific another python decompiler like uncompyle2_, unpyc37_,
or pycdc_. Since the later two work differently, bugs here often
aren't in that, and vice versa.
There is an interesting class of these programs that is readily
available give stronger verification: those programs that when run
test themselves. Our test suite includes these.
And Python comes with another a set of programs like this: its test
suite for the standard library. We have some code in :code:`test/stdlib` to
facilitate this kind of checking too.
Known Bugs/Restrictions
-----------------------
The biggest known and possibly fixable (but hard) problem has to do
with handling control flow. (Python has probably the most diverse and
screwy set of compound statements I've ever seen; there
are "else" clauses on loops and try blocks that I suspect many
programmers don't know about.)
All of the Python decompilers that I have looked at have problems
decompiling Python's control flow. In some cases we can detect an
erroneous decompilation and report that.
Python support is pretty good for Python 2
On the lower end of Python versions, decompilation seems pretty good although
we don't have any automated testing in place for Python's distributed tests.
Also, we don't have a Python interpreter for versions 1.6, and 2.0.
In the Python 3 series, Python support is strongest around 3.4 or
3.3 and drops off as you move further away from those versions. Python
3.0 is weird in that it in some ways resembles 2.6 more than it does
3.1 or 2.7. Python 3.6 changes things drastically by using word codes
rather than byte codes. As a result, the jump offset field in a jump
instruction argument has been reduced. This makes the :code:`EXTENDED_ARG`
instructions are now more prevalent in jump instruction; previously
they had been rare. Perhaps to compensate for the additional
:code:`EXTENDED_ARG` instructions, additional jump optimization has been
added. So in sum handling control flow by ad hoc means as is currently
done is worse.
Between Python 3.5, 3.6, 3.7 there have been major changes to the
:code:`MAKE_FUNCTION` and :code:`CALL_FUNCTION` instructions.
Python 3.8 removes :code:`SETUP_LOOP`, :code:`SETUP_EXCEPT`,
:code:`BREAK_LOOP`, and :code:`CONTINUE_LOOP`, instructions which may
make control-flow detection harder, lacking the more sophisticated
control-flow analysis that is planned. We'll see.
Currently not all Python magic numbers are supported. Specifically in
some versions of Python, notably Python 3.6, the magic number has
changes several times within a version.
**We support only released versions, not candidate versions.** Note
however that the magic of a released version is usually the same as
the *last* candidate version prior to release.
There are also customized Python interpreters, notably Dropbox,
which use their own magic and encrypt bytecode. With the exception of
the Dropbox's old Python 2.5 interpreter this kind of thing is not
handled.
We also don't handle PJOrion_ or otherwise obfuscated code. For
PJOrion try: PJOrion Deobfuscator_ to unscramble the bytecode to get
valid bytecode before trying this tool; pydecipher_ might help with that.
This program can't decompile Microsoft Windows EXE files created by
Py2EXE_, although we can probably decompile the code after you extract
the bytecode properly. `Pydeinstaller <https://github.com/charles-dyfis-net/pydeinstaller>`_ may help with unpacking Pyinstaller bundlers.
Handling pathologically long lists of expressions or statements is
slow. We don't handle Cython_ or MicroPython which don't use bytecode.
There are numerous bugs in decompilation. And that's true for every
other CPython decompiler I have encountered, even the ones that
claimed to be "perfect" on some particular version like 2.4.
As Python progresses decompilation also gets harder because the
compilation is more sophisticated and the language itself is more
sophisticated. I suspect that attempts there will be fewer ad-hoc
attempts like unpyc37_ (which is based on a 3.3 decompiler) simply
because it is harder to do so. The good news, at least from my
standpoint, is that I think I understand what's needed to address the
problems in a more robust way. But right now until such time as
project is better funded, I do not intend to make any serious effort
to support Python versions 3.8 or 3.9, including bugs that might come
in. I imagine at some point I may be interested in it.
You can easily find bugs by running the tests against the standard
test suite that Python uses to check itself. At any given time, there are
dozens of known problems that are pretty well isolated and that could
be solved if one were to put in the time to do so. The problem is that
there aren't that many people who have been working on bug fixing.
Some of the bugs in 3.7 and 3.8 are simply a matter of back-porting
the fixes in decompyle3. Volunteers are welcome to do so.
You may run across a bug, that you want to report. Please do so after
reading `How to report a bug
<https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md>`_ and
follow the `instructions when opening an issue <https://github.com/rocky/python-uncompyle6/issues/new?assignees=&labels=&template=bug-report.md>`_.
Be aware that it might not get my attention for a while. If you
sponsor or support the project in some way, I'll prioritize your
issues above the queue of other things I might be doing instead.
See Also
--------
* https://github.com/rocky/python-decompile3 : Much smaller and more modern code, focusing on 3.7 and 3.8. Changes in that will get migrated back here.
* https://code.google.com/archive/p/unpyc3/ : supports Python 3.2 only. The above projects use a different decompiling technique than what is used here. Currently unmaintained.
* https://github.com/figment/unpyc3/ : fork of above, but supports Python 3.3 only. Includes some fixes like supporting function annotations. Currently unmaintained.
* https://github.com/wibiti/uncompyle2 : supports Python 2.7 only, but does that fairly well. There are situations where :code:`uncompyle6` results are incorrect while :code:`uncompyle2` results are not, but more often uncompyle6 is correct when uncompyle2 is not. Because :code:`uncompyle6` adheres to accuracy over idiomatic Python, :code:`uncompyle2` can produce more natural-looking code when it is correct. Currently :code:`uncompyle2` is lightly maintained. See its issue `tracker <https://github.com/wibiti/uncompyle2/issues>`_ for more details.
* `How to report a bug <https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md>`_
* The HISTORY_ file.
* https://github.com/rocky/python-xdis : Cross Python version disassembler
* https://github.com/rocky/python-xasm : Cross Python version assembler
* https://github.com/rocky/python-uncompyle6/wiki : Wiki Documents which describe the code and aspects of it in more detail
* https://github.com/zrax/pycdc : The README for this C++ code says it aims to support all versions of Python. You can aim your slign shot for the moon too, but I doubt you are going to hit it. This code is best for Python versions around 2.7 and 3.3 when the code was initially developed. Accuracy for current versions of Python3 and early versions of Python is lacking. Without major effort, it is unlikely it can be made to support current Python 3. See its `issue tracker <https://github.com/zrax/pycdc/issues>`_ for details. Currently lightly maintained.
.. _Cython: https://en.wikipedia.org/wiki/Cython
.. _trepan: https://pypi.python.org/pypi/trepan3k
.. _compiler: https://github.com/rocky/python-uncompyle6/wiki/How-does-this-code-work%3F
.. _HISTORY: https://github.com/rocky/python-uncompyle6/blob/master/HISTORY.md
.. _report_bug: https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md
.. _debuggers: https://pypi.python.org/pypi/trepan3k
.. _remake: https://bashdb.sf.net/remake
.. _pycdc: https://github.com/zrax/pycdc
.. _decompyle3: https://github.com/rocky/python-decompile3
.. _uncompyle2: https://github.com/wibiti/uncompyle2
.. _unpyc37: https://github.com/andrew-tavera/unpyc37
.. _this: https://github.com/rocky/python-uncompyle6/wiki/Deparsing-technology-and-its-use-in-exact-location-reporting
.. |buildstatus| image:: https://travis-ci.org/rocky/python-uncompyle6.svg
:target: https://travis-ci.org/rocky/python-uncompyle6
.. |packagestatus| image:: https://repology.org/badge/vertical-allrepos/python:uncompyle6.svg
:target: https://repology.org/project/python:uncompyle6/versions
.. _PJOrion: http://www.koreanrandom.com/forum/topic/15280-pjorion-%D1%80%D0%B5%D0%B4%D0%B0%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5-%D0%BA%D0%BE%D0%BC%D0%BF%D0%B8%D0%BB%D1%8F%D1%86%D0%B8%D1%8F-%D0%B4%D0%B5%D0%BA%D0%BE%D0%BC%D0%BF%D0%B8%D0%BB%D1%8F%D1%86%D0%B8%D1%8F-%D0%BE%D0%B1%D1%84
.. _pydecipher: https://github.com/mitre/pydecipher
.. _Deobfuscator: https://github.com/extremecoders-re/PjOrion-Deobfuscator
.. _Py2EXE: https://en.wikipedia.org/wiki/Py2exe
.. |Supported Python Versions| image:: https://img.shields.io/pypi/pyversions/uncompyle6.svg
.. |Latest Version| image:: https://badge.fury.io/py/uncompyle6.svg
:target: https://badge.fury.io/py/uncompyle6
.. |Pypi Installs| image:: https://pepy.tech/badge/uncompyle6/month
License: MIT
Description: UNKNOWN
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.4
Classifier: Programming Language :: Python :: 2.5
Classifier: Programming Language :: Python :: 2.6
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.0
Classifier: Programming Language :: Python :: 3.1
Classifier: Programming Language :: Python :: 3.2
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: Software Development :: Libraries :: Python Modules

View File

@@ -62,8 +62,6 @@ classifiers = [
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Debuggers",
"Topic :: Software Development :: Libraries :: Python Modules",

View File

@@ -1,31 +0,0 @@
#!/bin/bash
# Run tests over all Python versions in branch python-3.0-3.2
set -e
function finish {
cd $owd
}
owd=$(pwd)
trap finish EXIT
cd $(dirname ${BASH_SOURCE[0]})
if ! source ./pyenv-3.0-3.2-versions ; then
exit $?
fi
if ! source ./setup-python-3.0.sh ; then
exit $?
fi
cd ..
for version in $PYVERSIONS; do
echo --- $version ---
if ! pyenv local $version ; then
exit $?
fi
make clean && python setup.py develop
if ! make check ; then
exit $?
fi
echo === $version ===
done
finish

View File

@@ -3,9 +3,9 @@ PACKAGE=uncompyle6
# FIXME put some of the below in a common routine
function finish {
cd $make_dist_uncompyle6_owd
cd $owd
}
make_dist_uncompyle6_owd=$(pwd)
owd=$(pwd)
trap finish EXIT
cd $(dirname ${BASH_SOURCE[0]})
@@ -21,11 +21,6 @@ source $PACKAGE/version.py
echo $__version__
for pyversion in $PYVERSIONS; do
echo --- $pyversion ---
if [[ ${pyversion:0:4} == "pypy" ]] ; then
echo "$pyversion - PyPy does not get special packaging"
continue
fi
if ! pyenv local $pyversion ; then
exit $?
fi
@@ -46,4 +41,3 @@ tarball=dist/${PACKAGE}-${__version_}_-tar.gz
if [[ -f $tarball ]]; then
rm -v dist/${PACKAGE}-${__version__}-tar.gz
fi
finish

View File

@@ -1,49 +0,0 @@
#!/bin/bash
PACKAGE=uncompyle6
# FIXME put some of the below in a common routine
function finish {
cd $uncompyle6_30_make_dist_owd
}
cd $(dirname ${BASH_SOURCE[0]})
uncompyle6_30_make_dist_owd=$(pwd)
trap finish EXIT
if ! source ./pyenv-3.0-3.2-versions ; then
exit $?
fi
if ! source ./setup-python-3.0.sh ; then
exit $?
fi
cd ..
source $PACKAGE/version.py
echo $__version__
for pyversion in $PYVERSIONS; do
echo --- $pyversion ---
if [[ ${pyversion:0:4} == "pypy" ]] ; then
echo "$pyversion - PyPy does not get special packaging"
continue
fi
if ! pyenv local $pyversion ; then
exit $?
fi
# pip bdist_egg create too-general wheels. So
# we narrow that by moving the generated wheel.
# Pick out first two number of version, e.g. 3.5.1 -> 35
first_two=$(echo $pyversion | cut -d'.' -f 1-2 | sed -e 's/\.//')
rm -fr build
python setup.py bdist_egg bdist_wheel
mv -v dist/${PACKAGE}-$__version__-{py2.py3,py$first_two}-none-any.whl
echo === $pyversion ===
done
python ./setup.py sdist
tarball=dist/${PACKAGE}-${__version__}.tar.gz
if [[ -f $tarball ]]; then
mv -v $tarball dist/${PACKAGE}_31-${__version__}.tar.gz
fi
finish

View File

@@ -3,11 +3,11 @@ PACKAGE=uncompyle6
# FIXME put some of the below in a common routine
function finish {
cd $uncompyle6_33_make_owd
cd $owd
}
cd $(dirname ${BASH_SOURCE[0]})
uncompyle6_33_make_owd=$(pwd)
owd=$(pwd)
trap finish EXIT
if ! source ./pyenv-3.3-3.5-versions ; then
@@ -22,11 +22,6 @@ source $PACKAGE/version.py
echo $__version__
for pyversion in $PYVERSIONS; do
echo --- $pyversion ---
if [[ ${pyversion:0:4} == "pypy" ]] ; then
echo "$pyversion - PyPy does not get special packaging"
continue
fi
if ! pyenv local $pyversion ; then
exit $?
fi
@@ -38,12 +33,6 @@ for pyversion in $PYVERSIONS; do
rm -fr build
python setup.py bdist_egg bdist_wheel
mv -v dist/${PACKAGE}-$__version__-{py2.py3,py$first_two}-none-any.whl
echo === $pyversion ===
done
python ./setup.py sdist
tarball=dist/${PACKAGE}-${__version__}.tar.gz
if [[ -f $tarball ]]; then
mv -v $tarball dist/${PACKAGE}_31-${__version__}.tar.gz
fi
finish

View File

@@ -3,11 +3,11 @@ PACKAGE=uncompyle6
# FIXME put some of the below in a common routine
function finish {
cd $make_uncompyle6_newest_owd
cd $owd
}
cd $(dirname ${BASH_SOURCE[0]})
make_uncompyle6_newest_owd=$(pwd)
owd=$(pwd)
trap finish EXIT
if ! source ./pyenv-newest-versions ; then
@@ -22,11 +22,6 @@ source $PACKAGE/version.py
echo $__version__
for pyversion in $PYVERSIONS; do
echo --- $pyversion ---
if [[ ${pyversion:0:4} == "pypy" ]] ; then
echo "$pyversion - PyPy does not get special packaging"
continue
fi
if ! pyenv local $pyversion ; then
exit $?
fi
@@ -41,4 +36,3 @@ for pyversion in $PYVERSIONS; do
done
python ./setup.py sdist
finish

View File

@@ -1,7 +1,7 @@
#/bin/bash
uncompyle6_merge_24_owd=$(pwd)
owd=$(pwd)
cd $(dirname ${BASH_SOURCE[0]})
if . ./setup-python-2.4.sh; then
git merge python-3.0-to-3.2
fi
cd $uncompyle6_merge_24_owd
cd $owd

View File

@@ -1,7 +1,7 @@
#/bin/bash
uncompyle6_merge_30_owd=$(pwd)
owd=$(pwd)
cd $(dirname ${BASH_SOURCE[0]})
if . ./setup-python-3.0.sh; then
git merge python-3.3-to-3.5
fi
cd $uncompyle6_merge_30_owd
cd $owd

View File

@@ -1,7 +1,7 @@
#/bin/bash
uncompyle6_merge_33_owd=$(pwd)
owd=$(pwd)
cd $(dirname ${BASH_SOURCE[0]})
if . ./setup-python-3.3.sh; then
git merge master
fi
cd $uncompyle6_merge_33_owd
cd $owd

View File

@@ -1,64 +0,0 @@
[build-system]
requires = [
"setuptools>=61.2",
]
build-backend = "setuptools.build_meta"
[project]
authors = [
{name = "Rocky Bernstein", email = "rb@dustyfeet.com"},
]
name = "uncompyle6"
description = "Python cross-version byte-code library and disassembler"
dependencies = [
"click",
"spark-parser >= 1.8.9, < 1.9.0",
"xdis >= 6.0.8, < 6.2.0",
]
readme = "README.rst"
license = {text = "GPL"}
keywords = ["Python bytecode", "bytecode", "disassembler"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Topic :: Software Development :: Libraries :: Python Modules",
"Programming Language :: Python :: 2.4",
"Programming Language :: Python :: 2.5",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.0",
"Programming Language :: Python :: 3.1",
"Programming Language :: Python :: 3.2",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dynamic = ["version"]
[project.urls]
Homepage = "https://github.com/rocky/python-uncompyle6"
Downloads = "https://github.com/rocky/python-uncompyle6/releases"
[project.optional-dependencies]
dev = [
"pre-commit",
"pytest",
]
[project.scripts]
uncompyle6 = "uncompyle6.bin.uncompile:main_bin"
uncompyle6-tokenize = "uncompyle6.bin.pydisassemble:main"
[tool.setuptools.dynamic]
version = {attr = "uncompyle6.version.__version__"}

View File

@@ -1,71 +0,0 @@
#!/usr/bin/env python
import sys
import setuptools
"""Setup script for the 'uncompyle6' distribution."""
SYS_VERSION = sys.version_info[0:2]
if SYS_VERSION < (3, 6):
mess = "Python Release 3.6 .. 3.12 are supported in this code branch."
if (2, 4) <= SYS_VERSION <= (2, 7):
mess += (
"\nFor your Python, version %s, use the python-2.4 code/branch."
% sys.version[0:3]
)
if SYS_VERSION >= (3, 6):
mess += (
"\nFor your Python, version %s, use the master code/branch."
% sys.version[0:3]
)
if (3, 0) >= SYS_VERSION < (3, 3):
mess += (
"\nFor your Python, version %s, use the python-3.0-to-3.2 code/branch."
% sys.version[0:3]
)
if (3, 3) >= SYS_VERSION < (3, 6):
mess += (
"\nFor your Python, version %s, use the python-3.3-to-3.5 code/branch."
% sys.version[0:3]
)
elif SYS_VERSION < (2, 4):
mess += (
"\nThis package is not supported for Python version %s." % sys.version[0:3]
)
print(mess)
raise Exception(mess)
from __pkginfo__ import (
__version__,
author,
author_email,
classifiers,
entry_points,
install_requires,
license,
long_description,
modname,
py_modules,
short_desc,
web,
zip_safe,
)
setuptools.setup(
author=author,
author_email=author_email,
classifiers=classifiers,
description=short_desc,
entry_points=entry_points,
install_requires=install_requires,
license=license,
long_description=long_description,
long_description_content_type="text/x-rst",
name=modname,
packages=setuptools.find_packages(),
py_modules=py_modules,
test_suite="nose.collector",
url=web,
version=__version__,
zip_safe=zip_safe,
)

View File

@@ -12,11 +12,10 @@ doc_files = README.rst
# examples/
[bdist_wheel]
universal = no
universal=1
[metadata]
description_file = README.rst
licences_files = COPYING
[flake8]
# max-line-length setting: NO we do not want everyone writing 120-character lines!

View File

@@ -1,6 +1,61 @@
#!/usr/bin/env python
"""Setup script for the 'xdis' distribution."""
import setuptools
import sys
from setuptools import setup
"""Setup script for the 'uncompyle6' distribution."""
setup(packages=["uncompyle6"])
SYS_VERSION = sys.version_info[0:2]
if not ((2, 4) <= SYS_VERSION < (3, 13)):
mess = "Python Release 2.6 .. 3.12 are supported in this code branch."
if (2, 4) <= SYS_VERSION <= (2, 7):
mess += (
"\nFor your Python, version %s, use the python-2.4 code/branch."
% sys.version[0:3]
)
if (3, 3) <= SYS_VERSION < (3, 6):
mess += (
"\nFor your Python, version %s, use the python-3.3-to-3.5 code/branch."
% sys.version[0:3]
)
elif SYS_VERSION < (2, 4):
mess += (
"\nThis package is not supported for Python version %s." % sys.version[0:3]
)
print(mess)
raise Exception(mess)
from __pkginfo__ import (
author,
author_email,
install_requires,
license,
long_description,
classifiers,
entry_points,
modname,
py_modules,
short_desc,
__version__,
web,
zip_safe,
)
setuptools.setup(
author=author,
author_email=author_email,
classifiers=classifiers,
description=short_desc,
entry_points=entry_points,
install_requires=install_requires,
license=license,
long_description=long_description,
long_description_content_type="text/x-rst",
name=modname,
packages=setuptools.find_packages(),
py_modules=py_modules,
test_suite="nose.collector",
url=web,
tests_require=["nose>=1.0"],
version=__version__,
zip_safe=zip_safe,
)

View File

@@ -115,7 +115,7 @@ check-bytecode-2:
# FIXME: Until we shaked out problems with xdis...
check-bytecode-3:
$(PYTHON) test_pythonlib.py \
--bytecode-3.3 --bytecode-3.4 --bytecode-3.5 --bytecode-3.6 \
--bytecode-3.4 --bytecode-3.5 --bytecode-3.6 \
--bytecode-3.7 --bytecode-3.8
#: Check deparsing on selected bytecode 3.x

View File

@@ -1,21 +0,0 @@
"""
This program is self checking!
"""
class TestContextManager:
def __enter__(self):
return 1, 2
def __exit__(self, exc_type, exc_value, exc_tb):
return self, exc_type, exc_value, exc_tb
with open(__file__) as a:
assert a
with open(__file__) as a, open(__file__) as b:
assert a.read() == b.read()
with TestContextManager() as a, b:
assert (a, b) == (1, 2)

View File

@@ -10,7 +10,7 @@ SKIP_TESTS=(
# tgt.append(elem)
[test_itertools.py]=1
[test_buffer.py]=pytest
[test_buffer.py]=pytest # FIXME: Works on c90ff51
[test_cmath.py]=pytest
[test_atexit.py]=1 # The atexit test starting at 3.3 looks for specific comments in error lines
@@ -19,6 +19,7 @@ SKIP_TESTS=(
[test_concurrent_futures.py]=1 # too long?
[test_decimal.py]=1 # test takes too long to run: 18 seconds
[test_descr.py]=1 # test assertion errors
[test_doctest.py]=1 # test assertion errors
[test_doctest2.py]=1 # test assertion errors
[test_dis.py]=1 # We change line numbers - duh!

View File

@@ -26,16 +26,7 @@ SKIP_TESTS=(
[test_dbm_gnu.py]=1 # fails on its own
[test_devpoll.py]=1 # it fails on its own
[test_descr.py]=1 # test assertion errors
# ERROR: test_reent_set_bases_on_base (__main__.MroTest)
# Traceback (most recent call last):
# File "test_descr.py", line 5521, in test_reent_set_bases_on_base
# class A(metaclass=M):
# File "test_descr.py", line 5472, in __new__
# return type.__new__(mcls, name, bases, attrs)
# TypeError: 'NoneType' object is not iterable
[test_dis.py]=1 # We change line numbers - duh!
[test_distutils.py]=1 # it fails on its own
[test_doctest2.py]=1

View File

@@ -361,7 +361,7 @@ def main(
outstream.write(f"{line}\n{e[0]}\n{line}\n")
last_mod = e[0]
info = offsets[e]
extract_info = deparsed_object.extract_node_info(info)
extract_info = deparse_object.extract_node_info(info)
outstream.write(f"{info.node.format().strip()}" + "\n")
outstream.write(extract_info.selectedLine + "\n")
outstream.write(extract_info.markerLine + "\n\n")
@@ -372,7 +372,7 @@ def main(
deparsed_object.f.close()
if PYTHON_VERSION_TRIPLE[:2] != deparsed_object.version[:2]:
sys.stdout.write(
f"\n# skipping running {deparsed_object.f.name}; it is "
f"\n# skipping running {deparsed_object.f.name}; it is"
f"{version_tuple_to_str(deparsed_object.version, end=2)}, "
"and we are "
f"{version_tuple_to_str(PYTHON_VERSION_TRIPLE, end=2)}\n"

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2015-2024 Rocky Bernstein
# Copyright (c) 2015-2023 Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 1999 John Aycock
@@ -21,10 +21,9 @@ Common uncompyle6 parser routines.
import sys
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG, GenericASTBuilder
from xdis import iscode
from spark_parser import GenericASTBuilder, DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.show import maybe_show_asm
from xdis import iscode
class ParserError(Exception):
@@ -92,14 +91,7 @@ class PythonParser(GenericASTBuilder):
# singleton reduction that we can simplify. It also happens to be optional
# in its other derivation
self.optional_nt |= frozenset(
(
"come_froms",
"suite_stmts",
"l_stmts_opt",
"c_stmts_opt",
"stmts_opt",
"stmt",
)
("come_froms", "suite_stmts", "l_stmts_opt", "c_stmts_opt", "stmts_opt", "stmt")
)
# Reduce singleton reductions in these nonterminals:
@@ -121,10 +113,10 @@ class PythonParser(GenericASTBuilder):
def add_unique_rule(self, rule, opname, arg_count, customize):
"""Add rule to grammar, but only if it hasn't been added previously
opname and stack_count are used in the customize() semantic
the actions to add the semantic action rule. Stack_count is
used in custom opcodes like MAKE_FUNCTION to indicate how
many arguments it has. Often it is not used.
opname and stack_count are used in the customize() semantic
the actions to add the semantic action rule. Stack_count is
used in custom opcodes like MAKE_FUNCTION to indicate how
many arguments it has. Often it is not used.
"""
if rule not in self.new_rules:
# print("XXX ", rule) # debug
@@ -231,9 +223,7 @@ class PythonParser(GenericASTBuilder):
"""
# Low byte indicates number of positional parameters,
# high byte number of keyword parameters
assert token.kind.startswith("CALL_FUNCTION") or token.kind.startswith(
"CALL_METHOD"
)
assert token.kind.startswith("CALL_FUNCTION") or token.kind.startswith("CALL_METHOD")
args_pos = token.attr & 0xFF
args_kw = (token.attr >> 8) & 0xFF
return args_pos, args_kw
@@ -374,7 +364,7 @@ class PythonParser(GenericASTBuilder):
stmt ::= tryelsestmt
stmt ::= tryfinallystmt
stmt ::= with
stmt ::= with_as
stmt ::= withasstmt
stmt ::= delete
delete ::= DELETE_FAST
@@ -917,7 +907,7 @@ def python_parser(
if __name__ == "__main__":
def parse_test(co):
from xdis import IS_PYPY, PYTHON_VERSION_TRIPLE
from xdis import PYTHON_VERSION_TRIPLE, IS_PYPY
ast = python_parser(PYTHON_VERSION_TRIPLE, co, showasm=True, is_pypy=IS_PYPY)
print(ast)

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2018, 2020, 2022-2024 Rocky Bernstein
# Copyright (c) 2016-2018, 2020, 2022-2023 Rocky Bernstein
"""
spark grammar differences over Python2.5 for Python 2.4.
"""
@@ -89,14 +89,12 @@ class Python24Parser(Python25Parser):
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK COME_FROM
while1stmt ::= SETUP_LOOP returns COME_FROM
whilestmt ::= SETUP_LOOP testexpr returns POP_BLOCK COME_FROM
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt POP_BLOCK
LOAD_CONST COME_FROM with_cleanup
with_as ::= expr setupwithas store suite_stmts_opt POP_BLOCK
LOAD_CONST COME_FROM with_cleanup
with_cleanup ::= LOAD_FAST DELETE_FAST WITH_CLEANUP END_FINALLY
with_cleanup ::= LOAD_NAME DELETE_NAME WITH_CLEANUP END_FINALLY
stmt ::= with
stmt ::= with_as
withasstmt ::= expr setupwithas store suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM with_cleanup
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM with_cleanup
stmt ::= with
stmt ::= withasstmt
"""
)
super(Python24Parser, self).customize_grammar_rules(tokens, customize)

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2018, 2020, 2022, 2024 Rocky Bernstein
# Copyright (c) 2016-2018, 2020, 2022 Rocky Bernstein
"""
spark grammar differences over Python2.6 for Python 2.5.
"""
@@ -33,7 +33,7 @@ class Python25Parser(Python26Parser):
POP_BLOCK LOAD_CONST COME_FROM with_cleanup
# Semantic actions want store to be at index 2
with_as ::= expr setupwithas store suite_stmts_opt
withasstmt ::= expr setupwithas store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM with_cleanup
@@ -48,7 +48,7 @@ class Python25Parser(Python26Parser):
# Python 2.6 omits the LOAD_FAST DELETE_FAST below
# withas is allowed as a "from future" in 2.5
with_as ::= expr setupwithas store suite_stmts_opt
withasstmt ::= expr setupwithas store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM
with_cleanup
@@ -67,7 +67,7 @@ class Python25Parser(Python26Parser):
setupwith ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 POP_TOP
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
with_as ::= expr setupwithas store suite_stmts_opt
withasstmt ::= expr setupwithas store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr CALL_FUNCTION_1 RAISE_VARARGS_1
classdefdeco ::= classdefdeco1 store

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2017-2024 Rocky Bernstein
# Copyright (c) 2017-2023 Rocky Bernstein
"""
spark grammar differences over Python2 for Python 2.6.
"""
@@ -136,7 +136,7 @@ class Python26Parser(Python2Parser):
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
# Semantic actions want store to be at index 2
with_as ::= expr setupwithas store suite_stmts_opt
withasstmt ::= expr setupwithas store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
# This is truly weird. 2.7 does this (not including POP_TOP) with
@@ -352,9 +352,9 @@ class Python26Parser(Python2Parser):
def customize_grammar_rules(self, tokens, customize):
self.remove_rules(
"""
with_as ::= expr SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
withasstmt ::= expr SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
"""
)
super(Python26Parser, self).customize_grammar_rules(tokens, customize)
@@ -391,6 +391,7 @@ class Python26Parser(Python2Parser):
("and", ("expr", "jmp_false", "expr", "come_from_opt")),
("assert_expr_and", ("assert_expr", "jmp_false", "expr")),
):
# FIXME: workaround profiling bug
if ast[1] is None:
return False
@@ -490,6 +491,7 @@ class Python26Parser(Python2Parser):
("JUMP_FORWARD", "RETURN_VALUE")
) or (tokens[last - 3] == "JUMP_FORWARD" and tokens[last - 3].attr != 2)
elif lhs == "tryelsestmt":
# We need to distinguish "try_except" from "tryelsestmt"; we do that
# by making sure that the jump before the except handler jumps to
# code somewhere before the end of the construct.

View File

@@ -161,9 +161,9 @@ class Python27Parser(Python2Parser):
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
with_as ::= expr SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
withasstmt ::= expr SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
whilestmt ::= SETUP_LOOP testexpr returns
_come_froms POP_BLOCK COME_FROM

View File

@@ -287,9 +287,9 @@ class Python3Parser(PythonParser):
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
with_as ::= expr SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
withasstmt ::= expr SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
expr_jt ::= expr jmp_true
expr_jitop ::= expr JUMP_IF_TRUE_OR_POP

View File

@@ -66,7 +66,7 @@ class Python30Parser(Python31Parser):
iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE COME_FROM POP_TOP
with_as ::= expr setupwithas store suite_stmts_opt
withasstmt ::= expr setupwithas store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_FINALLY
LOAD_FAST DELETE_FAST WITH_CLEANUP END_FINALLY
setupwithas ::= DUP_TOP LOAD_ATTR STORE_FAST LOAD_ATTR CALL_FUNCTION_0 setup_finally
@@ -222,17 +222,12 @@ class Python30Parser(Python31Parser):
# The were found using grammar coverage
while1stmt ::= SETUP_LOOP l_stmts COME_FROM JUMP_BACK COME_FROM_LOOP
whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM_LOOP
whileelsestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK
else_suitel COME_FROM_LOOP
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK
COME_FROM_LOOP
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK JUMP_BACK
COME_FROM_LOOP
whileelsestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK else_suitel COME_FROM_LOOP
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM_LOOP
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK JUMP_BACK COME_FROM_LOOP
whilestmt ::= SETUP_LOOP testexpr returns POP_TOP POP_BLOCK COME_FROM_LOOP
with_as ::= expr SETUP_WITH store suite_stmts_opt POP_BLOCK LOAD_CONST
COME_FROM_WITH WITH_CLEANUP END_FINALLY
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK LOAD_CONST
COME_FROM_WITH WITH_CLEANUP END_FINALLY
withasstmt ::= expr SETUP_WITH store suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM_WITH WITH_CLEANUP END_FINALLY
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM_WITH WITH_CLEANUP END_FINALLY
# lc_body ::= LOAD_FAST expr LIST_APPEND
# lc_body ::= LOAD_NAME expr LIST_APPEND

View File

@@ -23,7 +23,7 @@ class Python31Parser(Python32Parser):
# Keeps Python 3.1 "with .. as" designator in the same position as it is in other version.
setupwithas31 ::= setupwithas SETUP_FINALLY load delete
with_as ::= expr setupwithas31 store
withasstmt ::= expr setupwithas31 store
suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_FINALLY
load delete WITH_CLEANUP END_FINALLY

View File

@@ -1,17 +1,15 @@
# Copyright (c) 2016-2017, 2019, 2021, 2023-2024
# Rocky Bernstein
# Copyright (c) 2016-2017, 2019, 2021, 2023 Rocky Bernstein
"""
spark grammar differences over Python 3.4 for Python 3.5.
"""
from __future__ import print_function
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parser import PythonParserSingle, nop_func
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parsers.parse34 import Python34Parser
class Python35Parser(Python34Parser):
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
super(Python35Parser, self).__init__(debug_parser)
self.customized = {}
@@ -57,7 +55,7 @@ class Python35Parser(Python34Parser):
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
with_as ::= expr
withasstmt ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
@@ -137,42 +135,40 @@ class Python35Parser(Python34Parser):
"""
def customize_grammar_rules(self, tokens, customize):
self.remove_rules(
"""
self.remove_rules("""
yield_from ::= expr GET_ITER LOAD_CONST YIELD_FROM
yield_from ::= expr expr YIELD_FROM
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
with_as ::= expr SETUP_WITH store suite_stmts_opt
withasstmt ::= expr SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
"""
)
""")
super(Python35Parser, self).customize_grammar_rules(tokens, customize)
for i, token in enumerate(tokens):
opname = token.kind
if opname == "LOAD_ASSERT":
if "PyPy" in customize:
if opname == 'LOAD_ASSERT':
if 'PyPy' in customize:
rules_str = """
stmt ::= JUMP_IF_NOT_DEBUG stmts COME_FROM
"""
self.add_unique_doc_rules(rules_str, customize)
# FIXME: I suspect this is wrong for 3.6 and 3.5, but
# I haven't verified what the 3.7ish fix is
elif opname == "BUILD_MAP_UNPACK_WITH_CALL":
elif opname == 'BUILD_MAP_UNPACK_WITH_CALL':
if self.version < (3, 7):
self.addRule("expr ::= unmapexpr", nop_func)
nargs = token.attr % 256
map_unpack_n = "map_unpack_%s" % nargs
rule = map_unpack_n + " ::= " + "expr " * (nargs)
rule = map_unpack_n + ' ::= ' + 'expr ' * (nargs)
self.addRule(rule, nop_func)
rule = "unmapexpr ::= %s %s" % (map_unpack_n, opname)
self.addRule(rule, nop_func)
call_token = tokens[i + 1]
rule = "call ::= expr unmapexpr " + call_token.kind
call_token = tokens[i+1]
rule = 'call ::= expr unmapexpr ' + call_token.kind
self.addRule(rule, nop_func)
elif opname == "BEFORE_ASYNC_WITH" and self.version < (3, 8):
elif opname == 'BEFORE_ASYNC_WITH' and self.version < (3, 8):
# Some Python 3.5+ async additions
rules_str = """
stmt ::= async_with_stmt
@@ -203,27 +199,24 @@ class Python35Parser(Python34Parser):
async_with_post
"""
self.addRule(rules_str, nop_func)
elif opname == "BUILD_MAP_UNPACK":
self.addRule(
"""
elif opname == 'BUILD_MAP_UNPACK':
self.addRule("""
expr ::= dict_unpack
dict_unpack ::= dict_comp BUILD_MAP_UNPACK
""",
nop_func,
)
""", nop_func)
elif opname == "SETUP_WITH":
elif opname == 'SETUP_WITH':
# Python 3.5+ has WITH_CLEANUP_START/FINISH
rules_str = """
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
with ::= expr
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
withasstmt ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
"""
self.addRule(rules_str, nop_func)
pass
@@ -237,24 +230,19 @@ class Python35Parser(Python34Parser):
# 1 for CALL_FUNCTION_VAR or CALL_FUNCTION_KW
# 2 for * and ** args (CALL_FUNCTION_VAR_KW).
# Yes, this computation based on instruction name is a little bit hoaky.
nak = (len(opname) - len("CALL_FUNCTION")) // 3
nak = ( len(opname)-len('CALL_FUNCTION') ) // 3
uniq_param = args_kw + args_pos
if frozenset(("GET_AWAITABLE", "YIELD_FROM")).issubset(self.seen_ops):
rule = (
"async_call ::= expr "
+ ("pos_arg " * args_pos)
+ ("kwarg " * args_kw)
+ "expr " * nak
+ token.kind
+ " GET_AWAITABLE LOAD_CONST YIELD_FROM"
)
if frozenset(('GET_AWAITABLE', 'YIELD_FROM')).issubset(self.seen_ops):
rule = ('async_call ::= expr ' +
('pos_arg ' * args_pos) +
('kwarg ' * args_kw) +
'expr ' * nak + token.kind +
' GET_AWAITABLE LOAD_CONST YIELD_FROM')
self.add_unique_rule(rule, token.kind, uniq_param, customize)
self.add_unique_rule(
"expr ::= async_call", token.kind, uniq_param, customize
)
self.add_unique_rule('expr ::= async_call', token.kind, uniq_param, customize)
if opname.startswith("CALL_FUNCTION_VAR"):
if opname.startswith('CALL_FUNCTION_VAR'):
# Python 3.5 changes the stack position of *args. KW args come
# after *args.
@@ -262,55 +250,43 @@ class Python35Parser(Python34Parser):
# CALL_FUNCTION_VAR_KW with CALL_FUNCTION_EX
token.kind = self.call_fn_name(token)
if opname.endswith("KW"):
kw = "expr "
if opname.endswith('KW'):
kw = 'expr '
else:
kw = ""
rule = (
"call ::= expr expr "
+ ("pos_arg " * args_pos)
+ ("kwarg " * args_kw)
+ kw
+ token.kind
)
kw = ''
rule = ('call ::= expr expr ' +
('pos_arg ' * args_pos) +
('kwarg ' * args_kw) + kw + token.kind)
# Note: semantic actions make use of the fact of whether "args_pos"
# zero or not in creating a template rule.
self.add_unique_rule(rule, token.kind, args_pos, customize)
else:
super(Python35Parser, self).custom_classfunc_rule(
opname, token, customize, *args
super(Python35Parser, self).custom_classfunc_rule(opname, token, customize, *args
)
class Python35ParserSingle(Python35Parser, PythonParserSingle):
pass
if __name__ == "__main__":
if __name__ == '__main__':
# Check grammar
p = Python35Parser()
p.check_grammar()
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
from xdis.version_info import PYTHON_VERSION_TRIPLE, IS_PYPY
if PYTHON_VERSION_TRIPLE[:2] == (3, 5):
lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()
from uncompyle6.scanner import get_scanner
s = get_scanner(PYTHON_VERSION_TRIPLE, IS_PYPY)
opcode_set = set(s.opc.opname).union(
set(
"""JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
opcode_set = set(s.opc.opname).union(set(
"""JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
LAMBDA_MARKER RETURN_LAST
""".split()
)
)
""".split()))
remain_tokens = set(tokens) - opcode_set
import re
remain_tokens = set([re.sub(r"_\d+$", "", t) for t in remain_tokens])
remain_tokens = set([re.sub("_CONT$", "", t) for t in remain_tokens])
remain_tokens = set([re.sub(r'_\d+$', '', t) for t in remain_tokens])
remain_tokens = set([re.sub('_CONT$', '', t) for t in remain_tokens])
remain_tokens = set(remain_tokens) - opcode_set
print(remain_tokens)
# print(sorted(p.rule2name.items()))

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2020, 2022-2024 Rocky Bernstein
# Copyright (c) 2016-2020, 2022-2023 Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -17,25 +17,24 @@ spark grammar differences over Python 3.5 for Python 3.6.
"""
from __future__ import print_function
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parser import PythonParserSingle, nop_func
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parsers.parse35 import Python35Parser
from uncompyle6.scanners.tok import Token
class Python36Parser(Python35Parser):
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
super(Python36Parser, self).__init__(debug_parser)
self.customized = {}
def p_36_jump(self, args):
"""
# Zero or one COME_FROM
# And/or expressions have this
come_from_opt ::= COME_FROM?
"""
def p_36_misc(self, args):
"""sstmt ::= sstmt RETURN_LAST
@@ -208,8 +207,7 @@ class Python36Parser(Python35Parser):
# self.remove_rules("""
# """)
super(Python36Parser, self).customize_grammar_rules(tokens, customize)
self.remove_rules(
"""
self.remove_rules("""
_ifstmts_jumpl ::= c_stmts_opt
_ifstmts_jumpl ::= _ifstmts_jump
except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts END_FINALLY COME_FROM
@@ -236,8 +234,7 @@ class Python36Parser(Python35Parser):
for_block pb_ja
else_suite COME_FROM_LOOP
"""
)
""")
self.check_reduce["call_kw"] = "AST"
# Opcode names in the custom_ops_processed set have rules that get added
@@ -250,23 +247,24 @@ class Python36Parser(Python35Parser):
# the start.
custom_ops_processed = set()
for i, token in enumerate(tokens):
opname = token.kind
if opname == "FORMAT_VALUE":
if opname == 'FORMAT_VALUE':
rules_str = """
expr ::= formatted_value1
formatted_value1 ::= expr FORMAT_VALUE
"""
self.add_unique_doc_rules(rules_str, customize)
elif opname == "FORMAT_VALUE_ATTR":
elif opname == 'FORMAT_VALUE_ATTR':
rules_str = """
expr ::= formatted_value2
formatted_value2 ::= expr expr FORMAT_VALUE_ATTR
"""
self.add_unique_doc_rules(rules_str, customize)
elif opname == "MAKE_FUNCTION_CLOSURE":
if "LOAD_DICTCOMP" in self.seen_ops:
elif opname == 'MAKE_FUNCTION_CLOSURE':
if 'LOAD_DICTCOMP' in self.seen_ops:
# Is there something general going on here?
rule = """
dict_comp ::= load_closure LOAD_DICTCOMP LOAD_STR
@@ -274,7 +272,7 @@ class Python36Parser(Python35Parser):
GET_ITER CALL_FUNCTION_1
"""
self.addRule(rule, nop_func)
elif "LOAD_SETCOMP" in self.seen_ops:
elif 'LOAD_SETCOMP' in self.seen_ops:
rule = """
set_comp ::= load_closure LOAD_SETCOMP LOAD_STR
MAKE_FUNCTION_CLOSURE expr
@@ -282,7 +280,7 @@ class Python36Parser(Python35Parser):
"""
self.addRule(rule, nop_func)
elif opname == "BEFORE_ASYNC_WITH":
elif opname == 'BEFORE_ASYNC_WITH':
rules_str = """
stmt ::= async_with_stmt
async_with_pre ::= BEFORE_ASYNC_WITH GET_AWAITABLE LOAD_CONST YIELD_FROM SETUP_ASYNC_WITH
@@ -308,37 +306,30 @@ class Python36Parser(Python35Parser):
"""
self.addRule(rules_str, nop_func)
elif opname.startswith("BUILD_STRING"):
elif opname.startswith('BUILD_STRING'):
v = token.attr
rules_str = """
expr ::= joined_str
joined_str ::= %sBUILD_STRING_%d
""" % (
"expr " * v,
v,
)
""" % ("expr " * v, v)
self.add_unique_doc_rules(rules_str, customize)
if "FORMAT_VALUE_ATTR" in self.seen_ops:
if 'FORMAT_VALUE_ATTR' in self.seen_ops:
rules_str = """
formatted_value_attr ::= expr expr FORMAT_VALUE_ATTR expr BUILD_STRING
expr ::= formatted_value_attr
"""
self.add_unique_doc_rules(rules_str, customize)
elif opname.startswith("BUILD_MAP_UNPACK_WITH_CALL"):
elif opname.startswith('BUILD_MAP_UNPACK_WITH_CALL'):
v = token.attr
rule = "build_map_unpack_with_call ::= %s%s" % ("expr " * v, opname)
rule = 'build_map_unpack_with_call ::= %s%s' % ('expr ' * v, opname)
self.addRule(rule, nop_func)
elif opname.startswith("BUILD_TUPLE_UNPACK_WITH_CALL"):
elif opname.startswith('BUILD_TUPLE_UNPACK_WITH_CALL'):
v = token.attr
rule = (
"build_tuple_unpack_with_call ::= "
+ "expr1024 " * int(v // 1024)
+ "expr32 " * int((v // 32) % 32)
+ "expr " * (v % 32)
+ opname
)
rule = ('build_tuple_unpack_with_call ::= ' + 'expr1024 ' * int(v//1024) +
'expr32 ' * int((v//32) % 32) +
'expr ' * (v % 32) + opname)
self.addRule(rule, nop_func)
rule = "starred ::= %s %s" % ("expr " * v, opname)
rule = ('starred ::= %s %s' % ('expr ' * v, opname))
self.addRule(rule, nop_func)
elif opname == "GET_AITER":
self.addRule(
@@ -484,6 +475,7 @@ class Python36Parser(Python35Parser):
)
custom_ops_processed.add(opname)
elif opname == "GET_ANEXT":
self.addRule(
"""
@@ -508,7 +500,7 @@ class Python36Parser(Python35Parser):
)
custom_ops_processed.add(opname)
elif opname == "SETUP_ANNOTATIONS":
elif opname == 'SETUP_ANNOTATIONS':
# 3.6 Variable Annotations PEP 526
# This seems to come before STORE_ANNOTATION, and doesn't
# correspond to direct Python source code.
@@ -524,7 +516,7 @@ class Python36Parser(Python35Parser):
"""
self.addRule(rule, nop_func)
# Check to combine assignment + annotation into one statement
self.check_reduce["assign"] = "token"
self.check_reduce['assign'] = 'token'
elif opname == "WITH_CLEANUP_START":
rules_str = """
stmt ::= with_null
@@ -532,13 +524,13 @@ class Python36Parser(Python35Parser):
with_suffix ::= WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
"""
self.addRule(rules_str, nop_func)
elif opname == "SETUP_WITH":
elif opname == 'SETUP_WITH':
rules_str = """
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt COME_FROM_WITH
with_suffix
# Removes POP_BLOCK LOAD_CONST from 3.6-
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_suffix
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
BEGIN_FINALLY COME_FROM_WITH
@@ -550,6 +542,7 @@ class Python36Parser(Python35Parser):
return
def custom_classfunc_rule(self, opname, token, customize, next_token, is_pypy):
args_pos, args_kw = self.get_pos_kw(token)
# Additional exprs for * and ** args:
@@ -557,186 +550,140 @@ class Python36Parser(Python35Parser):
# 1 for CALL_FUNCTION_VAR or CALL_FUNCTION_KW
# 2 for * and ** args (CALL_FUNCTION_VAR_KW).
# Yes, this computation based on instruction name is a little bit hoaky.
nak = (len(opname) - len("CALL_FUNCTION")) // 3
nak = ( len(opname)-len('CALL_FUNCTION') ) // 3
uniq_param = args_kw + args_pos
if frozenset(("GET_AWAITABLE", "YIELD_FROM")).issubset(self.seen_ops):
rule = (
"async_call ::= expr "
+ ("pos_arg " * args_pos)
+ ("kwarg " * args_kw)
+ "expr " * nak
+ token.kind
+ " GET_AWAITABLE LOAD_CONST YIELD_FROM"
)
if frozenset(('GET_AWAITABLE', 'YIELD_FROM')).issubset(self.seen_ops):
rule = ('async_call ::= expr ' +
('pos_arg ' * args_pos) +
('kwarg ' * args_kw) +
'expr ' * nak + token.kind +
' GET_AWAITABLE LOAD_CONST YIELD_FROM')
self.add_unique_rule(rule, token.kind, uniq_param, customize)
self.add_unique_rule(
"expr ::= async_call", token.kind, uniq_param, customize
)
self.add_unique_rule('expr ::= async_call', token.kind, uniq_param, customize)
if opname.startswith("CALL_FUNCTION_KW"):
if opname.startswith('CALL_FUNCTION_KW'):
if is_pypy:
# PYPY doesn't follow CPython 3.6 CALL_FUNCTION_KW conventions
super(Python36Parser, self).custom_classfunc_rule(
opname, token, customize, next_token, is_pypy
)
super(Python36Parser, self).custom_classfunc_rule(opname, token, customize, next_token, is_pypy)
else:
self.addRule("expr ::= call_kw36", nop_func)
values = "expr " * token.attr
rule = "call_kw36 ::= expr {values} LOAD_CONST {opname}".format(
**locals()
)
values = 'expr ' * token.attr
rule = "call_kw36 ::= expr {values} LOAD_CONST {opname}".format(**locals())
self.add_unique_rule(rule, token.kind, token.attr, customize)
elif opname == "CALL_FUNCTION_EX_KW":
elif opname == 'CALL_FUNCTION_EX_KW':
# Note: this doesn't exist in 3.7 and later
self.addRule(
"""expr ::= call_ex_kw4
self.addRule("""expr ::= call_ex_kw4
call_ex_kw4 ::= expr
expr
expr
CALL_FUNCTION_EX_KW
""",
nop_func,
)
if "BUILD_MAP_UNPACK_WITH_CALL" in self.seen_op_basenames:
self.addRule(
"""expr ::= call_ex_kw
nop_func)
if 'BUILD_MAP_UNPACK_WITH_CALL' in self.seen_op_basenames:
self.addRule("""expr ::= call_ex_kw
call_ex_kw ::= expr expr build_map_unpack_with_call
CALL_FUNCTION_EX_KW
""",
nop_func,
)
if "BUILD_TUPLE_UNPACK_WITH_CALL" in self.seen_op_basenames:
""", nop_func)
if 'BUILD_TUPLE_UNPACK_WITH_CALL' in self.seen_op_basenames:
# FIXME: should this be parameterized by EX value?
self.addRule(
"""expr ::= call_ex_kw3
self.addRule("""expr ::= call_ex_kw3
call_ex_kw3 ::= expr
build_tuple_unpack_with_call
expr
CALL_FUNCTION_EX_KW
""",
nop_func,
)
if "BUILD_MAP_UNPACK_WITH_CALL" in self.seen_op_basenames:
""", nop_func)
if 'BUILD_MAP_UNPACK_WITH_CALL' in self.seen_op_basenames:
# FIXME: should this be parameterized by EX value?
self.addRule(
"""expr ::= call_ex_kw2
self.addRule("""expr ::= call_ex_kw2
call_ex_kw2 ::= expr
build_tuple_unpack_with_call
build_map_unpack_with_call
CALL_FUNCTION_EX_KW
""",
nop_func,
)
""", nop_func)
elif opname == "CALL_FUNCTION_EX":
self.addRule(
"""
elif opname == 'CALL_FUNCTION_EX':
self.addRule("""
expr ::= call_ex
starred ::= expr
call_ex ::= expr starred CALL_FUNCTION_EX
""",
nop_func,
)
""", nop_func)
if self.version >= (3, 6):
if "BUILD_MAP_UNPACK_WITH_CALL" in self.seen_ops:
self.addRule(
"""
if 'BUILD_MAP_UNPACK_WITH_CALL' in self.seen_ops:
self.addRule("""
expr ::= call_ex_kw
call_ex_kw ::= expr expr
build_map_unpack_with_call CALL_FUNCTION_EX
""",
nop_func,
)
if "BUILD_TUPLE_UNPACK_WITH_CALL" in self.seen_ops:
self.addRule(
"""
""", nop_func)
if 'BUILD_TUPLE_UNPACK_WITH_CALL' in self.seen_ops:
self.addRule("""
expr ::= call_ex_kw3
call_ex_kw3 ::= expr
build_tuple_unpack_with_call
%s
CALL_FUNCTION_EX
"""
% "expr "
* token.attr,
nop_func,
)
""" % 'expr ' * token.attr, nop_func)
pass
# FIXME: Is this right?
self.addRule(
"""
self.addRule("""
expr ::= call_ex_kw4
call_ex_kw4 ::= expr
expr
expr
CALL_FUNCTION_EX
""",
nop_func,
)
""", nop_func)
pass
else:
super(Python36Parser, self).custom_classfunc_rule(
opname, token, customize, next_token, is_pypy
)
super(Python36Parser, self).custom_classfunc_rule(opname, token, customize, next_token, is_pypy)
def reduce_is_invalid(self, rule, ast, tokens, first, last):
invalid = super(Python36Parser, self).reduce_is_invalid(
rule, ast, tokens, first, last
)
invalid = super(Python36Parser,
self).reduce_is_invalid(rule, ast,
tokens, first, last)
if invalid:
return invalid
if rule[0] == "assign":
if rule[0] == 'assign':
# Try to combine assignment + annotation into one statement
if (
len(tokens) >= last + 1
and tokens[last] == "LOAD_NAME"
and tokens[last + 1] == "STORE_ANNOTATION"
and tokens[last - 1].pattr == tokens[last + 1].pattr
):
if (len(tokens) >= last + 1 and
tokens[last] == 'LOAD_NAME' and
tokens[last+1] == 'STORE_ANNOTATION' and
tokens[last-1].pattr == tokens[last+1].pattr):
# Will handle as ann_assign_init_value
return True
pass
if rule[0] == "call_kw":
if rule[0] == 'call_kw':
# Make sure we don't derive call_kw
nt = ast[0]
while not isinstance(nt, Token):
if nt[0] == "call_kw":
if nt[0] == 'call_kw':
return True
nt = nt[0]
pass
pass
return False
class Python36ParserSingle(Python36Parser, PythonParserSingle):
pass
if __name__ == "__main__":
if __name__ == '__main__':
# Check grammar
p = Python36Parser()
p.check_grammar()
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
from xdis.version_info import PYTHON_VERSION_TRIPLE, IS_PYPY
if PYTHON_VERSION_TRIPLE[:2] == (3, 6):
lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()
from uncompyle6.scanner import get_scanner
s = get_scanner(PYTHON_VERSION_TRIPLE, IS_PYPY)
opcode_set = set(s.opc.opname).union(
set(
"""JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
opcode_set = set(s.opc.opname).union(set(
"""JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
LAMBDA_MARKER RETURN_LAST
""".split()
)
)
""".split()))
remain_tokens = set(tokens) - opcode_set
import re
remain_tokens = set([re.sub(r"_\d+$", "", t) for t in remain_tokens])
remain_tokens = set([re.sub("_CONT$", "", t) for t in remain_tokens])
remain_tokens = set([re.sub(r'_\d+$', '', t) for t in remain_tokens])
remain_tokens = set([re.sub('_CONT$', '', t) for t in remain_tokens])
remain_tokens = set(remain_tokens) - opcode_set
print(remain_tokens)
# print(sorted(p.rule2name.items()))

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2017-2020, 2022-2024 Rocky Bernstein
# Copyright (c) 2017-2020, 2022-2023 Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -17,12 +17,10 @@ Python 3.7 grammar for the spark Earley-algorithm parser.
"""
from __future__ import print_function
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parser import PythonParserSingle, nop_func
from uncompyle6.parsers.parse37base import Python37BaseParser
from uncompyle6.scanners.tok import Token
from uncompyle6.parser import PythonParserSingle, nop_func
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parsers.parse37base import Python37BaseParser
class Python37Parser(Python37BaseParser):
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
@@ -251,7 +249,8 @@ class Python37Parser(Python37BaseParser):
"""
def p_generator_exp(self, args):
""" """
"""
"""
def p_jump(self, args):
"""
@@ -758,7 +757,7 @@ class Python37Parser(Python37BaseParser):
"""
def p_dict_comp3(self, args):
""" "
""""
expr ::= dict_comp
stmt ::= dict_comp_func
@@ -1555,7 +1554,7 @@ class Python37Parser(Python37BaseParser):
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
# Removes POP_BLOCK LOAD_CONST from 3.6-
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
"""
if self.version < (3, 8):
@@ -1576,6 +1575,7 @@ class Python37Parser(Python37BaseParser):
pass
def custom_classfunc_rule(self, opname, token, customize, next_token):
args_pos, args_kw = self.get_pos_kw(token)
# Additional exprs for * and ** args:
@@ -1718,7 +1718,6 @@ class Python37Parser(Python37BaseParser):
pass
return False
def info(args):
# Check grammar
p = Python37Parser()
@@ -1749,7 +1748,7 @@ if __name__ == "__main__":
# FIXME: DRY this with other parseXX.py routines
p = Python37Parser()
p.check_grammar()
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
from xdis.version_info import PYTHON_VERSION_TRIPLE, IS_PYPY
if PYTHON_VERSION_TRIPLE[:2] == (3, 7):
lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2017, 2019-2020, 2022-2024 Rocky Bernstein
# Copyright (c) 2016-2017, 2019-2020, 2022-2023 Rocky Bernstein
"""
Python 3.7 base code. We keep non-custom-generated grammar rules out of this file.
"""
@@ -1055,14 +1055,14 @@ class Python37BaseParser(PythonParser):
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with
stmt ::= with_as
stmt ::= withasstmt
with ::= expr
SETUP_WITH POP_TOP
suite_stmts_opt
COME_FROM_WITH
with_suffix
with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
with_suffix
with ::= expr
@@ -1071,7 +1071,7 @@ class Python37BaseParser(PythonParser):
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
with_as ::= expr
withasstmt ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
@@ -1080,7 +1080,7 @@ class Python37BaseParser(PythonParser):
SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
with_as ::= expr
withasstmt ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
@@ -1098,18 +1098,17 @@ class Python37BaseParser(PythonParser):
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix
withasstmt ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
withasstmt ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH with_suffix
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
BEGIN_FINALLY COME_FROM_WITH
with_suffix
with_as ::= expr
SETUP_WITH store suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
with_as ::= expr
SETUP_WITH store suite_stmts
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH with_suffix
"""
self.addRule(rules_str, nop_func)

View File

@@ -80,8 +80,8 @@ class Python38Parser(Python37Parser):
JUMP_BACK COME_FROM_FINALLY
END_ASYNC_FOR
# FIXME: "come_froms" after the "else_suite" or END_ASYNC_FOR distinguish which of
# for / forelse is used. Add "come_froms" and check of add up control-flow detection phase.
# FIXME: come froms after the else_suite or END_ASYNC_FOR distinguish which of
# for / forelse is used. Add come froms and check of add up control-flow detection phase.
async_forelse_stmt38 ::= expr
GET_AITER
SETUP_FINALLY
@@ -586,15 +586,6 @@ class Python38Parser(Python37Parser):
GET_ITER CALL_FUNCTION_1
"""
self.addRule(rule, nop_func)
elif opname == "SETUP_WITH":
rules_str = """
stmt ::= with_as_pass
with_as_pass ::= expr
SETUP_WITH store pass
POP_BLOCK BEGIN_FINALLY COME_FROM_WITH
with_suffix
"""
self.addRule(rules_str, nop_func)
def reduce_is_invalid(self, rule, ast, tokens, first, last):
invalid = super(Python38Parser, self).reduce_is_invalid(

View File

@@ -4,6 +4,7 @@ from uncompyle6.scanners.tok import Token
def for_block_invalid(self, lhs, n, rule, tree, tokens, first: int, last: int) -> bool:
# print("XXX", first, last)
# for t in range(first, last):
# print(tokens[t])
@@ -50,8 +51,8 @@ def for_block_invalid(self, lhs, n, rule, tree, tokens, first: int, last: int) -
pop_jump_index -= 1
# FIXME: something is fishy when and EXTENDED ARG is needed before the
# pop_jump_index instruction to get the argument. In this case, the
# _ifsmtst_jump can jump to a spot beyond the ``come_froms``.
# pop_jump_index instruction to get the argment. In this case, the
# _ifsmtst_jump can jump to a spot beyond the come_froms.
# That is going on in the non-EXTENDED_ARG case is that the POP_JUMP_IF
# jumps to a JUMP_(FORWARD) which is changed into an EXTENDED_ARG POP_JUMP_IF
# to the jumped forwarded address

View File

@@ -44,6 +44,7 @@ IFELSE_STMT_RULES = frozenset(
def ifelsestmt2(self, lhs, n, rule, tree, tokens, first, last):
if (last + 1) < n and tokens[last + 1] == "COME_FROM_LOOP" and lhs != "ifelsestmtc":
# ifelsestmt jumped outside of loop. No good.
return True
@@ -65,7 +66,7 @@ def ifelsestmt2(self, lhs, n, rule, tree, tokens, first, last):
if raise_stmt1 == "raise_stmt1" and raise_stmt1[0] in ("LOAD_ASSERT",):
return True
# Make sure all of the "come_froms" offset at the
# Make sure all of the "come froms" offset at the
# end of the "if" come from somewhere inside the "if".
# Since the come_froms are ordered so that lowest
# offset COME_FROM is last, it is sufficient to test

View File

@@ -4,6 +4,7 @@ from uncompyle6.scanners.tok import Token
def ifstmts_jump(self, lhs, n, rule, ast, tokens, first, last):
if len(rule[1]) <= 1 or not ast:
return False
@@ -23,7 +24,7 @@ def ifstmts_jump(self, lhs, n, rule, ast, tokens, first, last):
pop_jump_index -= 1
# FIXME: something is fishy when and EXTENDED ARG is needed before the
# pop_jump_index instruction to get the argument. In this case, the
# pop_jump_index instruction to get the argment. In this case, the
# _ifsmtst_jump can jump to a spot beyond the come_froms.
# That is going on in the non-EXTENDED_ARG case is that the POP_JUMP_IF
# jumps to a JUMP_(FORWARD) which is changed into an EXTENDED_ARG POP_JUMP_IF
@@ -33,11 +34,16 @@ def ifstmts_jump(self, lhs, n, rule, ast, tokens, first, last):
pop_jump_offset = tokens[pop_jump_index].off2int(prefer_last=False)
if isinstance(come_froms, Token):
if tokens[pop_jump_index].attr < pop_jump_offset and ast[0] != "pass":
if (
tokens[pop_jump_index].attr < pop_jump_offset and ast[0] != "pass"
):
# This is a jump backwards to a loop. All bets are off here when there the
# unless statement is "pass" which has no instructions associated with it.
return False
return come_froms.attr is not None and pop_jump_offset > come_froms.attr
return (
come_froms.attr is not None
and pop_jump_offset > come_froms.attr
)
elif len(come_froms) == 0:
return False

View File

@@ -304,14 +304,41 @@ class Scanner(ABC):
return self.insts[self.offset2inst_index[offset] - 1].offset
def get_inst(self, offset: int):
# Instructions can get moved as a result of EXTENDED_ARGS removal.
# So if "offset" is not in self.offset2inst_index, then
# we assume that it was an instruction moved back.
# We check that assumption though by looking at
# self.code's opcode.
"""
Returns the instruction from ``self.insts`` that has at offset
``offset``.
Instructions can get moved as a result of ``EXTENDED_ARGS`` removal.
So if ``offset`` is not in self.offset2inst_index, then
we assume that it was an instruction moved back.
We check that assumption though by looking at
self.code's opcode.
Sadly instructions can get moved forward too.
So we have to check which direction we are going.
"""
offset_increment = instruction_size(self.opc.EXTENDED_ARG, self.opc)
if offset not in self.offset2inst_index:
offset -= instruction_size(self.opc.EXTENDED_ARG, self.opc)
assert self.code[offset] == self.opc.EXTENDED_ARG
if self.code[offset] != self.opc.EXTENDED_ARG:
target_name = self.opc.opname[self.code[offset]]
# JUMP_ABSOLUTE can be like this where
# the inst offset is at what used to be an EXTENDED_ARG
# so find the first extended arg.
next_offset = offset - offset_increment
while next_offset not in self.offset2inst_index:
next_offset -= offset_increment
assert self.code[next_offset] == self.opc.EXTENDED_ARG
inst = self.insts[self.offset2inst_index[next_offset]]
assert inst.opname == target_name, inst
else:
next_offset = offset + offset_increment
while next_offset not in self.offset2inst_index:
next_offset += offset_increment
inst = self.insts[self.offset2inst_index[next_offset]]
assert inst.has_extended_arg is True
return inst
return self.insts[self.offset2inst_index[offset]]
def get_target(self, offset: int, extended_arg: int = 0) -> int:
@@ -453,7 +480,7 @@ class Scanner(ABC):
pass
pass
pass
if inst.offset >= end:
if isinstance(inst, int) and inst.offset >= end:
break
pass

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2015-2024 by Rocky Bernstein
# Copyright (c) 2015-2023 by Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
#
@@ -36,13 +36,13 @@ Finally we save token information.
from __future__ import print_function
from copy import copy
from sys import intern
from xdis import code2num, instruction_size, iscode, op_has_argument
from xdis import code2num, iscode, op_has_argument, instruction_size
from xdis.bytecode import _get_const_info
from uncompyle6.scanner import Scanner, Token
from sys import intern
class Scanner2(Scanner):
def __init__(self, version, show_asm=None, is_pypy=False):
@@ -206,7 +206,7 @@ class Scanner2(Scanner):
bytecode = self.build_instructions(co)
if show_asm in ("both", "before"):
print("\n# ---- disassembly:")
print("\n# ---- before tokenization:")
bytecode.disassemble_bytes(
co.co_code,
varnames=co.co_varnames,
@@ -235,6 +235,7 @@ class Scanner2(Scanner):
# 'LOAD_ASSERT' is used in assert statements.
self.load_asserts = set()
for i in self.op_range(0, codelen):
# We need to detect the difference between:
# raise AssertionError
# and
@@ -326,14 +327,9 @@ class Scanner2(Scanner):
"BUILD_SET",
):
t = Token(
op_name,
oparg,
pattr,
offset,
op_name, oparg, pattr, offset,
self.linestarts.get(offset, None),
op,
has_arg,
self.opc,
op, has_arg, self.opc
)
collection_type = op_name.split("_")[1]
next_tokens = self.bound_collection_from_tokens(
@@ -494,7 +490,7 @@ class Scanner2(Scanner):
pass
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
print("\n# ---- after tokenization:")
for t in new_tokens:
print(t.format(line_prefix=""))
print()
@@ -544,17 +540,14 @@ class Scanner2(Scanner):
for s in stmt_list:
if code[s] == self.opc.JUMP_ABSOLUTE and s not in pass_stmts:
target = self.get_target(s)
if target > s or (
self.lines and self.lines[last_stmt].l_no == self.lines[s].l_no
):
if target > s or (self.lines and self.lines[last_stmt].l_no == self.lines[s].l_no):
stmts.remove(s)
continue
j = self.prev[s]
while code[j] == self.opc.JUMP_ABSOLUTE:
j = self.prev[j]
if (
self.version >= (2, 3)
and self.opname_for_offset(j) == "LIST_APPEND"
self.version >= (2, 3) and self.opname_for_offset(j) == "LIST_APPEND"
): # list comprehension
stmts.remove(s)
continue
@@ -931,6 +924,7 @@ class Scanner2(Scanner):
# Is it an "and" inside an "if" or "while" block
if op == self.opc.PJIF:
# Search for other POP_JUMP_IF_...'s targeting the
# same target, of the current POP_JUMP_... instruction,
# starting from current offset, and filter everything inside inner 'or'
@@ -1122,6 +1116,7 @@ class Scanner2(Scanner):
# Is this a loop and not an "if" statement?
if (if_end < pre_rtarget) and (pre[if_end] in self.setup_loop_targets):
if if_end > start:
return
else:

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2015-2017, 2021-2022, 2024 by Rocky Bernstein
# Copyright (c) 2015-2017, 2021-2022 by Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
#
@@ -23,31 +23,27 @@ use in deparsing.
"""
import sys
import uncompyle6.scanners.scanner2 as scan
# bytecode verification, verify(), uses JUMP_OPs from here
from xdis import iscode
from xdis.bytecode import _get_const_info
from xdis.opcodes import opcode_26
from xdis.bytecode import _get_const_info
import uncompyle6.scanners.scanner2 as scan
from uncompyle6.scanner import Token
intern = sys.intern
JUMP_OPS = opcode_26.JUMP_OPS
class Scanner26(scan.Scanner2):
def __init__(self, show_asm=False):
super(Scanner26, self).__init__((2, 6), show_asm)
# "setup" opcodes
self.setup_ops = frozenset(
[
self.opc.SETUP_EXCEPT,
self.opc.SETUP_FINALLY,
]
)
self.setup_ops = frozenset([
self.opc.SETUP_EXCEPT, self.opc.SETUP_FINALLY,
])
return
@@ -80,9 +76,8 @@ class Scanner26(scan.Scanner2):
# show_asm = 'after'
if show_asm in ("both", "before"):
print("\n# ---- disassembly:")
for instr in bytecode.get_instructions(co):
print(instr.disassemble(self.opc))
print(instr.disassemble())
# Container for tokens
tokens = []
@@ -101,18 +96,17 @@ class Scanner26(scan.Scanner2):
# 'LOAD_ASSERT' is used in assert statements.
self.load_asserts = set()
for i in self.op_range(0, codelen):
# We need to detect the difference between:
# raise AssertionError
# and
# assert ...
if (
self.code[i] == self.opc.JUMP_IF_TRUE
and i + 4 < codelen
and self.code[i + 3] == self.opc.POP_TOP
and self.code[i + 4] == self.opc.LOAD_GLOBAL
):
if names[self.get_argument(i + 4)] == "AssertionError":
self.load_asserts.add(i + 4)
if (self.code[i] == self.opc.JUMP_IF_TRUE and
i + 4 < codelen and
self.code[i+3] == self.opc.POP_TOP and
self.code[i+4] == self.opc.LOAD_GLOBAL):
if names[self.get_argument(i+4)] == 'AssertionError':
self.load_asserts.add(i+4)
jump_targets = self.find_jump_targets(show_asm)
# contains (code, [addrRefToCode])
@@ -137,8 +131,7 @@ class Scanner26(scan.Scanner2):
i += 1
op = self.code[offset]
op_name = self.opname[op]
oparg = None
pattr = None
oparg = None; pattr = None
if offset in jump_targets:
jump_idx = 0
@@ -149,37 +142,28 @@ class Scanner26(scan.Scanner2):
# properly. For example, a "loop" with an "if" nested in it should have the
# "loop" tag last so the grammar rule matches that properly.
last_jump_offset = -1
for jump_offset in sorted(jump_targets[offset], reverse=True):
for jump_offset in sorted(jump_targets[offset], reverse=True):
if jump_offset != last_jump_offset:
tokens.append(
Token(
"COME_FROM",
jump_offset,
repr(jump_offset),
offset="%s_%d" % (offset, jump_idx),
has_arg=True,
)
)
tokens.append(Token(
'COME_FROM', jump_offset, repr(jump_offset),
offset="%s_%d" % (offset, jump_idx),
has_arg = True))
jump_idx += 1
last_jump_offset = jump_offset
elif offset in self.thens:
tokens.append(
Token(
"THEN",
None,
self.thens[offset],
offset="%s_0" % offset,
has_arg=True,
)
)
tokens.append(Token(
'THEN', None, self.thens[offset],
offset="%s_0" % offset,
has_arg = True))
has_arg = op >= self.opc.HAVE_ARGUMENT
has_arg = (op >= self.opc.HAVE_ARGUMENT)
if has_arg:
oparg = self.get_argument(offset) + extended_arg
extended_arg = 0
if op == self.opc.EXTENDED_ARG:
extended_arg += self.extended_arg_val(oparg)
continue
extended_arg += self.extended_arg_val(oparg)
continue
# Note: name used to match on rather than op since
# BUILD_SET isn't in earlier Pythons.
@@ -188,14 +172,7 @@ class Scanner26(scan.Scanner2):
"BUILD_SET",
):
t = Token(
op_name,
oparg,
pattr,
offset,
self.linestarts.get(offset, None),
op,
has_arg,
self.opc,
op_name, oparg, pattr, offset, self.linestarts.get(offset, None), op, has_arg, self.opc
)
collection_type = op_name.split("_")[1]
@@ -244,8 +221,8 @@ class Scanner26(scan.Scanner2):
# FIXME: this is a hack to catch stuff like:
# if x: continue
# the "continue" is not on a new line.
if len(tokens) and tokens[-1].kind == "JUMP_BACK":
tokens[-1].kind = intern("CONTINUE")
if len(tokens) and tokens[-1].kind == 'JUMP_BACK':
tokens[-1].kind = intern('CONTINUE')
elif op in self.opc.JABS_OPS:
pattr = repr(oparg)
@@ -263,23 +240,17 @@ class Scanner26(scan.Scanner2):
# CE - Hack for >= 2.5
# Now all values loaded via LOAD_CLOSURE are packed into
# a tuple before calling MAKE_CLOSURE.
if (
self.version >= (2, 5)
and op == self.opc.BUILD_TUPLE
and self.code[self.prev[offset]] == self.opc.LOAD_CLOSURE
):
if (self.version >= (2, 5) and op == self.opc.BUILD_TUPLE and
self.code[self.prev[offset]] == self.opc.LOAD_CLOSURE):
continue
else:
op_name = "%s_%d" % (op_name, oparg)
op_name = '%s_%d' % (op_name, oparg)
customize[op_name] = oparg
elif self.version > (2, 0) and op == self.opc.CONTINUE_LOOP:
customize[op_name] = 0
elif (
op_name
in """
elif op_name in """
CONTINUE_LOOP EXEC_STMT LOAD_LISTCOMP LOAD_SETCOMP
""".split()
):
""".split():
customize[op_name] = 0
elif op == self.opc.JUMP_ABSOLUTE:
# Further classify JUMP_ABSOLUTE into backward jumps
@@ -295,24 +266,23 @@ class Scanner26(scan.Scanner2):
# rule for that.
target = self.get_target(offset)
if target <= offset:
op_name = "JUMP_BACK"
if offset in self.stmts and self.code[offset + 3] not in (
self.opc.END_FINALLY,
self.opc.POP_BLOCK,
):
if (
offset in self.linestarts and tokens[-1].kind == "JUMP_BACK"
) or offset not in self.not_continue:
op_name = "CONTINUE"
op_name = 'JUMP_BACK'
if (offset in self.stmts
and self.code[offset+3] not in (self.opc.END_FINALLY,
self.opc.POP_BLOCK)):
if ((offset in self.linestarts and
tokens[-1].kind == 'JUMP_BACK')
or offset not in self.not_continue):
op_name = 'CONTINUE'
else:
# FIXME: this is a hack to catch stuff like:
# if x: continue
# the "continue" is not on a new line.
if tokens[-1].kind == "JUMP_BACK":
if tokens[-1].kind == 'JUMP_BACK':
# We need 'intern' since we have
# already have processed the previous
# token.
tokens[-1].kind = intern("CONTINUE")
tokens[-1].kind = intern('CONTINUE')
elif op == self.opc.LOAD_GLOBAL:
if offset in self.load_asserts:
@@ -346,7 +316,6 @@ class Scanner26(scan.Scanner2):
pass
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
for t in tokens:
print(t.format(line_prefix=""))
print()

View File

@@ -418,7 +418,7 @@ class Scanner3(Scanner):
# show_asm = 'both'
if show_asm in ("both", "before"):
print("\n# ---- disassembly:")
print("\n# ---- before tokenization:")
bytecode.disassemble_bytes(
co.co_code,
varnames=co.co_varnames,
@@ -788,7 +788,7 @@ class Scanner3(Scanner):
pass
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
print("\n# ---- after tokenization:")
for t in new_tokens:
print(t.format(line_prefix=""))
print()

View File

@@ -54,13 +54,8 @@ class Scanner37Base(Scanner):
super(Scanner37Base, self).__init__(version, show_asm, is_pypy)
self.offset2tok_index = None
self.debug = debug
# True is code is from PyPy
self.is_pypy = is_pypy
# Bytecode converted into instruction
self.insts = []
# Create opcode classification sets
# Note: super initialization above initializes self.opc
@@ -227,7 +222,7 @@ class Scanner37Base(Scanner):
bytecode = self.build_instructions(co)
if show_asm in ("both", "before"):
print("\n# ---- disassembly:")
print("\n# ---- before tokenization:")
self.insts = bytecode.disassemble_bytes(
co.co_code,
varnames=co.co_varnames,
@@ -271,9 +266,10 @@ class Scanner37Base(Scanner):
if (
next_inst.opname == "LOAD_GLOBAL"
and next_inst.argval == "AssertionError"
and inst.argval is not None
and inst.argval
):
raise_inst = self.get_inst(self.prev_op[inst.argval])
raise_idx = self.get_inst(self.prev_op[inst.argval])
raise_inst = self.insts[raise_idx]
if raise_inst.opname.startswith("RAISE_VARARGS"):
self.load_asserts.add(next_inst.offset)
pass
@@ -539,7 +535,7 @@ class Scanner37Base(Scanner):
pass
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
print("\n# ---- after tokenization:")
for t in tokens:
print(t.format(line_prefix=""))
print()

View File

@@ -272,6 +272,8 @@ TABLE_DIRECT = {
(2, NO_PARENTHESIS_EVER)
),
"IMPORT_FROM": ("%{pattr}",),
"IMPORT_NAME_ATTR": ("%{pattr}",),
"attribute": ("%c.%[1]{pattr}", (0, "expr")),
"delete_subscript": (
"%|del %p[%c]\n",
@@ -378,46 +380,6 @@ TABLE_DIRECT = {
(0, PRECEDENCE["named_expr"]-1)),
"break": ("%|break\n",),
"continue": ("%|continue\n",),
"except": ("%|except:\n%+%c%-", 3),
"except_cond1": ("%|except %c:\n", 1),
"except_cond2": ("%|except %c as %c:\n", (1, "expr"), (5, "store")),
"except_suite": ("%+%c%-%C", 0, (1, maxint, "")),
# In Python 3.6+, this is more complicated in the presence of "returns"
"except_suite_finalize": ("%+%c%-%C", 1, (3, maxint, "")),
"expr_stmt": (
"%|%p\n",
# When a statement contains only a named_expr (:=)
# the named_expr should have parenthesis around it.
(0, "expr", PRECEDENCE["named_expr"] - 1)
),
# Note: Python 3.8+ changes this
"for": ("%|for %c in %c:\n%+%c%-\n\n", (3, "store"), (1, "expr"), (4, "for_block")),
"forelsestmt": (
"%|for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n",
(3, "store"),
(1, "expr"),
(4, "for_block"),
-2,
),
"forelselaststmt": (
"%|for %c in %c:\n%+%c%-%|else:\n%+%c%-",
(3, "store"),
(1, "expr"),
(4, "for_block"),
-2,
),
"forelselaststmtl": (
"%|for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n",
(3, "store"),
(1, "expr"),
(4, "for_block"),
-2,
),
"raise_stmt0": ("%|raise\n",),
"raise_stmt1": ("%|raise %c\n", 0),
"raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2),
@@ -459,7 +421,63 @@ TABLE_DIRECT = {
1,
3,
), # has COME_FROM
"whileTruestmt": ("%|while True:\n%+%c%-\n\n", 1),
"whilestmt": ("%|while %c:\n%+%c%-\n\n", 1, 2),
"while1stmt": ("%|while 1:\n%+%c%-\n\n", 1),
"while1elsestmt": ("%|while 1:\n%+%c%-%|else:\n%+%c%-\n\n", 1, -2),
"whileelsestmt": ("%|while %c:\n%+%c%-%|else:\n%+%c%-\n\n", 1, 2, -2),
"whileelsestmt2": ("%|while %c:\n%+%c%-%|else:\n%+%c%-\n\n", 1, 2, -3),
"whileelselaststmt": ("%|while %c:\n%+%c%-%|else:\n%+%c%-", 1, 2, -2),
"expr_stmt": (
"%|%p\n",
# When a statement contains only a named_expr (:=)
# the named_expr should have parenthesis around it.
(0, "expr", PRECEDENCE["named_expr"] - 1)
),
# Note: Python 3.8+ changes this
"for": ("%|for %c in %c:\n%+%c%-\n\n", (3, "store"), (1, "expr"), (4, "for_block")),
"forelsestmt": (
"%|for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n",
(3, "store"),
(1, "expr"),
(4, "for_block"),
-2,
),
"forelselaststmt": (
"%|for %c in %c:\n%+%c%-%|else:\n%+%c%-",
(3, "store"),
(1, "expr"),
(4, "for_block"),
-2,
),
"forelselaststmtl": (
"%|for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n",
(3, "store"),
(1, "expr"),
(4, "for_block"),
-2,
),
"try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3),
"tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4),
"tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
"tryelsestmtl": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
# Note: this is generated generated by grammar rules but in this phase.
"tf_try_except": ("%c%-%c%+", 1, 3),
"tf_tryelsestmt": ("%c%-%c%|else:\n%+%c", 1, 3, 4),
"tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5),
"except": ("%|except:\n%+%c%-", 3),
"except_cond1": ("%|except %c:\n", 1),
"except_cond2": ("%|except %c as %c:\n", (1, "expr"), (5, "store")),
"except_suite": ("%+%c%-%C", 0, (1, maxint, "")),
# In Python 3.6+, this is more complicated in the presence of "returns"
"except_suite_finalize": ("%+%c%-%C", 1, (3, maxint, "")),
"pass": ("%|pass\n",),
"STORE_FAST": ("%{pattr}",),
"kv": ("%c: %c", 3, 1),
"kv2": ("%c: %c", 1, 2),
"import": ("%|import %c\n", 2),
"importlist": ("%C", (0, maxint, ", ")),
@@ -473,36 +491,6 @@ TABLE_DIRECT = {
"import_from_star": (
"%|from %[2]{pattr} import *\n",
),
"kv": ("%c: %c", 3, 1),
"kv2": ("%c: %c", 1, 2),
"pass": ("%|pass\n",),
"whileTruestmt": ("%|while True:\n%+%c%-\n\n", 1),
"whilestmt": ("%|while %c:\n%+%c%-\n\n", 1, 2),
"while1stmt": ("%|while 1:\n%+%c%-\n\n", 1),
"while1elsestmt": ("%|while 1:\n%+%c%-%|else:\n%+%c%-\n\n", 1, -2),
"whileelsestmt": ("%|while %c:\n%+%c%-%|else:\n%+%c%-\n\n", 1, 2, -2),
"whileelsestmt2": ("%|while %c:\n%+%c%-%|else:\n%+%c%-\n\n", 1, 2, -3),
"whileelselaststmt": ("%|while %c:\n%+%c%-%|else:\n%+%c%-", 1, 2, -2),
# If there are situations where we need "with ... as ()"
# We may need to customize this in n_with_as
"with_as": (
"%|with %c as %c:\n%+%c%-",
(0, "expr"),
(2, "store"),
(3, ("suite_stmts_opt", "suite_stmts")),
),
"try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3),
"tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4),
"tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
"tryelsestmtl": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
# Note: this is generated generated by grammar rules but in this phase.
"tf_try_except": ("%c%-%c%+", 1, 3),
"tf_tryelsestmt": ("%c%-%c%|else:\n%+%c", 1, 3, 4),
"tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5),
}

View File

@@ -17,24 +17,23 @@
from uncompyle6.semantics.consts import TABLE_DIRECT
#######################
# Python 2.5+ Changes #
#######################
def customize_for_version25(self, version):
########################
# Import style for 2.5+
########################
TABLE_DIRECT.update(
{
"importmultiple": ("%|import %c%c\n", 2, 3),
"import_cont": (", %c", 2),
# With/as is allowed as "from future" thing in 2.5
# Note: It is safe to put the variables after "as" in parenthesis,
# and sometimes it is needed.
"with": ("%|with %c:\n%+%c%-", 0, 3),
}
)
TABLE_DIRECT.update({
'importmultiple': ( '%|import %c%c\n', 2, 3 ),
'import_cont' : ( ', %c', 2 ),
# With/as is allowed as "from future" thing in 2.5
# Note: It is safe to put the variables after "as" in parenthesis,
# and sometimes it is needed.
'with': ( '%|with %c:\n%+%c%-', 0, 3),
'withasstmt': ( '%|with %c as (%c):\n%+%c%-', 0, 2, 3),
})
# In 2.5+ "except" handlers and the "finally" can appear in one
# "try" statement. So the below has the effect of combining the
@@ -42,18 +41,16 @@ def customize_for_version25(self, version):
# FIXME: something doesn't smell right, since the semantics
# are different. See test_fileio.py for an example that shows this.
def tryfinallystmt(node):
if len(node[1][0]) == 1 and node[1][0][0] == "stmt":
if node[1][0][0][0] == "try_except":
node[1][0][0][0].kind = "tf_try_except"
if node[1][0][0][0] == "tryelsestmt":
node[1][0][0][0].kind = "tf_tryelsestmt"
if len(node[1][0]) == 1 and node[1][0][0] == 'stmt':
if node[1][0][0][0] == 'try_except':
node[1][0][0][0].kind = 'tf_try_except'
if node[1][0][0][0] == 'tryelsestmt':
node[1][0][0][0].kind = 'tf_tryelsestmt'
self.default(node)
self.n_tryfinallystmt = tryfinallystmt
def n_import_from(node):
if node[0].pattr > 0:
node[2].pattr = ("." * node[0].pattr) + node[2].pattr
self.default(node)
self.n_import_from = n_import_from

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2018-2021, 2023-2024 by Rocky Bernstein
# Copyright (c) 2018-2021, 2023 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -51,6 +51,7 @@ def customize_for_version3(self, version):
"tf_tryelsestmtl3": ("%c%-%c%|else:\n%+%c", 1, 3, 5),
"store_locals": ("%|# inspect.currentframe().f_locals = __locals__\n",),
"with": ("%|with %c:\n%+%c%-", 0, 3),
"withasstmt": ("%|with %c as (%c):\n%+%c%-", 0, 2, 3),
}
)
@@ -138,10 +139,14 @@ def customize_for_version3(self, version):
# Python 3.2 works like this
subclass_code = find_code_node(load_closure, -2).attr
else:
raise RuntimeError("Internal Error n_classdef: cannot find class body")
subclass_info = build_class
raise "Internal Error n_classdef: cannot find class body"
if hasattr(build_class[3], "__len__"):
if not subclass_info:
subclass_info = build_class[3]
elif hasattr(build_class[2], "__len__"):
subclass_info = build_class[2]
else:
raise "Internal Error n_classdef: cannot superclass name"
elif not subclass_info:
if mkfunc[0] in ("no_kwargs", "kwargs"):
subclass_code = mkfunc[1].attr

View File

@@ -23,8 +23,8 @@ from uncompyle6.semantics.consts import PRECEDENCE, TABLE_DIRECT
from uncompyle6.semantics.customize37 import FSTRING_CONVERSION_MAP
from uncompyle6.semantics.helper import escape_string, strip_quotes
def customize_for_version38(self, version):
# FIXME: pytest doesn't add proper keys in testing. Reinstate after we have fixed pytest.
# for lhs in 'for forelsestmt forelselaststmt '
# 'forelselaststmtc tryfinally38'.split():
@@ -40,10 +40,10 @@ def customize_for_version38(self, version):
),
"async_forelse_stmt38": (
"%|async for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n",
(7, "store"),
(0, "expr"),
(8, "for_block"),
(-1, "else_suite"),
(7, 'store'),
(0, 'expr'),
(8, 'for_block'),
(-1, 'else_suite')
),
"async_with_stmt38": (
"%|async with %c:\n%+%c%-\n",
@@ -70,15 +70,8 @@ def customize_for_version38(self, version):
),
# Python 3.8 reverses the order of keys and items
# from all prior versions of Python.
"dict_comp_body": (
"%c: %c",
(0, "expr"),
(1, "expr"),
),
"except_cond1a": (
"%|except %c:\n",
(1, "expr"),
),
"dict_comp_body": ("%c: %c", (0, "expr"), (1, "expr"),),
"except_cond1a": ("%|except %c:\n", (1, "expr"),),
"except_cond_as": (
"%|except %c as %c:\n",
(1, "expr"),
@@ -128,19 +121,10 @@ def customize_for_version38(self, version):
-2,
),
"ifpoplaststmtc": ("%|if %c:\n%+%c%-", (0, "testexpr"), (2, "l_stmts")),
"named_expr": ( # AKA "walrus operator"
"%c := %p",
(2, "store"),
(0, "expr", PRECEDENCE["named_expr"] - 1),
),
"pop_return": ("%|return %c\n", (1, "return_expr")),
"popb_return": ("%|return %c\n", (0, "return_expr")),
"pop_ex_return": ("%|return %c\n", (0, "return_expr")),
"set_for": (
" for %c in %c",
(2, "store"),
(0, "expr_or_arg"),
),
"set_for": (" for %c in %c", (2, "store"), (0, "expr_or_arg"),),
"whilestmt38": (
"%|while %c:\n%+%c%-\n\n",
(1, ("bool_op", "testexpr", "testexprc")),
@@ -227,11 +211,10 @@ def customize_for_version38(self, version):
(2, "suite_stmts_opt"),
(8, "suite_stmts_opt"),
),
"with_as_pass": (
"%|with %c as %c:\n%+%c%-",
(0, "expr"),
"named_expr": ( # AKA "walrus operator"
"%c := %p",
(2, "store"),
(3, "pass"),
(0, "expr", PRECEDENCE["named_expr"] - 1),
),
}
)

View File

@@ -471,7 +471,7 @@ class ComprehensionMixin:
assert store, "Couldn't find store in list/set comprehension"
# A problem created with later Python code generation is that there
# is a lambda set up with a dummy argument name that is then called
# is a lamda set up with a dummy argument name that is then called
# So we can't just translate that as is but need to replace the
# dummy name. Below we are picking out the variable name as seen
# in the code. And trying to generate code for the other parts

View File

@@ -43,7 +43,7 @@ def make_function2(self, node, is_lambda, nested=1, code_node=None):
- handle defaults
- handle format tuple parameters
"""
# if formal parameter is a tuple, the parameter name
# if formal parameter is a tuple, the paramater name
# starts with a dot (eg. '.1', '.2')
if name.startswith("."):
# replace the name with the tuple-string

View File

@@ -311,22 +311,21 @@ class TreeTransform(GenericASTTraversal, object):
n = n[0][0]
elif len_n == 0:
return node
if n[0].kind in ("lastc_stmt", "lastl_stmt"):
elif n[0].kind in ("lastc_stmt", "lastl_stmt"):
n = n[0]
if n[0].kind in (
"ifstmt",
"iflaststmt",
"iflaststmtl",
"ifelsestmtl",
"ifelsestmtc",
"ifpoplaststmtl",
):
n = n[0]
if n.kind == "ifpoplaststmtl":
old_stmts = n[2]
else_suite_index = 2
if n[0].kind in (
"ifstmt",
"iflaststmt",
"iflaststmtl",
"ifelsestmtl",
"ifelsestmtc",
"ifpoplaststmtl",
):
n = n[0]
if n.kind == "ifpoplaststmtl":
old_stmts = n[2]
else_suite_index = 2
pass
pass
else:
if (

View File

@@ -14,4 +14,4 @@
# This file is suitable for sourcing inside POSIX shell as
# well as importing into Python
# fmt: off
__version__="3.9.1" # noqa
__version__="3.9.1.dev0" # noqa