FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143055: Fix crash in AST unparser when unparsing dict comprehension unpacking by StanFromIreland · Pull Request #145556 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (4) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions Doc/conf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@
# mapping unique short aliases to a base URL and a prefix.
# https://www.sphinx-doc.org/en/master/usage/extensions/extlinks.html
extlinks = {
"oss-fuzz": ("https://issues.oss-fuzz.com/issues/%s", "#%s"),
"pypi": ("https://pypi.org/project/%s/", "%s"),
"source": (SOURCE_URI, "%s"),
}
Expand Down
10 changes: 7 additions & 3 deletions Lib/_ast_unparse.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,13 @@ def visit_SetComp(self, node):

def visit_DictComp(self, node):
with self.delimit("{", "}"):
self.traverse(node.key)
self.write(": ")
self.traverse(node.value)
if node.value:
self.traverse(node.key)
self.write(": ")
self.traverse(node.value)
else:
self.write("**")
self.traverse(node.key)
for gen in node.generators:
self.traverse(gen)

Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_future_stmt/test_future.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ def test_annotations(self):
eq("(i ** 2 + j for i in (1, 2, 3) for j in (1, 2, 3))")
eq("{i: 0 for i in (1, 2, 3)}")
eq("{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}")
eq("{**x for x in ()}")
eq("[*x for x in ()]")
eq("[(x, y) for x, y in (a, b)]")
eq("[(x,) for x, in (a,)]")
eq("Python3 > Python2 > COBOL")
Comment thread
StanFromIreland marked this conversation as resolved.
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_unparse.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ def test_set_comprehension(self):
def test_dict_comprehension(self):
self.check_ast_roundtrip("{x: x*x for x in range(10)}")

def test_dict_comprehension_unpacking(self):
self.check_ast_roundtrip("{**x for x in ()}")
self.check_ast_roundtrip("{**x for x in range(10)}")
Comment thread
StanFromIreland marked this conversation as resolved.
self.check_ast_roundtrip("[*x for x in ()]")

def test_class_decorators(self):
self.check_ast_roundtrip(class_decorator)

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in AST unparser when unparsing dict comprehension unpacking.
Found by OSS Fuzz in :oss-fuzz:`489790200`.
12 changes: 9 additions & 3 deletions Python/ast_unparse.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,15 @@ static int
append_ast_dictcomp(PyUnicodeWriter *writer, expr_ty e)
{
APPEND_CHAR('{');
APPEND_EXPR(e->v.DictComp.key, PR_TEST);
APPEND_STR(": ");
APPEND_EXPR(e->v.DictComp.value, PR_TEST);
if (e->v.DictComp.value) {
APPEND_EXPR(e->v.DictComp.key, PR_TEST);
APPEND_STR(": ");
APPEND_EXPR(e->v.DictComp.value, PR_TEST);
}
else {
APPEND_STR("**");
APPEND_EXPR(e->v.DictComp.key, PR_TEST);
}
APPEND(comprehensions, e->v.DictComp.generators);
APPEND_CHAR_FINISH('}');
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL