Skip to content

Commit

Permalink
grammar and rules for chained compares with "and"
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed May 31, 2020
1 parent d0ff4d8 commit fd2865b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion __pkginfo__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
]
}
ftp_url = None
install_requires = ["spark-parser >= 1.8.9, < 1.9.0", "xdis >= 4.6.0, < 4.7.0"]
install_requires = ["spark-parser >= 1.8.9, < 1.9.0", "xdis >= 4.6.1, < 4.7.0"]

license = "GPL3"
mailing_list = "[email protected]"
Expand Down
2 changes: 2 additions & 0 deletions decompyle3/parsers/p37/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ def p_grammar(self, args):
testtruec ::= c_nand
testtrue ::= compare_chained37
testtrue ::= compare_chained_and
testtrue ::= nor_cond
testfalse ::= and_not
Expand Down
12 changes: 12 additions & 0 deletions decompyle3/parsers/p37/lambda_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ def p_37chained(self, args):
compare_chained ::= compare_chained37
compare_chained ::= compare_chained37_false
compare_chained_and ::= expr chained_parts
compare_chained2a_false_37
come_froms
POP_TOP JUMP_FORWARD COME_FROM
negated_testtrue
come_froms
# We don't use testtrue directly because we need to tell the semantic
# action to negate the testtrue
negated_testtrue ::= testtrue
c_compare_chained ::= c_compare_chained37_false
compare_chained37 ::= expr chained_parts
Expand Down
16 changes: 16 additions & 0 deletions decompyle3/semantics/customize37.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ def customize_for_version37(self, version):
' %[3]{pattr.replace("-", " ")} %p',
(0, "expr", PRECEDENCE["compare"]-1),
),

"compare_chained_and": (
"%c%c and %c",
(0, "expr"),
(1, "chained_parts"),
-2, # Is often a transformed negated_testtrue
),

# This is eliminated in the transform phase, but
# we have it here to be logically complete and more robust
# if something goes wrong.
"negated_testtrue": (
"not %c",
(0, "testtrue"),
),

"compare_chained1c_37": (
"%p %p",
(0, PRECEDENCE["compare"]-1),
Expand Down
1 change: 1 addition & 0 deletions decompyle3/semantics/customize38.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def customize_for_version38(self, version):
(2, "store"),
(0, "expr"),
(3, "for_block"), -1 ),

"except_cond1a": (
"%|except %c:\n", (1, "expr"),
),
Expand Down
6 changes: 6 additions & 0 deletions decompyle3/semantics/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ def n_list_for(self, list_for_node):
list_for_node.transformed_by = ("n_list_for",)
return list_for_node

def n_negated_testtrue(self, node):
assert node[0] == "testtrue"
test_node = node[0][0]
test_node.transformed_by = "n_negated_testtrue"
return test_node

def n_stmts(self, node):
if node.first_child() == "SETUP_ANNOTATIONS":
prev = node[0]
Expand Down
Binary file added test/bytecode_3.7/02_and_chained_compare.pyc
Binary file not shown.
Binary file added test/bytecode_3.8/02_and_chained_compare.pyc
Binary file not shown.
28 changes: 28 additions & 0 deletions test/simple_source/bug37/02_and_chained_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# In Python 3.7+ and/or's have gotten a lot smarter and are harder to
# deal with.

# See:
# https://github.com/rocky/python-uncompyle6/issues/317

"""This program is self-checking!"""

def chained_compare_and(a, b):
assert (0 <= a <= 10 and
10 <= b <= 20)
return 6

assert chained_compare_and(5, 15) == 6
try:
chained_compare_and(13, 16)
except:
pass
else:
raise


try:
chained_compare_and(4, 8)
except:
pass
else:
raise

0 comments on commit fd2865b

Please sign in to comment.