Compare commits

..

1 Commits

Author SHA1 Message Date
rocky
5b738cdbe1 Not working: sync with decompyle3 2024-03-02 04:22:32 -05:00
108 changed files with 1047 additions and 2041 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

@@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [macOS]
python-version: [3.8]
python-version: [3.7, 3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
@@ -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

@@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
python-version: [3.7, 3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
@@ -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

@@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [windows]
python-version: [3.8]
python-version: [3.7, 3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
@@ -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

View File

@@ -19,17 +19,17 @@
TL;DR (too long; didn't read)
* Don't do something illegal. And don't ask me to do something illegal or help you do something illegal.
* We already have an infinite supply of decompilation bugs that need fixing, and an automated mechanism for finding more. Decompilation bugs get addressed by easiness to fix and by whim. If you expect yours to be fixed ahead of those, you need to justify why. You can ask for a hand-assisted decompilation, but that is expensive and beyond what most are willing to spend. A $100 fee is needed just to look at the bytecode.
* Don't do something illegal. And don't ask me to do something illegal or help you do something illegal
* We already have an infinite supply of decompilation bugs that need fixing, and an automated mechanism for finding more. Decompilation bugs get addressed by easiness to fix and by whim. If you expect yours to be fixed ahead of those, you need to justify why.
* When asking for help, you may be asked for what you've tried on your own first. There are plenty of sources of information about this code.
* Bugs get fixed, slowly. Sometimes on the order of months or years. If you are looking for *timely* help or support, that is typically known as a _paid_ service.
* Submitting a bug or issue report that is likely to get acted upon may require a bit of effort on your part to make it easy for the problem solver. If you are not willing to do that, please don't waste your or our time. Bug report may be closed with about as much thought and care as apparent in the effort to create the bug. Supporting the project however, does increase the likelihood of your issue getting noticed and acted upon.
* If you are looking for *timely* help or support, well, that is typically known as a _paid_ service. I don't really have a mechanism for that since I have a full-time job. But supporting the project is an approximation.
* Submitting a bug or issue report that is likely to get acted upon may require a bit of effort on your part to make it easy for the problem solver. If you are not willing to do that, please don't waste our time. As indicated above, supporting the project will increase the likelihood of your issue getting noticed and acted upon.
# Ethics
Do not use this program for unethical or illegal purposes. More detestable, at least to me, is asking for help to assist you in something that might not legitimate.
I do not condone using this program for unethical or illegal purposes. More detestable, at least to me, is asking for help to assist you in something that might not legitimate.
Don't use the issue tracker for such unethical or illegal solicitations. To try to stave off illegitimate behavior, you should note that the issue tracker, the code, and bugs mentioned in that are in the open: there is no
Don't use the issue tracker for such solicitations. To try to stave off illegitimate behavior, you should note that the issue tracker, the code, and bugs mentioned in that are in the open: there is no
confidentiality. You may be asked about the authorship or claimed ownership of the bytecode. If I think something is not quite right, I may label the issue questionable which may make the it easier those who are looking for illegal activity.
@@ -37,13 +37,13 @@ confidentiality. You may be asked about the authorship or claimed ownership of t
For many open-source projects bugs where the expectation is that bugs are rare, reporting bugs in a *thoughtful* way can be helpful. See also [How to Ask Questions the Smart Way](http://www.catb.org/~esr/faqs/smart-questions.html).
In this project though, most of the bug reports boil down to the something like: I am trying to reverse engineer some code that I am not the author/owner and that person doesn't want me to have access to. I am hitting a problem somewhere along the line which might have to do with decompilation. But it could be something else like how the bytecode was extracted, some problem in deliberately obfuscated code, or the use some kind of Python bytecode version that isn't supported by the decompiler. Gee this stuff is complicated, here's an open source project, so maybe someone there will help me figure stuff out.
In this project though, most of the bug reports boil down to the something like: I am trying to reverse engineer some code that I am not the author/owner and that person doesn't want me to have access to. I am hitting a problem somewhere along the line which might have to do with decompilation, but it could be something else like how the bytecode was extracted, some problem in deliberately obfuscated code, or the use some kind of Python bytecode version that isn't supported by the decompiler.
While you are free to report bugs, unless you sponsor the project, I may close them with about the same amount of effort spent that I think was used to open the report for them. And if you spent a considerable amount of time to create the bug report but didn't follow instructions given here and in the issue template, I am sorry in advance. Just go back, read, and follow instructions.
While you are free to report these, unless you sponsor the project, I may close them with about the same amount of effort spent that I think was used to open the report for them. And if you spent a considerable amount of time to create the bug report but didn't follow instructions given here and in the issue template, I am sorry in advance. Just go back, read, and follow instructions.
This project already has an infinite supply of bugs that have been narrowed to the most minimal form and where I have source code to compare against. And in the unlikely event this supply runs out, I have automated means for generating *another* infinite supply.
The task of justifying why addressing your bug is of use to the community, and why it should be prioritized over the others, is the bug reporter's responsibility.
In this project the task of justifying why addressing your bug is of use to the community, and why it should be prioritized over the others, is the bug reporter's responsibility.
While in the abstract, I have no problem answering questions about how to read a Python traceback or install Python software, or trying to understand what is going wrong in your particular setup, I am not a paid support person and there other things I'd rather be doing with my limited volunteer time. So save us both time, effort, and aggravation: use other avenues like StackOverflow. Again, justifying why you should receive unpaid help is the help requester's responsibility.

26
NEWS.md
View File

@@ -1,29 +1,3 @@
3.9.2: 2024-07-21
=================
- track xdis API changes
- Bug fixes and lint
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

@@ -162,7 +162,7 @@ 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
`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
@@ -265,7 +265,7 @@ 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*. Any volunteers?
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
@@ -274,10 +274,7 @@ follow the `instructions when opening an issue <https://github.com/rocky/python-
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. In
rare situtations, I can do a hand decompilation of bytecode for a fee.
However this is expansive, usually beyond what most people are willing
to spend.
issues above the queue of other things I might be doing instead.
See Also
--------

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",
@@ -79,7 +77,7 @@ entry_points = {
]
}
ftp_url = None
install_requires = ["click", "spark-parser >= 1.8.9, < 1.9.0", "xdis >= 6.1.1, < 6.2.0"]
install_requires = ["click", "spark-parser >= 1.8.9, < 1.9.0", "xdis >= 6.0.8, < 6.2.0"]
license = "GPL3"
mailing_list = "python-debugger@googlegroups.com"

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>=71.0.3",
]
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.1.0, < 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

@@ -6,4 +6,4 @@ pytest
Click~=7.0
xdis>=6.0.4
configobj~=5.0.6
setuptools~=71.0.3
setuptools~=65.5.1

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
import setuptools
import sys
"""Setup script for the 'uncompyle6' distribution."""
from setuptools import setup
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)
setup(packages=["uncompyle6"])
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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -29,7 +29,6 @@ storage.
#------------------------------------------------------------------------
import sys
absolute_import = (sys.version_info[0] >= 3)
if absolute_import :
# Because this syntaxis is not valid before Python 2.5
@@ -230,7 +229,7 @@ class DBShelf(MutableMapping):
def associate(self, secondaryDB, callback, flags=0):
def _shelf_callback(priKey, priData, realCallback=callback):
# Safe in Python 2.x because expression short circuit
# Safe in Python 2.x because expresion short circuit
if sys.version_info[0] < 3 or isinstance(priData, bytes) :
data = cPickle.loads(priData)
else :
@@ -367,7 +366,7 @@ class DBShelfCursor:
return None
else:
key, data = rec
# Safe in Python 2.x because expression short circuit
# Safe in Python 2.x because expresion short circuit
if sys.version_info[0] < 3 or isinstance(data, bytes) :
return key, cPickle.loads(data)
else :

View File

@@ -1,18 +0,0 @@
# 2.5 Bug is from nose/plugins/cover.py
def wantFile(self, file, package=None):
if self.coverInclusive:
if file.endswith(".py"):
if package and self.coverPackages:
for want in self.coverPackages:
if package.startswith(want):
return True
else:
return True
return None
# 2.5 bug is from nose/plugins/doctests.py
def wantFile2(self, file):
if self and (self.conf or [exc.search(file) for exc in self.conf]):
return True
return None

View File

@@ -1 +0,0 @@
/.python-version

View File

@@ -3,10 +3,10 @@
# Grammar allows multiple adjacent 'if's in listcomps and genexps,
# even though it's silly. Make sure it works (ifelse broke this.)
[x for x in range(10) if x % 2 if x % 3]
[ x for x in range(10) if x % 2 if x % 3 ]
list(x for x in range(10) if x % 2 if x % 3)
# expression which evaluates True unconditionally,
# expresion which evaluates True unconditionally,
# but leave dead code or junk around that we have to match on.
# Tests "if_exp_true" rule
5 if 1 else 2

View File

@@ -1,34 +0,0 @@
# Bug portion of Issue #405 https://github.com/rocky/python-uncompyle6/issues/405
# Bug was detecting if/else as the last item in a "try: .. except" block.
class Saveframe(object):
"""A saveframe. Use the classmethod from_scratch to create one."""
frame_list = {}
def frame_dict(self):
return
# Next line is 1477
def __setitem__(self, key, item):
# Next line is 1481
if isinstance(item, Saveframe):
try:
self.frame_list[key] = item
except TypeError:
if key in (self.frame_dict()):
dict((frame.name, frame) for frame in self.frame_list)
for pos, frame in enumerate(self.frame_list):
if frame.name == key:
self.frame_list[pos] = item
else:
raise KeyError(
"Saveframe with name '%s' does not exist and "
"therefore cannot be written to. Use the add_saveframe method to add new saveframes."
% key
)
# Next line is 1498
raise ValueError("You can only assign an entry to a saveframe splice.")
x = Saveframe()
x.__setitem__("foo", 5)

View File

@@ -1,8 +1,8 @@
# From 2.7.17 test_bdb.py
# The problem was detecting a docstring at the beginning of the module
# The problem was detecting a docstring at the begining of the module
# It must be detected and change'd or else the "from __future__" below
# is invalid.
# Note that this has to be compiled with optimization < 2 or else optimization
# Note that this has to be compiled with optimation < 2 or else optimization
# will remove the docstring
"""Rational, infinite-precision, real numbers."""

View File

@@ -1,20 +1,20 @@
# Statements to beef up grammar coverage rules
# Force "inplace" ops
# Note this is like simple_source/bug22/01_ops.py
# But we don't have the UNARY_CONVERT which dropped
# But we don't ahve the UNARY_CONVERT which dropped
# out around 2.7
y = +10 # UNARY_POSITIVE
y /= 1 # INPLACE_DIVIDE
y %= 4 # INPLACE_MODULO
y /= 1 # INPLACE_DIVIDE
y %= 4 # INPLACE_MODULO
y **= 1 # INPLACE POWER
y >>= 2 # INPLACE_RSHIFT
y <<= 2 # INPLACE_LSHIFT
y //= 1 # INPLACE_TRUE_DIVIDE
y &= 1 # INPLACE_AND
y ^= 1 # INPLACE_XOR
y &= 1 # INPLACE_AND
y ^= 1 # INPLACE_XOR
# Beef up aug_assign and STORE_SLICE+3
x = [1, 2, 3, 4, 5]
x = [1,2,3,4,5]
x[0:1] = 1
x[0:3] += 1, 2, 3

View File

@@ -1,20 +1,18 @@
# From 3.x test_audiop.py
# Bug is handling default value after * argument in a lambda.
# That's a mouthful of description; I am not sure if the really
# That's a mouthful of desciption; I am not sure if the really
# hacky fix to the code is even correct.
#
# FIXME: try and test with more than one default argument.
# RUNNABLE
def pack(width, data):
return (width, data)
packs = {w: (lambda *data, width=w: pack(width, data)) for w in (1, 2, 4)}
assert packs[1]("a") == (1, ("a",))
assert packs[2]("b") == (2, ("b",))
assert packs[4]("c") == (4, ("c",))
assert packs[1]('a') == (1, ('a',))
assert packs[2]('b') == (2, ('b',))
assert packs[4]('c') == (4, ('c',))

View File

@@ -1,19 +1,16 @@
# From python 3.3.7 trace
# Bug was not having not having semantic rule for conditional not
# RUNNABLE!
def init(modules=None):
mods = set() if not modules else set(modules)
return mods
assert init() == set()
assert init([1, 2, 3]) == set([1, 2, 3])
# From 3.6 sre_parse
# Bug was in handling multiple COME_FROMS from nested if's
# Bug was in handling multple COME_FROMS from nested if's
def _escape(a, b, c, d, e):
if a:
if b:
@@ -27,16 +24,15 @@ def _escape(a, b, c, d, e):
return
raise
assert _escape(False, True, True, True, True) is None
assert _escape(True, True, True, False, True) is None
assert _escape(True, True, False, False, True) is None
assert _escape(False, True, True, True, True) is None
assert _escape(True, True, True, False, True) is None
assert _escape(True, True, False, False, True) is None
for args in (
(True, True, True, False, True),
(True, False, True, True, True),
(True, False, True, True, False),
):
(True, True, True, False, True),
(True, False, True, True, True),
(True, False, True, True, False),
):
try:
_escape(*args)
assert False, args

View File

@@ -1,4 +0,0 @@
# Next line is 1164
def foo():
name = "bar"
lambda x: compile(x, "<register %s's commit>" % name, "exec") if x else None

View File

@@ -1,10 +0,0 @@
# Adapted 3.5 from _bootstrap_external.py
def spec_from_file_location(loader, location):
if loader:
for _ in __file__:
if location:
break
else:
return None

View File

@@ -1,9 +1,8 @@
# From Python 3.4 asynchat.py
# Tests presence or absence of
# Tests presence or absense of
# SETUP_LOOP testexpr return_stmts POP_BLOCK COME_FROM_LOOP
# Note: that there is no JUMP_BACK because of the return_stmts.
def initiate_send(a, b, c, num_sent):
while a and b:
try:
@@ -25,7 +24,6 @@ def initiate_send2(a, b):
return 2
assert initiate_send(1, 1, 2, False) == 1
assert initiate_send(1, 2, 3, False) == 3
assert initiate_send(1, 2, 3, True) == 2

View File

@@ -1,20 +1,13 @@
# Python 3.6 changes, yet again, the way default pairs are handled
# Python 3.6 changes, yet again, the way deafult pairs are handled
def foo1(bar, baz=1):
return 1
def foo2(bar, baz, qux=1):
return 2
def foo3(bar, baz=1, qux=2):
return 3
def foo4(bar, baz, qux=1, quux=2):
return 4
# From 3.6 compileall.
# Bug was in omitting default which when used in an "if"
# are treated as False would be

View File

@@ -1,23 +1,17 @@
# From 3.6 test_abc.py
# Bug was Receiver() class definition
# Bug was Reciever() class definition
import abc
import unittest
class TestABCWithInitSubclass(unittest.TestCase):
def test_works_with_init_subclass(self):
class ReceivesClassKwargs:
def __init_subclass__(cls, **kwargs):
super().__init_subclass__()
class Receiver(ReceivesClassKwargs, abc.ABC, x=1, y=2, z=3):
pass
def test_abstractmethod_integration(self):
for abstractthing in [abc.abstractmethod]:
class C(metaclass=abc.ABCMeta):
@abstractthing
def foo(self):
pass # abstract
def foo(self): pass # abstract

View File

@@ -1,12 +1,12 @@
# From 3.6 base64.py
# Bug was handling "and" condition in the presence of POP_JUMP_IF_FALSE
# Bug was handling "and" condition in the presense of POP_JUMP_IF_FALSE
# locations
def _85encode(foldnuls, words):
return ["z" if foldnuls and word else "y" for word in words]
return ['z' if foldnuls and word
else 'y'
for word in words]
# From Python 3.6 enum.py
def __new__(metacls, cls, bases, classdict):
{k: classdict[k] for k in classdict._member_names}

View File

@@ -1,74 +0,0 @@
# From https://github.com/rocky/python-uncompyle6/issues/420
# Related to EXTENDED_ARG in whilestmt
ERRPR_CODE_DEFINE = {} # Remove this and things works
try:
print()
except Exception:
var1 = 0
var2 = 1
if var1 or var2:
times = 1
while times != False and self.scanner.is_open():
try:
try:
print()
except Exception:
print()
out = 0
count = 1
if out == 1:
break
elif out == 2:
count += 1
if times == 3:
self.func.emit({})
break
else:
continue
if out == 3 or out == b"":
if self.times == 3:
break
count += 1
if count == 3:
count = 0
if out == 4:
self.func.emit(ERRPR_CODE_DEFINE.ReceiedError())
else:
print()
break
continue
else:
count = 0
except Exception:
print("upper exception")
else:
try:
print("jump forward")
while True:
out = self.func.read(count)
if out == b"":
self.func.emit(ERRPR_CODE_DEFINE.ReceiedError())
break
continue
imagedata = out[0]
if imagedata == b"\x05":
self.func.emit(INFORMATION.UnsupportedImage())
break
continue
if imagedata == b"\x15":
self.func.emit(INFORMATION.NoneImage())
break
continue
if out[1] == False:
start_index = imagedata.find(b"BM6")
self.func.emit(imagedata[start_index:], False)
continue
(imagedata, all_code) = imagedata
self.func.emit({})
self.func.emit({})
self.func.emit({}) # remove {} and this works
break
except Exception:
pass

View File

@@ -14,7 +14,6 @@ assert (
assert "def0" == f"{abc}0"
assert "defdef" == f"{abc}{abc!s}"
# From 3.8 test/test_string.py
# We had the precedence of yield vs. lambda incorrect.
def fn(x):
@@ -98,10 +97,9 @@ else:
(x, y, width) = ("foo", 2, 10)
assert f"x={x*y:{width}}" == "x=foofoo "
# Why the fact that the distinction of docstring versus stmt is a
# string expression is important academic, but we will decompile an
# equivalent thing. For compatibility with older Python we'll use "%"
# equivalent thing. For compatiblity with older Python we'll use "%"
# instead of a format string
def f():
f"""Not a docstring""" # noqa

View File

@@ -1,27 +1,26 @@
# From 3.6 _markupbase.py
# Bug is that the routine is long enough that POP_JUMP_IF_FALSE instruction has an
# EXTENDED_ARG instruction before it and we weren't picking out the jump offset properly
# Bug is that the routine is long enough that POP_JUMP_IF_FALSE instruciton has an
# EXTENDED_ARG intruction before it and we weren't picking out the jump offset properly
def parse_declaration(self, i):
if rawdata[j:j] in ("-", ""):
return -1
n = len(rawdata)
if rawdata[j : j + 2] == "-":
if rawdata[j:j+2] == '-':
return self.parse_comment(i)
elif rawdata[j] == "[":
elif rawdata[j] == '[':
return self.parse_marked_section(i)
else:
decltype, j = self._scan_name(j, i)
if j < 0:
return j
if decltype == "d":
self._decl_otherchars = ""
self._decl_otherchars = ''
while j < n:
c = rawdata[j]
if c == ">":
data = rawdata[i + 2 : j]
data = rawdata[i+2:j]
if decltype == "d":
self.handle_decl(data)
else:
@@ -44,7 +43,8 @@ def parse_declaration(self, i):
else:
self.error("unexpected '[' char in declaration")
else:
self.error("unexpected %r char in declaration" % rawdata[j])
self.error(
"unexpected %r char in declaration" % rawdata[j])
if j < 0:
return j
return -1

View File

@@ -1,9 +0,0 @@
# See https://github.com/rocky/python-uncompyle6/issues/498
# Bug was in not allowing _stmts in whilestmt38
import time
r = 0
while r == 1:
print(time.time())
if r == 1:
r = 0

View File

@@ -1,5 +1,5 @@
# Tests custom added grammar rule:
# expr ::= expr {expr}^n CALL_FUNCTION_n
# which in the specific case below is:
# which in the specifc case below is:
# expr ::= expr expr expr CALL_FUNCTION_2
max(1, 2)

View File

@@ -27,7 +27,7 @@ while 1:
else:
raise RuntimeError
# Degenerate case. Note: we can't run because this causes an infinite loop.
# Degenerate case. Note: we can't run becase this causes an infinite loop.
# Suggested in issue #172
while 1:
pass

View File

@@ -1,8 +1,7 @@
# From 3.6.10 test_binascii.py
# Bug was getting "while c and noise" parsed correctly
# Bug was getting "while c and noise" parsed correclty
# and not put into the "ifelsesmt"
# RUNNABLE!
def addnoise(c, noise):
while c and noise:
@@ -13,7 +12,6 @@ def addnoise(c, noise):
noise = False
return c
assert addnoise(0, True) == 0
assert addnoise(1, False) == 1
assert addnoise(2, True) == 2
@@ -21,10 +19,9 @@ assert addnoise(3, True) == 3
assert addnoise(4, True) == 3
assert addnoise(5, False) == 5
# From 3.6.10 test_dbm_dumb.py
# Bug was getting attaching "else" to the right "if" in the
# presence of a loop.
# presense of a loop.
def test_random(a, r):
x = 0
for dummy in r:
@@ -35,13 +32,11 @@ def test_random(a, r):
x += 1
return x
assert test_random(True, [1]) == 2
assert test_random(True, [1, 1]) == 4
assert test_random(False, [1]) == 0
assert test_random(False, [1, 1]) == 0
# From 2.7.17 test_frozen.py
# Bug was getting making sure we have "try" not
# "try"/"else"
@@ -58,13 +53,11 @@ def test_frozen(a, b):
return x
assert test_frozen(1, 1) == 4.0
assert test_frozen(0, 1) == 5.0
assert test_frozen(0.5, 0) == 6.0
assert test_frozen(0, 0.5) == 8.0
# From 3.6.10 test_binop.py
# Bug was getting "other += 3" outside of "if"/"else.
def __floordiv__(a, b):
@@ -77,7 +70,6 @@ def __floordiv__(a, b):
other += 3
return other
assert __floordiv__(True, True) == 4
assert __floordiv__(True, False) == 4
assert __floordiv__(False, True) == 3

View File

@@ -1,19 +1,19 @@
# Self-checking test.
# Mixed boolean expressions
# Mixed boolean expresions
b = True
assert b, "b = True"
assert b, 'b = True'
c = False
assert not c, "c = False"
assert not c, 'c = False'
d = True
a = b and c or d
assert a, "b and c or d"
assert a, 'b and c or d'
a = (b or c) and d
assert a, "(b or c) and d"
assert a, '(b or c) and d'
a = b or c or d
assert a, "b or c or d"
assert a, 'b or c or d'
a = b and c and d
assert not a, "b and c and d"
assert not a, 'b and c and d'
a = b or c and d
assert a
a = b and (c or d)

View File

@@ -1,5 +1,5 @@
# 2.6.9 symbols.py
# Bug in 2.6 is having multiple COME_FROMs due to the
# Bug in 2.6 is having multple COME_FROMs due to the
# "and" in the "if" clause
# RUNNABLE
@@ -10,7 +10,7 @@ if __name__:
assert False
# 2.6.9 transformer.py
# Bug in 2.6 is multiple COME_FROMs as a result
# Bug in 2.6 is multple COME_FROMs as a result
# of the "or" in the "assert"
# In PyPy the assert is handled via PyPy's unique JUMP_IF_NOT_DEBUG
@@ -24,7 +24,6 @@ elif __file__:
else:
pass
# From 3.3.7 test_binop.py
# Bug was in ifelsestmt(c) ensuring b+=5 is not in "else"
# Also note: ifelsetmtc should not have been used since this
@@ -37,7 +36,6 @@ def __floordiv__(a, b):
b += 5
return b
assert __floordiv__(1, 1) == 7
assert __floordiv__(1, 0) == 6
assert __floordiv__(0, 3) == 8

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

@@ -38,14 +38,17 @@ SKIP_TESTS=(
[test_winreg.py]=1 # it fails on its own
[test_winsound.py]=1 # it fails on its own
[test_zlib.py]=1 # it fails on its own
[test_decimal.py]=1 # fails on its own - no module named test_support
[test_decimal.py]=1 #
[test_dis.py]=1 # We change line numbers - duh!
[test_generators.py]=1 # fails on its own - no module named test_support
[test_generators.py]=1 # Investigate
# [test_grammar.py]=1 # fails on its own - no module tests.test_support
[test_grp.py]=1 # Long test - might work Control flow?
[test_pep247.py]=1 # Long test - might work? Control flow?
[test_socketserver.py]=1 # -- test takes too long to run: 40 seconds
[test_threading.py]=1 # test takes too long to run: 11 seconds
[test_thread.py]=1 # test takes too long to run: 36 seconds
[test_trace.py]=1 # Long test - works
[test_zipfile64.py]=1 # Runs ok but takes 204 seconds
)
# About 243 files, 0 in 19 minutes

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

@@ -1,7 +1,7 @@
#!/usr/bin/env python
# emacs-mode: -*-python-*-
"""
test_pyenvlib -- decompile and verify Python libraries
test_pyenvlib -- uncompyle and verify Python libraries
Usage-Examples:
@@ -20,18 +20,13 @@ Step 2: Run the test:
test_pyenvlib --mylib --verify # decompile verify 'mylib'
"""
# Does not work on 2.5.9 or before
# from __future__ import print_function
import os
import re
import shutil
import sys
import time
from __future__ import print_function
import os, time, re, shutil, sys
from fnmatch import fnmatch
import xdis.magics as magics
from uncompyle6 import main
import xdis.magics as magics
# ----- configure this for your needs
@@ -87,7 +82,6 @@ for vers in TEST_VERSIONS:
if vers == "native":
short_vers = os.path.basename(sys.path[-1])
from xdis.version_info import PYTHON_VERSION_TRIPLE, version_tuple_to_str
if PYTHON_VERSION_TRIPLE > (3, 0):
version = version_tuple_to_str(end=2)
PYC = f"*.cpython-{version}.pyc"
@@ -139,17 +133,8 @@ def do_tests(
pass
if len(files) > max_files:
files = [
file
for file in files
if not "site-packages" in file
and (file.endswith(".pyo") or file.endswith(".pyc"))
]
files = [
file
for file in files
if not "test" in file and (file.endswith(".pyo") or file.endswith(".pyc"))
]
files = [file for file in files if not "site-packages" in file and (file.endswith(".pyo") or file.endswith(".pyc"))]
files = [file for file in files if not "test" in file and (file.endswith(".pyo") or file.endswith(".pyc"))]
if len(files) > max_files:
# print("Number of files %d - truncating to last 200" % len(files))
print(
@@ -166,8 +151,7 @@ def do_tests(
if __name__ == "__main__":
import getopt
import sys
import getopt, sys
do_coverage = do_verify = False
test_dirs = []

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2015-2016, 2818-2022, 2024 by Rocky Bernstein
# Copyright (c) 2015-2016, 2818-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>
# Copyright (c) 1999 John Aycock
@@ -17,12 +17,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
CPython magic- and version-independent disassembly routines
CPython magic- and version- independent disassembly routines
There are two reasons we can't use Python's built-in routines
from ``dis``.
First, the bytecode we are extracting may be from a different
from dis. First, the bytecode we are extracting may be from a different
version of Python (different magic number) than the version of Python
that is doing the extraction.
@@ -41,12 +39,12 @@ from uncompyle6.scanner import get_scanner
def disco(version, co, out=None, is_pypy=False):
"""
diassembles and deparses a given code block ``co``.
diassembles and deparses a given code block 'co'
"""
assert iscode(co)
# Store final output stream in case there is an error.
# store final output stream for case of error
real_out = out or sys.stdout
print("# Python %s" % version_tuple_to_str(version), file=real_out)
if co.co_filename:
@@ -101,7 +99,7 @@ def disco_loop(disasm, queue, real_out):
def disassemble_file(filename, outstream=None):
"""
Disassemble Python byte-code file (.pyc).
disassemble Python byte-code file (.pyc)
If given a Python source file (".py") file, we'll
try to find the corresponding compiled object.
@@ -115,6 +113,7 @@ def disassemble_file(filename, outstream=None):
disco(version, con, outstream)
else:
disco(version, co, outstream, is_pypy=is_pypy)
co = None
def _test():

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
@@ -669,8 +659,6 @@ def get_python_parser(
version = version[:2]
p = None
# FIXME: there has to be a better way...
# We could do this as a table lookup, but that would force us
# in import all of the parsers all of the time. Perhaps there is
@@ -919,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)
@@ -120,6 +118,19 @@ class Python24Parser(Python25Parser):
token_len = len(tokens)
if 0 <= token_len < len(tokens):
return not int(tokens[first].pattr) == tokens[last].offset
elif lhs == "try_except":
if last == len(tokens):
last -= 1
if tokens[last] != "COME_FROM" and tokens[last - 1] == "COME_FROM":
last -= 1
return (
tokens[last] == "COME_FROM"
and tokens[last - 1] == "END_FINALLY"
and tokens[last - 2] == "POP_TOP"
and tokens[last - 3].kind != "JUMP_FORWARD"
)
return False
class Python24ParserSingle(Python24Parser, PythonParserSingle):

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,11 +33,9 @@ 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
# The last except of a "try: ... except" can do this...
except_suite ::= c_stmts_opt COME_FROM JUMP_ABSOLUTE POP_TOP
store ::= STORE_NAME
store ::= STORE_FAST
@@ -50,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
@@ -60,13 +58,6 @@ class Python25Parser(Python26Parser):
kvlist ::= kvlist kv
kv ::= DUP_TOP expr ROT_TWO expr STORE_SUBSCR
_ifstmts_jump ::= c_stmts_opt COME_FROM JUMP_ABSOLUTE COME_FROM POP_TOP
# "and_then" is a hack around the fact we have THEN detection.
and_then ::= expr JUMP_IF_FALSE THEN POP_TOP expr JUMP_IF_FALSE THEN POP_TOP
testexpr_then ::= and_then
"""
def customize_grammar_rules(self, tokens, customize):
@@ -76,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.
"""
@@ -130,20 +130,13 @@ class Python26Parser(Python2Parser):
# Semantic actions want else_suitel to be at index 3
ifelsestmtl ::= testexpr c_stmts_opt cf_jb_cf_pop else_suitel
ifelsestmtc ::= testexpr c_stmts_opt ja_cf_pop else_suitec
ifelsestmt ::= testexpr stmts_opt ja_cf_pop else_suite
stmts_opt ::= stmts
stmts_opt ::=
# The last except of a "try: ... except" can do this...
except_suite ::= stmts_opt COME_FROM JUMP_ABSOLUTE POP_TOP
# Semantic actions want suite_stmts_opt to be at index 3
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt
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
@@ -359,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)
@@ -398,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
@@ -473,16 +467,14 @@ class Python26Parser(Python2Parser):
return tokens[last].offset != ja_attr
elif lhs == "try_except":
# We need to distinguish "try_except" from "tryelsestmt"; we do that
# by looking for a jump before the END_FINALLY to the "else" clause of
# "try else".
#
# by checking the jump before the END_FINALLY
# If we have:
# <insn>
# insn
# POP_TOP
# END_FINALLY
# COME_FROM
# then <insn> has to be either a a jump of some sort (JUMP_FORWARD, BREAK_LOOP, JUMP_BACK, or RETURN_VALUE).
# Furthermore, if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
# then insn has to be either a JUMP_FORWARD or a RETURN_VALUE
# and if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
# COME_FROM
if last == len(tokens):
last -= 1
@@ -496,8 +488,54 @@ class Python26Parser(Python2Parser):
# A jump of 2 is a jump around POP_TOP, END_FINALLY which
# would indicate try/else rather than try
return tokens[last - 3].kind not in frozenset(
("JUMP_FORWARD", "JUMP_BACK", "BREAK_LOOP", "RETURN_VALUE")
("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.
# This AST method is slower, but the token-only based approach
# didn't work as it failed with a "try" embedded inside a "try/else"
# since we can't detect COME_FROM boundaries.
if ast[3] == "except_handler":
except_handler = ast[3]
if except_handler[0] == "JUMP_FORWARD":
else_start = int(except_handler[0].pattr)
if last == len(tokens):
last -= 1
if tokens[last] == "COME_FROM" and isinstance:
last_offset = int(tokens[last].offset.split("_")[0])
return else_start >= last_offset
# The above test apparently isn't good enough, so we have additional
# checks distinguish "try_except" from "tryelsestmt". we do that
# by checking the jump before the "END_FINALLY".
# If we have:
# insn
# POP_TOP
# END_FINALLY
# COME_FROM
# then insn is neither a JUMP_FORWARD nor RETURN_VALUE,
# or if it is JUMP_FORWARD, then it can't be a JUMP_FORWARD to right after
# COME_FROM
if last == len(tokens):
last -= 1
while tokens[last - 1] == "COME_FROM" and tokens[last - 2] == "COME_FROM":
last -= 1
if tokens[last] == "COME_FROM" and tokens[last - 1] == "COME_FROM":
last -= 1
if (
tokens[last] == "COME_FROM"
and tokens[last - 1] == "END_FINALLY"
and tokens[last - 2] == "POP_TOP"
):
# A jump of 2 is a jump around POP_TOP, END_FINALLY which
# would indicate try/else rather than try
return tokens[last - 3].kind in frozenset(
("JUMP_FORWARD", "RETURN_VALUE")
) and (tokens[last - 3] != "JUMP_FORWARD" or tokens[last - 3].attr == 2)
return False

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2020, 2023-2024 Rocky Bernstein
# Copyright (c) 2016-2020, 2023 Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <hartmut@goebel.noris.de>
@@ -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
@@ -706,7 +706,7 @@ class Python3Parser(PythonParser):
# Note: BUILD_TUPLE_UNPACK_WITH_CALL gets considered by
# default because it starts with BUILD. So we'll set to ignore it from
# the start.
custom_ops_processed = {"BUILD_TUPLE_UNPACK_WITH_CALL"}
custom_ops_processed = set(("BUILD_TUPLE_UNPACK_WITH_CALL",))
# A set of instruction operation names that exist in the token stream.
# We use this customize the grammar that we create.

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

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2017, 2022, 2024 Rocky Bernstein
# Copyright (c) 2016-2017, 2022 Rocky Bernstein
"""
spark grammar differences over Python 3.2 for Python 3.1.
"""
@@ -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,4 +1,4 @@
# Copyright (c) 2016-2017, 2022-2024 Rocky Bernstein
# Copyright (c) 2016-2017, 2022 Rocky Bernstein
"""
spark grammar differences over Python 3 for Python 3.2.
"""
@@ -84,7 +84,7 @@ class Python32Parser(Python3Parser):
for i, token in enumerate(tokens):
opname = token.kind
if opname.startswith("MAKE_FUNCTION_A"):
args_pos, _, annotate_args = token.attr
args_pos, args_kw, annotate_args = token.attr
# Check that there are 2 annotated params?
rule = (
"mkfunc_annotate ::= %s%sannotate_tuple "

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2017-2018, 2022-2024 Rocky Bernstein
# Copyright (c) 2017-2018 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
@@ -21,6 +21,7 @@ from uncompyle6.parsers.parse33 import Python33Parser
class Python34Parser(Python33Parser):
def p_misc34(self, args):
"""
expr ::= LOAD_ASSERT
@@ -53,52 +54,39 @@ class Python34Parser(Python33Parser):
_ifstmts_jump ::= c_stmts_opt JUMP_ABSOLUTE JUMP_FORWARD COME_FROM
genexpr_func ::= LOAD_ARG _come_froms FOR_ITER store comp_iter JUMP_BACK
if_exp_lambda ::= expr jmp_false expr return_if_lambda return_stmt_lambda LAMBDA_MARKER
return_if_lambda ::= RETURN_END_IF_LAMBDA come_froms
return_if_stmt ::= return_expr RETURN_END_IF POP_BLOCK
"""
def customize_grammar_rules(self, tokens, customize):
self.remove_rules(
"""
self.remove_rules("""
yield_from ::= expr expr YIELD_FROM
# 3.4.2 has this. 3.4.4 may now
# while1stmt ::= SETUP_LOOP l_stmts COME_FROM JUMP_BACK COME_FROM_LOOP
"""
)
""")
super(Python34Parser, self).customize_grammar_rules(tokens, customize)
return
class Python34ParserSingle(Python34Parser, PythonParserSingle):
pass
if __name__ == "__main__":
if __name__ == '__main__':
# Check grammar
p = Python34Parser()
p.check_grammar()
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
if PYTHON_VERSION_TRIPLE[:2] == (3, 4):
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,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
@@ -108,10 +106,9 @@ class Python35Parser(Python34Parser):
# Python 3.5+ does jump optimization
# In <.3.5 the below is a JUMP_FORWARD to a JUMP_ABSOLUTE.
return_if_stmt ::= return_expr RETURN_END_IF POP_BLOCK
return_if_lambda ::= RETURN_END_IF_LAMBDA COME_FROM
return ::= return_expr RETURN_END_IF
jb_else ::= JUMP_BACK ELSE
ifelsestmtc ::= testexpr c_stmts_opt JUMP_FORWARD else_suitec
ifelsestmtl ::= testexpr c_stmts_opt jb_else else_suitel
@@ -138,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
@@ -204,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
@@ -238,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.
@@ -263,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
@@ -53,8 +52,6 @@ class Python36Parser(Python35Parser):
for_block ::= l_stmts_opt come_from_loops JUMP_BACK
come_from_loops ::= COME_FROM_LOOP*
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt
JUMP_BACK come_froms POP_BLOCK
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt
JUMP_BACK come_froms POP_BLOCK COME_FROM_LOOP
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt
@@ -210,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
@@ -238,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
@@ -252,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
@@ -276,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
@@ -284,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
@@ -310,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(
@@ -486,6 +475,7 @@ class Python36Parser(Python35Parser):
)
custom_ops_processed.add(opname)
elif opname == "GET_ANEXT":
self.addRule(
"""
@@ -510,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.
@@ -526,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
@@ -534,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
@@ -552,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:
@@ -559,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.
"""
@@ -138,7 +138,7 @@ class Python37BaseParser(PythonParser):
# Note: BUILD_TUPLE_UNPACK_WITH_CALL gets considered by
# default because it starts with BUILD. So we'll set to ignore it from
# the start.
custom_ops_processed = {"BUILD_TUPLE_UNPACK_WITH_CALL"}
custom_ops_processed = set(("BUILD_TUPLE_UNPACK_WITH_CALL",))
# A set of instruction operation names that exist in the token stream.
# We use this customize the grammar that we create.
@@ -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
@@ -417,7 +417,7 @@ class Python38Parser(Python37Parser):
[opname[: opname.rfind("_")] for opname in self.seen_ops]
)
custom_ops_processed = {"DICT_MERGE"}
custom_ops_processed = set(["DICT_MERGE"])
# Loop over instructions adding custom grammar rules based on
# a specific instruction seen.
@@ -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

@@ -1,8 +1,7 @@
# Copyright (c) 2020 Rocky Bernstein
def except_handler(self, lhs, n, rule, ast, tokens, first, last):
end_token = tokens[last - 1]
end_token = tokens[last-1]
# print("XXX", first, last)
# for t in range(first, last):
@@ -14,7 +13,7 @@ def except_handler(self, lhs, n, rule, ast, tokens, first, last):
if self.version[:2] == (1, 4):
return False
# Make sure COME_FROMs froms come from within "except_handler".
# Make sure come froms all come from within "except_handler".
if end_token != "COME_FROM":
return False
return end_token.attr < tokens[first].offset

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

@@ -82,8 +82,14 @@ IFELSE_STMT_RULES = frozenset(
),
),
(
"ifelsestmtc",
("testexpr", "c_stmts_opt", "JUMP_FORWARD", "else_suite", "come_froms"),
'ifelsestmtc',
(
'testexpr',
'c_stmts_opt',
'JUMP_FORWARD',
'else_suite',
'come_froms'
),
),
(
"ifelsestmt",
@@ -149,6 +155,7 @@ IFELSE_STMT_RULES = frozenset(
def ifelsestmt(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
@@ -169,10 +176,13 @@ def ifelsestmt(self, lhs, n, rule, tree, tokens, first, last):
stmts = tree[1]
if stmts in ("c_stmts",) and len(stmts) == 1:
raise_stmt1 = stmts[0]
if raise_stmt1 == "raise_stmt1" and raise_stmt1[0] in ("LOAD_ASSERT",):
if (
raise_stmt1 == "raise_stmt1" and
raise_stmt1[0] in ("LOAD_ASSERT",)
):
return True
# Make sure all the offsets from the "COME_FROMs" at the
# Make sure all the offsets from the "come froms" 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
@@ -267,18 +277,17 @@ def ifelsestmt(self, lhs, n, rule, tree, tokens, first, last):
# only if we are trying to match or reduce an "if"
# statement of the kind that can occur only inside a
# loop construct.
if lhs in ("ifelsestmtl", "ifelsestmtc"):
jump_false = jmp
if (
tree[2].kind in ("JUMP_FORWARD", "JUMP_ABSOLUTE")
tree[2].kind == "JUMP_FORWARD"
and jump_false == "jmp_false"
and len(else_suite) == 1
):
suite_stmts = else_suite[0]
continue_stmt = suite_stmts[0]
if (
suite_stmts in ("suite_stmts", "c_stmts")
suite_stmts == "suite_stmts"
and len(suite_stmts) == 1
and continue_stmt == "continue"
and jump_false[0].attr == continue_stmt[0].attr

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

@@ -1,17 +1,16 @@
# Copyright (c) 2020, 2022, 2024 Rocky Bernstein
# Copyright (c) 2020, 2022 Rocky Bernstein
def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
come_from_except = ast[-1]
if rule == (
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"opt_come_from_except",
),
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"opt_come_from_except",
),
):
if come_from_except[0] == "COME_FROM":
# There should be at least two COME_FROMs, one from an
@@ -21,31 +20,31 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
pass
elif rule == (
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"COME_FROM",
),
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"COME_FROM",
),
):
return come_from_except.attr < tokens[first].offset
elif rule == (
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"\\e_opt_come_from_except",
),
'try_except',
(
'SETUP_EXCEPT',
'suite_stmts_opt',
'POP_BLOCK',
'except_handler',
'\\e_opt_come_from_except'
),
):
# Find END_FINALLY.
for i in range(last, first, -1):
if tokens[i] == "END_FINALLY":
jump_before_finally = tokens[i - 1]
jump_before_finally = tokens[i-1]
if jump_before_finally.kind.startswith("JUMP"):
if jump_before_finally == "JUMP_FORWARD":
# If there is a JUMP_FORWARD before
@@ -53,9 +52,7 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
# beyond tokens[last].off2int() then
# this is a try/else rather than an
# try (no else).
return tokens[i - 1].attr > tokens[last].off2int(
prefer_last=True
)
return tokens[i-1].attr > tokens[last].off2int(prefer_last=True)
elif jump_before_finally == "JUMP_BACK":
# If there is a JUMP_BACK before the
# END_FINALLY then this is a looping
@@ -64,10 +61,8 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
# jump or this is a try/else rather
# than an try (no else).
except_handler = ast[3]
if (
except_handler == "except_handler"
and except_handler[0] == "JUMP_FORWARD"
):
if (except_handler == "except_handler" and
except_handler[0] == "JUMP_FORWARD"):
return True
return False
pass

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
@@ -605,10 +632,6 @@ class Scanner(ABC):
def get_scanner(version: Union[str, tuple], is_pypy=False, show_asm=None) -> Scanner:
"""
Import the right scanner module for ``version`` and return the Scanner class
in that module.
"""
# If version is a string, turn that into the corresponding float.
if isinstance(version, str):
if version not in canonic_python_version:

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,14 +206,14 @@ 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,
names=co.co_names,
constants=co.co_consts,
cells=bytecode._cell_names,
line_starts=bytecode._linestarts,
linestarts=bytecode._linestarts,
asm_format="extended",
)
@@ -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,9 +490,8 @@ class Scanner2(Scanner):
pass
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
# FIXME: t.format() is changing tokens!
for t in new_tokens.copy():
print("\n# ---- after tokenization:")
for t in new_tokens:
print(t.format(line_prefix=""))
print()
return new_tokens, customize
@@ -545,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
@@ -932,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'
@@ -1123,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,16 +76,9 @@ class Scanner26(scan.Scanner2):
# show_asm = 'after'
if show_asm in ("both", "before"):
print("\n# ---- disassembly:")
bytecode.disassemble_bytes(
co.co_code,
varnames=co.co_varnames,
names=co.co_names,
constants=co.co_consts,
cells=bytecode._cell_names,
line_starts=bytecode._linestarts,
asm_format="extended",
)
for instr in bytecode.get_instructions(co):
print(instr.disassemble())
# Container for tokens
tokens = []
@@ -107,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])
@@ -143,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
@@ -155,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.
@@ -194,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]
@@ -250,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)
@@ -269,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
@@ -301,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:
@@ -352,9 +316,7 @@ class Scanner26(scan.Scanner2):
pass
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
# FIXME: t.format() is changing tokens!
for t in tokens.copy():
for t in tokens:
print(t.format(line_prefix=""))
print()
return tokens, customize

View File

@@ -216,7 +216,7 @@ class Scanner3(Scanner):
collection_type: str,
) -> Optional[list]:
"""
Try to replace a sequence of instruction that ends with a
Try to a replace sequence of instruction that ends with a
BUILD_xxx with a sequence that can be parsed much faster, but
inserting the token boundary at the beginning of the sequence.
"""
@@ -298,9 +298,8 @@ class Scanner3(Scanner):
)
return new_tokens
# Move to scanner35?
def bound_map_from_inst_35(
self, insts: list, next_tokens: list, t: Token, i: int
def bound_map_from_inst(
self, insts: list, next_tokens: list, inst: Instruction, t: Token, i: int
) -> Optional[list]:
"""
Try to a sequence of instruction that ends with a BUILD_MAP into
@@ -316,8 +315,6 @@ class Scanner3(Scanner):
if count < 5:
return None
# Newer Python BUILD_MAP argument's count is a
# key and value pair so it is multiplied by two.
collection_start = i - (count * 2)
assert (count * 2) <= i
@@ -341,7 +338,7 @@ class Scanner3(Scanner):
attr=collection_enum,
pattr="CONST_MAP",
offset=f"{start_offset}_0",
linestart=insts[collection_start].starts_line,
linestart=False,
has_arg=True,
has_extended_arg=False,
opc=self.opc,
@@ -359,7 +356,6 @@ class Scanner3(Scanner):
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype="pseudo",
)
)
new_tokens.append(
@@ -372,7 +368,7 @@ class Scanner3(Scanner):
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype="pseudo",
optype=insts[j + 1].optype,
)
)
new_tokens.append(
@@ -385,7 +381,7 @@ class Scanner3(Scanner):
has_arg=t.has_arg,
has_extended_arg=False,
opc=t.opc,
optype="pseudo",
optype=t.optype,
)
)
return new_tokens
@@ -422,14 +418,14 @@ 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,
names=co.co_names,
constants=co.co_consts,
cells=bytecode._cell_names,
line_starts=bytecode._linestarts,
linestarts=bytecode._linestarts,
asm_format="extended",
)
@@ -494,16 +490,7 @@ class Scanner3(Scanner):
last_op_was_break = False
new_tokens = []
skip_end_offset = None
for i, inst in enumerate(self.insts):
# BUILD_MAP for < 3.5 can skip *forward* in instructions and
# replace them. So we use the below to get up to the position
# scanned and replaced forward
if skip_end_offset and inst.offset <= skip_end_offset:
continue
skip_end_offset = None
opname = inst.opname
argval = inst.argval
pattr = inst.argrepr
@@ -537,21 +524,17 @@ class Scanner3(Scanner):
if try_tokens is not None:
new_tokens = try_tokens
continue
elif opname in ("BUILD_MAP",):
if self.version >= (3, 5):
try_tokens = self.bound_map_from_inst_35(
self.insts,
new_tokens,
t,
i,
)
if try_tokens is not None:
new_tokens = try_tokens
continue
pass
pass
pass
try_tokens = self.bound_map_from_inst(
self.insts,
new_tokens,
inst,
t,
i,
)
if try_tokens is not None:
new_tokens = try_tokens
continue
argval = inst.argval
op = inst.opcode
@@ -805,9 +788,8 @@ class Scanner3(Scanner):
pass
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
# FIXME: t.format() is changing tokens!
for t in new_tokens.copy():
print("\n# ---- after tokenization:")
for t in new_tokens:
print(t.format(line_prefix=""))
print()
return new_tokens, customize

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,14 +222,14 @@ class Scanner37Base(Scanner):
bytecode = self.build_instructions(co)
if show_asm in ("both", "before"):
print("\n# ---- disassembly:")
bytecode.disassemble_bytes(
print("\n# ---- before tokenization:")
self.insts = bytecode.disassemble_bytes(
co.co_code,
varnames=co.co_varnames,
names=co.co_names,
constants=co.co_consts,
cells=bytecode._cell_names,
line_starts=bytecode._linestarts,
linestarts=bytecode._linestarts,
asm_format="extended",
filename=co.co_filename,
show_source=True,
@@ -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
@@ -481,17 +477,12 @@ class Scanner37Base(Scanner):
next_opname = self.insts[i + 1].opname
# 'Continue's include jumps to loops that are not
# and the end of a block which follow with
# POP_BLOCK and COME_FROM_LOOP. If the
# JUMP_ABSOLUTE is to a FOR_ITER, and it is
# followed by another JUMP_FORWARD then we'll take
# it as a "continue".
next_inst = self.insts[i + 1]
is_continue = self.insts[
self.offset2inst_index[target]
].opname == "FOR_ITER" and next_inst.opname in (
"JUMP_FORWARD",
"JUMP_ABSOLUTE",
# and the end of a block which follow with POP_BLOCK and COME_FROM_LOOP.
# If the JUMP_ABSOLUTE is to a FOR_ITER and it is followed by another JUMP_FORWARD
# then we'll take it as a "continue".
is_continue = (
self.insts[self.offset2inst_index[target]].opname == "FOR_ITER"
and self.insts[i + 1].opname == "JUMP_FORWARD"
)
if self.version < (3, 8) and (
@@ -506,65 +497,21 @@ class Scanner37Base(Scanner):
):
opname = "CONTINUE"
else:
# "continue" versus "break_loop" dectction is more complicated
# because "continue" to an outer loop is really a "break loop"
opname = "JUMP_BACK"
# FIXME: this is a hack to catch stuff like:
# if x: continue
# the "continue" is not on a new line.
#
# Another situation is where we have
# for method in methods:
# for B in method:
# if c:
# return
# break # A "continue" but not the innermost one
if tokens[-1].kind == "JUMP_LOOP" and tokens[-1].attr <= argval:
# There are other situations where we don't catch
# CONTINUE as well.
if tokens[-1].kind == "JUMP_BACK" and tokens[-1].attr <= argval:
if tokens[-2].kind == "BREAK_LOOP":
del tokens[-1]
j -= 1
else:
# "intern" is used because we are
# changing the *previous* token. A
# POP_TOP suggests a "break" rather
# than a "continue"?
if tokens[-2] == "POP_TOP" and (
is_continue and next_inst.argval != tokens[-1].attr
):
tokens[-1].kind = sys.intern("BREAK_LOOP")
else:
tokens[-1].kind = sys.intern("CONTINUE")
last_continue = tokens[-1]
pass
pass
pass
# elif (
# last_continue is not None
# and tokens[-1].kind == "JUMP_LOOP"
# and last_continue.attr <= tokens[-1].attr
# and last_continue.offset > tokens[-1].attr
# ):
# # Handle mis-characterized "CONTINUE"
# # We have a situation like:
# # loop ... for or while)
# # loop
# # if ...: # code below starts here
# # break # not continue
# #
# # POP_JUMP_IF_FALSE_LOOP # to outer loop
# # JUMP_LOOP # to inner loop
# # ...
# # JUMP_LOOP # to outer loop
# tokens[-2].kind = sys.intern("BREAK_LOOP")
# pass
# if last_op_was_break and opname == "CONTINUE":
# last_op_was_break = False
# continue
pass
else:
opname = "JUMP_FORWARD"
# intern is used because we are changing the *previous* token
tokens[-1].kind = sys.intern("CONTINUE")
if last_op_was_break and opname == "CONTINUE":
last_op_was_break = False
continue
elif inst.offset in self.load_asserts:
opname = "LOAD_ASSERT"
@@ -587,10 +534,9 @@ class Scanner37Base(Scanner):
)
pass
if show_asm in ("both", "after") and self.version < (3, 8):
print("\n# ---- tokenization:")
# FIXME: t.format() is changing tokens!
for t in tokens.copy():
if show_asm in ("both", "after"):
print("\n# ---- after tokenization:")
for t in tokens:
print(t.format(line_prefix=""))
print()
return tokens, customize

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2019-2022, 2024 by Rocky Bernstein
# Copyright (c) 2019-2022 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
@@ -24,12 +24,12 @@ scanner routine for Python 3.7 and up.
from typing import Dict, Tuple
# bytecode verification, verify(), uses JUMP_OPs from here
from xdis.opcodes import opcode_38 as opc
from uncompyle6.scanners.tok import off2int
from uncompyle6.scanners.scanner37 import Scanner37
from uncompyle6.scanners.scanner37base import Scanner37Base
from uncompyle6.scanners.tok import off2int
# bytecode verification, verify(), uses JUMP_OPs from here
from xdis.opcodes import opcode_38 as opc
# bytecode verification, verify(), uses JUMP_OPS from here
JUMP_OPs = opc.JUMP_OPS
@@ -121,26 +121,35 @@ class Scanner38(Scanner37):
new_tokens.append(token)
continue
j = i
while tokens[j - 1] in ("POP_TOP", "POP_BLOCK", "POP_EXCEPT"):
j -= 1
if tokens[j].linestart:
break
token_with_linestart = tokens[j]
# We also want to avoid confusing BREAK_LOOPS with parts of the
# grammar rules for loops. (Perhaps we should change the grammar.)
# Try to find an adjacent JUMP_BACK which is part of the normal loop end.
if token_with_linestart.linestart:
if i + 1 < len(tokens) and tokens[i + 1] == "JUMP_BACK":
# Sometimes the jump back is after the "break" instruction..
jump_back_index = i + 1
else:
# and sometimes, because of jump-to-jump optimization, it is before the
# jump target instruction.
jump_back_index = self.offset2tok_index[jump_target] - 1
while tokens[jump_back_index].kind.startswith("COME_FROM_"):
jump_back_index -= 1
pass
pass
jump_back_token = tokens[jump_back_index]
# Is this a forward jump not next to a JUMP_BACK ? ...
break_loop = token.linestart and jump_back_token != "JUMP_BACK"
# or if there is looping jump back, then that loop
# should start before where the "break" instruction sits.
if break_loop or (
jump_back_token == "JUMP_BACK"
and jump_back_token.attr < token.off2int()
):
token.kind = "BREAK_LOOP"
pass
new_tokens.append(token)
if show_asm in ("both", "after"):
print("\n# ---- tokenization:")
# FIXME: t.format() is changing tokens!
for t in new_tokens.copy():
print(t.format(line_prefix=""))
print()
return new_tokens, customize

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2018, 2022-2024 by Rocky Bernstein
# Copyright (c) 2018, 2022-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
@@ -18,14 +18,11 @@ import sys
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from xdis import iscode
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
from xdis.version_info import IS_PYPY
from uncompyle6.scanner import get_scanner
from uncompyle6.semantics.consts import ASSIGN_DOC_STRING
from uncompyle6.semantics.pysource import (
ASSIGN_DOC_STRING,
RETURN_NONE,
TREE_DEFAULT_DEBUG,
SourceWalker,
SourceWalkerError,
find_globals_and_nonlocals
@@ -41,7 +38,7 @@ class AligningWalker(SourceWalker, object):
version,
out,
scanner,
showast=TREE_DEFAULT_DEBUG,
showast=False,
debug_parser=PARSER_DEFAULT_DEBUG,
compile_mode="exec",
is_pypy=False,
@@ -51,7 +48,6 @@ class AligningWalker(SourceWalker, object):
)
self.desired_line_number = 0
self.current_line_number = 0
self.showast = showast
def println(self, *data):
if data and not (len(data) == 1 and data[0] == ""):
@@ -117,12 +113,12 @@ class AligningWalker(SourceWalker, object):
key = key[i]
pass
if key.kind in table:
self.template_engine(table[key.kind], node)
if key.type in table:
self.template_engine(table[key.type], node)
self.prune()
DEFAULT_DEBUG_OPTS = {"asm": False, "tree": TREE_DEFAULT_DEBUG, "grammar": False}
DEFAULT_DEBUG_OPTS = {"asm": False, "tree": False, "grammar": False}
def code_deparse_align(
@@ -141,7 +137,7 @@ def code_deparse_align(
assert iscode(co)
if version is None:
version = PYTHON_VERSION_TRIPLE
version = float(sys.version[0:3])
if is_pypy is None:
is_pypy = IS_PYPY
@@ -160,11 +156,11 @@ def code_deparse_align(
debug_parser["errorstack"] = True
# Build a parse tree from tokenized and massaged disassembly.
show_ast = debug_opts.get("ast", TREE_DEFAULT_DEBUG)
show_ast = debug_opts.get("ast", None)
deparsed = AligningWalker(
version,
out,
scanner,
out,
showast=show_ast,
debug_parser=debug_parser,
compile_mode=compile_mode,
@@ -214,4 +210,4 @@ if __name__ == "__main__":
print(deparsed.text)
return
deparse_test(deparse_test.func_code)
deparse_test(deparse_test.__code__)

View File

@@ -44,7 +44,7 @@ maxint = sys.maxsize
# children. For example, "call" has precedence 2 so we don't get
# additional the additional parenthesis of: ".. op (call())". However
# for call's children, it parameters, we set the the precedence high,
# say to 100, to make sure we avoid additional parenthesis in
# say to 100, to make sure we avoid additional prenthesis in
# call((.. op ..)).
NO_PARENTHESIS_EVER = 100
@@ -57,8 +57,6 @@ PRECEDENCE = {
"list_unpack": 38, # *args
"yield_from": 38,
"tuple_list_starred": 38, # *x, *y, *z - about at the level of yield?
"unpack": 38, # A guess. Used in "async with ... as ...
# This might also get used in tuple assignment?
"_lambda_body": 30,
"lambda_body": 32, # lambda ... : lambda_body
@@ -132,7 +130,7 @@ LINE_LENGTH = 80
# Some parse trees created below are used for comparing code
# fragments (like "return None" at the end of functions).
ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree( # noqa
ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree(
"assign",
[
SyntaxTree(
@@ -158,7 +156,6 @@ NAME_MODULE = SyntaxTree(
],
)
NEWLINE = SyntaxTree("newline", [])
NONE = SyntaxTree("expr", [NoneToken])
RETURN_NONE = SyntaxTree("stmt", [SyntaxTree("return", [NONE, Token("RETURN_VALUE")])])
@@ -243,72 +240,180 @@ TABLE_DIRECT = {
"UNARY_NOT": ( "not ", ),
"UNARY_POSITIVE": ( "+",),
"and": ("%c and %c", 0, 2),
"and2": ("%c", 3),
"assert_expr_or": ("%c or %c", 0, 2),
"assert_expr_and": ("%c and %c", 0, 2),
"assign": (
"%|%c = %p\n",
-1,
(0, ("expr", "branch_op"), PRECEDENCE["tuple_list_starred"] + 1)
),
"attribute": ("%c.%[1]{pattr}", (0, "expr")),
# This nonterminal we create on the fly in semantic routines
"attribute_w_parens": ("(%c).%[1]{pattr}", (0, "expr")),
# The 2nd parameter should have a = suffix.
# There is a rule with a 4th parameter "store"
# which we don't use here.
"aug_assign1": ("%|%c %c %c\n", 0, 2, 1),
"aug_assign2": ("%|%c.%[2]{pattr} %c %c\n", 0, -3, -4),
# bin_op (formerly "binary_expr") is the Python AST BinOp
"bin_op": ("%c %c %c", 0, (-1, "binary_operator"), (1, "expr")),
# unary_op (formerly "unary_expr") is the Python AST UnaryOp
"unary_op": ("%c%c", (1, "unary_operator"), (0, "expr")),
"unary_not": ("not %c", (0, "expr")),
"unary_convert": ("`%c`", (0, "expr"),),
"get_iter": ("iter(%c)", (0, "expr"),),
"break": ("%|break\n",),
"build_tuple2": (
"%P",
(0, -1, ", ", NO_PARENTHESIS_EVER)
),
"set_iter": ( "%c", 0 ),
"call_stmt": ( "%|%p\n",
# When a call statement contains only a named_expr (:=)
# the named_expr should have parenthesis around it.
(0, PRECEDENCE["named_expr"]-1)),
"slice0": (
"%c[:]",
(0, "expr"),
),
"slice1": (
"%c[%p:]",
(0, "expr"),
(1, NO_PARENTHESIS_EVER)
),
# "classdef": (), # handled by n_classdef()
# A custom rule in n_function def distinguishes whether to call this or
# function_def_async
"slice2": ( "%c[:%p]",
(0, "expr"),
(1, NO_PARENTHESIS_EVER)
),
"classdefdeco": ("\n\n%c", 0),
"classdefdeco1": ("%|@%c\n%c", 0, 1),
"comp_body": ("",), # ignore when recusing
"comp_if": (" if %c%c", 0, 2),
"comp_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2),
"comp_iter": ("%c", 0),
"compare_single": ('%p %[-1]{pattr.replace("-", " ")} %p', (0, 19), (1, 19)),
"compare_chained": ("%p %p", (0, 29), (1, 30)),
"compared_chained_middle": ('%[3]{pattr.replace("-", " ")} %p %p', (0, 19), (-2, 19)),
"compare_chained_right": ('%[1]{pattr.replace("-", " ")} %p', (0, 19)),
"continue": ("%|continue\n",),
"slice3": (
"%c[%p:%p]",
(0, "expr"),
(1, NO_PARENTHESIS_EVER),
(2, NO_PARENTHESIS_EVER)
),
"IMPORT_FROM": ("%{pattr}",),
"IMPORT_NAME_ATTR": ("%{pattr}",),
"attribute": ("%c.%[1]{pattr}", (0, "expr")),
"delete_subscript": (
"%|del %p[%c]\n",
(0, "expr", PRECEDENCE["subscript"]),
(1, "expr"),
),
"designList": ("%c = %c", 0, -1),
"dict_comp_body": ("%c: %c", 1, 0),
"subscript": (
"%p[%p]",
(0, "expr", PRECEDENCE["subscript"]),
(1, "expr", NO_PARENTHESIS_EVER)
),
"subscript2": (
"%p[%p]",
(0, "expr", PRECEDENCE["subscript"]),
(1, "expr", NO_PARENTHESIS_EVER)
),
"store_subscript": ("%p[%c]", (0, "expr", PRECEDENCE["subscript"]), (1, "expr")),
"unpack": ("%C%,", (1, maxint, ", ")),
# This nonterminal we create on the fly in semantic routines
"unpack_w_parens": ("(%C%,)", (1, maxint, ", ")),
# This nonterminal we create on the fly in semantic routines
"attribute_w_parens": ("(%c).%[1]{pattr}", (0, "expr")),
# This nonterminal we create on the fly in semantic routines
"store_w_parens": ("(%c).%[1]{pattr}", (0, "expr")),
"unpack_list": ("[%C]", (1, maxint, ", ")),
"build_tuple2": ("%P", (0, -1, ", ", 100)),
"list_iter": ("%c", 0),
"list_for": (" for %c in %c%c", 2, 0, 3),
"list_if": (" if %p%c", (0, "expr", 27), 2),
"list_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2),
"lc_body": ("",), # ignore when recursing
"comp_iter": ("%c", 0),
"comp_if": (" if %c%c", 0, 2),
"comp_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2),
"comp_body": ("",), # ignore when recusing
"set_comp_body": ("%c", 0),
"gen_comp_body": ("%c", 0),
"dict_comp_body": ("%c: %c", 1, 0),
"assign": ("%|%c = %p\n", -1, (0, 200)),
# The 2nd parameter should have a = suffix.
# There is a rule with a 4th parameter "store"
# which we don't use here.
"aug_assign1": ("%|%c %c %c\n", 0, 2, 1),
"aug_assign2": ("%|%c.%[2]{pattr} %c %c\n", 0, -3, -4),
"designList": ("%c = %c", 0, -1),
"and": ("%c and %c", 0, 2),
"ret_and": ("%c and %c", 0, 2),
"and2": ("%c", 3),
"or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])),
"ret_or": ("%c or %c", 0, 2),
"if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4),
"if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4),
"if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2),
"if_exp_ret": ("%p if %p else %p", (2, 27), (0, 27), (-1, 27)),
"if_exp_not": (
"%p if not %p else %p",
(2, 27),
(0, "expr", PRECEDENCE["unary_not"]),
(4, 27),
),
"if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4),
"compare_single": ('%p %[-1]{pattr.replace("-", " ")} %p', (0, 19), (1, 19)),
"compare_chained": ("%p %p", (0, 29), (1, 30)),
"compared_chained_middle": ('%[3]{pattr.replace("-", " ")} %p %p', (0, 19), (-2, 19)),
"compare_chained_right": ('%[1]{pattr.replace("-", " ")} %p', (0, 19)),
# "classdef": (), # handled by n_classdef()
# A custom rule in n_function def distinguishes whether to call this or
# function_def_async
"function_def": ("\n\n%|def %c\n", -2), # -2 to handle closures
"function_def_deco": ("\n\n%c", 0),
"mkfuncdeco": ("%|@%c\n%c", 0, 1),
# A custom rule in n_function def distinguishes whether to call this or
# function_def_async
"mkfuncdeco0": ("%|def %c\n", 0),
"classdefdeco": ("\n\n%c", 0),
"classdefdeco1": ("%|@%c\n%c", 0, 1),
"kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR
"kwargs": ("%D", (0, maxint, ", ")),
"kwargs1": ("%D", (0, maxint, ", ")),
"assert_expr_or": ("%c or %c", 0, 2),
"assert_expr_and": ("%c and %c", 0, 2),
"print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only
"print_items_nl_stmt": ("%|print %c%c\n", 0, 2),
"print_item": (", %c", 0),
"print_nl": ("%|print\n",),
"print_to": ("%|print >> %c, %c,\n", 0, 1),
"print_to_nl": ("%|print >> %c, %c\n", 0, 1),
"print_nl_to": ("%|print >> %c\n", 0),
"print_to_items": ("%C", (0, 2, ", ")),
# This is only generated by transform
# it is a string at the beginning of a function that is *not* a docstring
# 3.7 test_fstring.py tests for this kind of crap.
# For compatibility with older Python, we'll use "%" instead of
# a format string.
"string_at_beginning": ('%|"%%s" %% %c\n', 0),
"call_stmt": ( "%|%p\n",
# When a call statement contains only a named_expr (:=)
# the named_expr should have parenthesis around it.
(0, PRECEDENCE["named_expr"]-1)),
"break": ("%|break\n",),
"continue": ("%|continue\n",),
"raise_stmt0": ("%|raise\n",),
"raise_stmt1": ("%|raise %c\n", 0),
"raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2),
# "yield": ( "yield %c", 0),
# Note: we have a custom rule, which calls when we don't
# have "return None"
"return": ( "%|return %c\n", 0),
"return_if_stmt": ("return %c\n", 0),
"ifstmt": (
"%|if %c:\n%+%c%-",
0, # "testexpr" or "testexpr_then"
1, # "_ifstmts_jump" or "return_stmts"
),
"iflaststmt": ("%|if %c:\n%+%c%-", 0, 1),
"iflaststmtl": ("%|if %c:\n%+%c%-", 0, 1),
"testtrue": ("not %p", (0, PRECEDENCE["unary_not"])),
# Generally the args here are 0: (some sort of) "testexpr",
# 1: (some sort of) "cstmts_opt",
# 2 or 3: "else_suite"
# But unfortunately there are irregularities, For example, 2.6- uses "testexpr_then"
# and sometimes "cstmts" instead of "cstmts_opt" happens.
# Down the line we might isolate these into version-specific rules.
"ifelsestmt": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtc": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtl": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
# These are created only via transformation
"ifelifstmt": ("%|if %c:\n%+%c%-%c", 0, 1, 3), # "testexpr" or "testexpr_then"
"elifelifstmt": ("%|elif %c:\n%+%c%-%c", 0, 1, 3),
"elifstmt": ("%|elif %c:\n%+%c%-", 0, 1),
"elifelsestmt": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtr": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 2),
"ifelsestmtr2": ("%|if %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 3), # has COME_FROM
"elifelsestmtr": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 2),
"elifelsestmtr2": (
"%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n",
@@ -316,22 +421,20 @@ TABLE_DIRECT = {
1,
3,
), # has COME_FROM
"elifstmt": ("%|elif %c:\n%+%c%-", 0, 1),
"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, "")),
"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")),
@@ -357,50 +460,24 @@ TABLE_DIRECT = {
(4, "for_block"),
-2,
),
"function_def": ("\n\n%|def %c\n", -2), # -2 to handle closures
"function_def_deco": ("\n\n%c", 0),
"gen_comp_body": ("%c", 0),
"get_iter": ("iter(%c)", (0, "expr"),),
"if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4),
"if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4),
"if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2),
"if_exp_ret": ("%p if %p else %p", (2, 27), (0, 27), (-1, 27)),
"if_exp_not": (
"%p if not %p else %p",
(2, 27),
(0, "expr", PRECEDENCE["unary_not"]),
(4, 27),
),
"if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4),
# Generally the args here are 0: (some sort of) "testexpr",
# 1: (some sort of) "cstmts_opt",
# 2 or 3: "else_suite"
# But unfortunately there are irregularities, For example, 2.6- uses "testexpr_then"
# and sometimes "cstmts" instead of "cstmts_opt" happens.
# Down the line we might isolate these into version-specific rules.
"ifelsestmt": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtc": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtl": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
# This is created only via transformation.
"ifelifstmt": ("%|if %c:\n%+%c%-%c", 0, 1, 3), # "testexpr" or "testexpr_then"
"ifelsestmtr": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 2),
"ifelsestmtr2": ("%|if %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 3), # has COME_FROM
"iflaststmt": ("%|if %c:\n%+%c%-", 0, 1),
"iflaststmtl": ("%|if %c:\n%+%c%-", 0, 1),
"ifstmt": (
"%|if %c:\n%+%c%-",
0, # "testexpr" or "testexpr_then"
1, # "_ifstmts_jump" or "return_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),
"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, ", ")),
@@ -414,155 +491,7 @@ TABLE_DIRECT = {
"import_from_star": (
"%|from %[2]{pattr} import *\n",
),
"kv": ("%c: %c", 3, 1),
"kv2": ("%c: %c", 1, 2),
"kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR
"kwargs": ("%D", (0, maxint, ", ")),
"kwargs1": ("%D", (0, maxint, ", ")),
"lc_body": ("",), # ignore when recursing
"list_iter": ("%c", 0),
"list_for": (" for %c in %c%c", 2, 0, 3),
"list_if": (" if %p%c", (0, "expr", 27), 2),
"list_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2),
"mkfuncdeco": ("%|@%c\n%c", (0, "expr"), 1),
# A custom rule in n_function def distinguishes whether to call this or
# function_def_async
"mkfuncdeco0": ("%|def %c\n", (0, ("mkfunc", "mkfunc_annotate"))),
# In cases where we desire an explicit new line.
# After docstrings which are followed by a "def" is
# one situations where Python formatting desires two newlines,
# and this is added, as a transformation rule.
"newline": ("\n"),
"or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])),
"pass": ("%|pass\n",),
"print_item": (", %c", 0),
"print_items_nl_stmt": ("%|print %c%c\n", 0, 2),
"print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only
"print_nl": ("%|print\n",),
"print_nl_to": ("%|print >> %c\n", 0),
"print_to": ("%|print >> %c, %c,\n", 0, 1),
"print_to_items": ("%C", (0, 2, ", ")),
"print_to_nl": ("%|print >> %c, %c\n", 0, 1),
"raise_stmt0": ("%|raise\n",),
"raise_stmt1": ("%|raise %c\n", 0),
"raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2),
"ret_and": ("%c and %c", 0, 2),
"ret_or": ("%c or %c", 0, 2),
# Note: we have a custom rule, which calls when we don't
# have "return None"
"return": ( "%|return %c\n", 0),
"set_comp_body": ("%c", 0),
"set_iter": ( "%c", 0 ),
"return_if_stmt": ("return %c\n", 0),
"slice0": (
"%c[:]",
(0, "expr"),
),
"slice1": (
"%c[%p:]",
(0, "expr"),
(1, NO_PARENTHESIS_EVER)
),
"slice2": ( "%c[:%p]",
(0, "expr"),
(1, NO_PARENTHESIS_EVER)
),
"slice3": (
"%c[%p:%p]",
(0, "expr"),
(1, NO_PARENTHESIS_EVER),
(2, NO_PARENTHESIS_EVER)
),
"store_subscript": (
"%p[%c]",
(0, "expr", PRECEDENCE["subscript"]), (1, "expr")
),
# This nonterminal we create on the fly in semantic routines
"store_w_parens": (
"(%c).%[1]{pattr}",
(0, "expr")
),
# This is only generated by transform
# it is a string at the beginning of a function that is *not* a docstring
# 3.7 test_fstring.py tests for this kind of crap.
# For compatibility with older Python, we'll use "%" instead of
# a format string.
"string_at_beginning": ('%|"%%s" %% %c\n', 0),
"subscript": (
"%p[%p]",
(0, "expr", PRECEDENCE["subscript"]),
(1, "expr", NO_PARENTHESIS_EVER)
),
"subscript2": (
"%p[%p]",
(0, "expr", PRECEDENCE["subscript"]),
(1, "expr", NO_PARENTHESIS_EVER)
),
"testtrue": ("not %p", (0, PRECEDENCE["unary_not"])),
# 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),
"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),
"tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5),
# unary_op (formerly "unary_expr") is the Python AST UnaryOp
"unary_op": ("%c%c", (1, "unary_operator"), (0, "expr")),
"unary_not": ("not %c", (0, "expr")),
"unary_convert": ("`%c`", (0, "expr"),),
"unpack": ("%C%,", (1, maxint, ", ")),
"unpack_list": ("[%C]", (1, maxint, ", ")),
# This nonterminal we create on the fly in semantic routines
"unpack_w_parens": ("(%C%,)", (1, maxint, ", ")),
"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")),
),
# "yield": ( "yield %c", 0),
}
# fmt: on
MAP_DIRECT = (TABLE_DIRECT,)
@@ -575,7 +504,7 @@ MAP = {
"store": MAP_R,
}
ASSIGN_TUPLE_PARAM = lambda param_name: SyntaxTree( # noqa
ASSIGN_TUPLE_PARAM = lambda param_name: SyntaxTree(
"expr", [Token("LOAD_FAST", pattr=param_name)]
)

View File

@@ -17,25 +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),
"and_then": ("%c and %c", (0, "expr"), (4, "expr")),
}
)
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
@@ -43,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

@@ -1,4 +1,4 @@
# Copyright (c) 2019-2024 by Rocky Bernstein
# Copyright (c) 2019-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
@@ -15,17 +15,16 @@
"""Isolate Python 3.6 version-specific semantic actions here.
"""
from spark_parser.ast import GenericASTTraversalPruningException
from xdis import iscode
from spark_parser.ast import GenericASTTraversalPruningException
from uncompyle6.scanners.tok import Token
from uncompyle6.semantics.helper import flatten_list, escape_string, strip_quotes
from uncompyle6.semantics.consts import (
INDENT_PER_LEVEL,
PRECEDENCE,
TABLE_DIRECT,
TABLE_R,
)
from uncompyle6.semantics.helper import escape_string, flatten_list, strip_quotes
from uncompyle6.util import get_code_name
@@ -39,6 +38,7 @@ def escape_format(s):
def customize_for_version36(self, version):
# fmt: off
PRECEDENCE["call_kw"] = 0
PRECEDENCE["call_kw36"] = 1
@@ -276,7 +276,7 @@ def customize_for_version36(self, version):
if value == "":
fmt = "%c(%p)"
else:
fmt = "%c" + ("(%s, " % value).replace("%", "%%") + "%p)"
fmt = "%%c(%s, %%p)" % value
self.template_engine(
(fmt, (0, "expr"), (2, "build_map_unpack_with_call", 100)), node
@@ -295,7 +295,7 @@ def customize_for_version36(self, version):
if value == "":
fmt = "%c(%p)"
else:
fmt = "%c" + ("(%s, " % value).replace("%", "%%") + "%p)"
fmt = "%%c(%s, %%p)" % value
self.template_engine(
(fmt, (0, "expr"), (2, "build_map_unpack_with_call", 100)), node
@@ -707,7 +707,6 @@ def customize_for_version36(self, version):
self.comprehension_walk_newer(node, iter_index=3, code_index=0)
self.write("]")
self.prune()
self.n_list_comp_async = n_list_comp_async
# def kwargs_only_36(node):

View File

@@ -20,7 +20,6 @@ import re
from uncompyle6.semantics.consts import INDENT_PER_LEVEL, PRECEDENCE, TABLE_DIRECT
from uncompyle6.semantics.helper import flatten_list
# FIXME get from a newer xdis
FSTRING_CONVERSION_MAP = {1: "!s", 2: "!r", 3: "!a", "X": ":X"}

Some files were not shown because too many files have changed in this diff Show More