4 ms·
Does anyone use this and have a good example they've round-tripped through the CPython compiler and this decompiler? I would assume variable names are lost, li
by loeg 8y ago
Does anyone use this and have a good example they've round-tripped through the CPython compiler and this decompiler? I would assume variable names are lost, like in most other bytecode compiler languages.
- dbrueck 8y agoActually, variable names should largely /not/ be lost - Python is insanely dynamic and needs variable/function/etc. names to remain. At most you might lose the names of local variables inside a function (i.e. variables that, due to internal scope, have a limited lifespan), but member variables, global variables, function names, class names, method names, etc. would all need to be kept in the bytecode.
- ben-schaaf 8y agoConsidering `globals` and `locals` exist I would be very surprised if any name got lost. Python is so dynamic you can actually mutate the bytecode of a function at runtime[0] (LISP esque), so the compiler can't actually make any assumptions and optimizations based on your source code. [0] https://pypi.org/project/byteplay/ https://pypi.org/project/byteplay/
- loeg 8y agoAh, that's unfortunate. I sort of assumed the compiler would be able to optimize stack variables and only do the slow thing if `locals` was actually invoked.
- sametmax 8y agoIt's a common joke to say that if you just shorter names in C, your program might run faster. It's not true of course, but it Python, it might. All those names are used in hash computations and look ups, and you may shave a few micro seconds if you make your program unmaintable.
- rockybernstein 8y agoActually, no, shortening names won't speed bytecode up in the way you seem to be suggesting. Variables "are" indexed by position in some arrays like co_varnames, co_names, co_const and so on. See for example https://docs.python.org/3.8/library/dis.html?highlight=co_names#opcode-LOAD_FAST https://docs.python.org/3.8/library/dis.html?highlight=co_na... There are maps in the code object that will turn the index back into a name, but that's only used when some sort of introspection occurs, such as you might find in a debugger or traceback. The names found in an "import" of course is a string so there might some small advantage there in a shorter name.
- sametmax 8y agoVariables are not the only names in a program. Attribute lookups do use a dictionary.
- rockybernstein 8y ago"Does anyone use this": I use it in the "deparse" command of my Pytohn 3 debugger trepan3k ( https://pypi.org/project/trepan3k/#exact-location-information https://pypi.org/project/trepan3k/#exact-location-informatio... ) and the corresponding Python 2 debugger trepan2. See http://rocky.github.io/pycon2018.co/#/16 http://rocky.github.io/pycon2018.co/#/16 for a video showing use in this context. As for round-tripping, I make use of that idea in testing the decompiler and/or looking for bugs (which are numerous). Python has an extensive test suite for its many library programs (in later versions in excesss of 800) which are largely written in Python. The cool thing about a unit test program is that it is self checking. uncompyle6 is far from perfect, so no, it doesn't roundrip all of the unit test programs that it could. (There are some unit test programs that are too fragile, like those that expect the line number to be exactly the same which might occur in tests testing a debugger). The biggest problem as I've said many times is handling control flow properly. However that could be solved much better if someone who had more time to spend on this wanted to.