Skip to content

Commit a215ee2

Browse files
committed
Use "co_consts" in docstring detection.
Note: this is an upheaval because we need to pass "code" or at least "code.co_consts" to the docstring detection routine
1 parent f62512d commit a215ee2

File tree

15 files changed

+75
-42
lines changed

15 files changed

+75
-42
lines changed

.github/ISSUE_TEMPLATE/bug-report.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@ about: Tell us about uncompyle6 bugs
44

55
---
66

7-
<!-- __Note:__ Bugs are not for asking questions about a problem you are trying to solve that involve the use of uncompyle6 along the way, although I may be more tolerent of this if you sponsor the project. Also, the unless you are a sponsor of the project, it may take a while, maybe a week or so, before the bug report is noticed, let alone acted upon. To set expectations, some legitimate bugs can take years to fix, but they eventually do get fixed. Funding the project was added to address the problem that there are lots of people seeking help and reporting bugs, but few people who are willing or capable of providing help or fixing bugs. Have you read https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md ?
7+
<!-- __Note:__ Bugs are not for asking questions about a problem you
8+
are trying to solve that involve the use of uncompyle6 along the way,
9+
although I may be more tolerent of this if you sponsor the project.
10+
11+
Also, the unless you are a sponsor of the project, it may take a
12+
while, maybe a week or so, before the bug report is noticed, let alone
13+
acted upon.
14+
15+
To set expectations, some legitimate bugs can take years
16+
to fix, but they eventually do get fixed. Funding the project was
17+
added to address the problem that there are lots of people seeking
18+
help and reporting bugs, but few people who are willing or capable of
19+
providing help or fixing bugs.
20+
21+
Finally, have you read https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md
22+
?
823
924
1025
Please remove any of the optional sections if they are not applicable.

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ valid bytecode before trying this tool. This program can't decompile
216216
Microsoft Windows EXE files created by Py2EXE_, although we can
217217
probably decompile the code after you extract the bytecode
218218
properly. Handling pathologically long lists of expressions or
219-
statements is slow. We don't handle Cython_ or MicroPython_ which don't use bytecode.
219+
statements is slow. We don't handle Cython_ or MicroPython which don't
220+
use bytecode.
220221

221222
There are numerous bugs in decompilation. And that's true for every
222223
other CPython decompiler I have encountered, even the ones that
@@ -263,7 +264,6 @@ See Also
263264

264265

265266
.. _Cython: https://en.wikipedia.org/wiki/Cython
266-
.. _MicroPython: https://micropotyon.org
267267
.. _trepan: https://pypi.python.org/pypi/trepan2g
268268
.. _compiler: https://pypi.python.org/pypi/spark_parser
269269
.. _HISTORY: https://github.com/rocky/python-uncompyle6/blob/master/HISTORY.md

test/add-test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
"""
44
import os, sys, py_compile
55

6-
assert len(sys.argv) >= 2
6+
assert (2 <= len(sys.argv) <= 4)
77
version = sys.version[0:3]
88
vers = sys.version_info[:2]
99
if sys.argv[1] in ("--run", "-r"):
1010
suffix = "_run"
1111
py_source = sys.argv[2:]
12+
i = 2
1213
else:
1314
suffix = ""
1415
py_source = sys.argv[1:]
16+
i = 1
17+
try:
18+
optimize = int(sys.argv[-1])
19+
py_source = sys.argv[i:-1]
20+
except:
21+
optimize = 2
1522

1623
for path in py_source:
1724
short = os.path.basename(path)
@@ -20,7 +27,7 @@
2027
else:
2128
cfile = "bytecode_%s%s/%s" % (version, suffix, short) + "c"
2229
print("byte-compiling %s to %s" % (path, cfile))
23-
optimize = 2
30+
optimize = optimize
2431
if vers > (3, 1):
2532
py_compile.compile(path, cfile, optimize=optimize)
2633
else:
251 Bytes
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# From 2.7.17 test_bdb.py
2+
# The problem was detecting a docstring at the begining of the module
3+
# It must be detected and change'd or else the "from __future__" below
4+
# is invalid.
5+
# Note that this has to be compiled with optimation < 2 or else optimization
6+
# will remove the docstring
7+
"""Rational, infinite-precision, real numbers."""
8+
9+
from __future__ import division

uncompyle6/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def p_store(self, args):
623623
"""
624624

625625

626-
def parse(p, tokens, customize):
626+
def parse(p, tokens, customize, code):
627627
p.customize_grammar_rules(tokens, customize)
628628
ast = p.parse(tokens)
629629
# p.cleanup()
@@ -878,7 +878,7 @@ def python_parser(
878878
# parser_debug = {'rules': True, 'transition': True, 'reduce' : True,
879879
# 'showstack': 'full'}
880880
p = get_python_parser(version, parser_debug)
881-
return parse(p, tokens, customize)
881+
return parse(p, tokens, customize, co)
882882

883883

884884
if __name__ == "__main__":

uncompyle6/semantics/aligner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def code_deparse_align(co, out=sys.stderr, version=None, is_pypy=None,
142142
is_pypy = is_pypy)
143143

144144
isTopLevel = co.co_name == '<module>'
145-
deparsed.ast = deparsed.build_ast(tokens, customize, isTopLevel=isTopLevel)
145+
deparsed.ast = deparsed.build_ast(tokens, customize, co, isTopLevel=isTopLevel)
146146

147147
assert deparsed.ast == 'stmts', 'Should have parsed grammar start'
148148

uncompyle6/semantics/consts.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,12 @@
142142
)
143143

144144
ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree(
145-
"stmt",
145+
"assign",
146146
[
147147
SyntaxTree(
148-
"assign",
149-
[
150-
SyntaxTree(
151-
"expr", [Token(doc_load, pattr=doc_string, attr=doc_string)]
152-
),
153-
SyntaxTree("store", [Token("STORE_NAME", pattr="__doc__")]),
154-
],
155-
)
148+
"expr", [Token(doc_load, pattr=doc_string, attr=doc_string)]
149+
),
150+
SyntaxTree("store", [Token("STORE_NAME", pattr="__doc__")]),
156151
],
157152
)
158153

uncompyle6/semantics/customize3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def listcomp_closure3(node):
9090
code_obj = node[1].attr
9191
assert iscode(code_obj)
9292
code = Code(code_obj, self.scanner, self.currentclass)
93-
ast = self.build_ast(code._tokens, code._customize)
93+
ast = self.build_ast(code._tokens, code._customize, code)
9494
self.customize(code._customize)
9595

9696
# skip over: sstmt, stmt, return, ret_expr

uncompyle6/semantics/fragments.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ def comprehension_walk(self, node, iter_index, code_index=-5):
681681
assert iscode(cn.attr)
682682

683683
code = Code(cn.attr, self.scanner, self.currentclass)
684-
ast = self.build_ast(code._tokens, code._customize)
684+
ast = self.build_ast(code._tokens, code._customize, code)
685685
self.customize(code._customize)
686686
ast = ast[0][0][0]
687687

@@ -728,7 +728,7 @@ def comprehension_walk3(self, node, iter_index, code_index=-5):
728728
code_name = code.co_name
729729
code = Code(code, self.scanner, self.currentclass)
730730

731-
ast = self.build_ast(code._tokens, code._customize)
731+
ast = self.build_ast(code._tokens, code._customize, code)
732732

733733
self.customize(code._customize)
734734
if ast[0] == "sstmt":
@@ -850,7 +850,7 @@ def listcomprehension_walk2(self, node):
850850
self.prec = 27
851851

852852
code = Code(node[1].attr, self.scanner, self.currentclass)
853-
ast = self.build_ast(code._tokens, code._customize)
853+
ast = self.build_ast(code._tokens, code._customize, code)
854854
self.customize(code._customize)
855855
if node == "set_comp":
856856
ast = ast[0][0][0]
@@ -992,7 +992,7 @@ def setcomprehension_walk3(self, node, collection_index):
992992
self.prec = 27
993993

994994
code = Code(node[1].attr, self.scanner, self.currentclass)
995-
ast = self.build_ast(code._tokens, code._customize)
995+
ast = self.build_ast(code._tokens, code._customize, code)
996996
self.customize(code._customize)
997997
ast = ast[0][0][0]
998998
store = ast[3]
@@ -1142,9 +1142,8 @@ def gen_source(self, ast, name, customize, is_lambda=False, returnNone=False):
11421142
self.name = old_name
11431143
self.return_none = rn
11441144

1145-
def build_ast(
1146-
self, tokens, customize, is_lambda=False, noneInNames=False, isTopLevel=False
1147-
):
1145+
def build_ast(self, tokens, customize, code, is_lambda=False,
1146+
noneInNames=False, isTopLevel=False):
11481147

11491148
# FIXME: DRY with pysource.py
11501149

0 commit comments

Comments
 (0)