GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-27494: Fix a 2to3 parser bug (trailing comma) by jstasiak · Pull Request #60 · python/cpython

bpo-27494: Fix a 2to3 parser bug (trailing comma) #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jstasiak wants to merge 1 commit into python:master from jstasiak:fix-lib2to3

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-27494: Fix a 2to3 parser bug (trailing comma) #60

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
71b60a5
bpo-27494: Fix 2to3 handling of trailing comma after a generator expr…
jstasiak Feb 13, 2017
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

25 changes: 22 additions & 3 deletions 25 Lib/lib2to3/Grammar.txt
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ atom: ('(' [yield_expr|testlist_gexp] ')' |
'{' [dictsetmaker] '}' |
'`' testlist1 '`' |
NAME | NUMBER | STRING+ | '.' '.' '.')
listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
listmaker: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
testlist_gexp: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
Expand Down Expand Up @@ -161,9 +161,28 @@ argument: ( test [comp_for] |
star_expr )

comp_iter: comp_for | comp_if
comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
comp_if: 'if' old_test [comp_iter]

# See note above regarding testlist_safe to see why testlist_safe is
# necessary. Unfortunately we can't use it indiscriminately in all derivations
# using comp_for-like pattern because testlist_safe derivation contains
# comma which clashes with trailing comma in arglist.
#
# The above was an issue because the parser would not follow the correct derivation
# when parsing syntactically valid Python code. Since testlist_safe was created
# specifically to handle list comprehensions and generator expressions enclosed
# with parentheses it's safe to only use it in those. That avoids the issue
# mentioned above and we can parse code like set(x for x in [],).
#
# Note that the syntax supported by this set of rules is not a valid Python 3
# syntax, hence the prefix "old".
#
# See the bug report: http://bugs.python.org/issue27494
old_comp_iter: old_comp_for | old_comp_if
old_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter]
old_comp_if: 'if' old_test [old_comp_iter]

testlist1: test (',' test)*

# not used in grammar, but may appear in "node" passed from Parser to Compiler
Expand Down
5 changes: 3 additions & 2 deletions 5 Lib/lib2to3/fixer_util.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def ListComp(xp, fp, it, test=None):
test.prefix = " "
if_leaf = Leaf(token.NAME, "if")
if_leaf.prefix = " "
inner_args.append(Node(syms.comp_if, [if_leaf, test]))
inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
inner_args.append(Node(syms.old_comp_if, [if_leaf, test]))
inner = Node(syms.listmaker, [xp, Node(syms.old_comp_for, inner_args)])
return Node(syms.atom,
[Leaf(token.LBRACE, "["),
inner,
Expand Down Expand Up @@ -208,6 +208,7 @@ def attr_chain(obj, attr):
next = getattr(next, attr)

p0 = """for_stmt< 'for' any 'in' node=any ':' any* >
| old_comp_for< 'for' any 'in' node=any any* >
| comp_for< 'for' any 'in' node=any any* >
"""
p1 = """
Expand Down
1 change: 1 addition & 0 deletions 1 Lib/lib2to3/fixes/fix_dict.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def transform(self, node, results):
p1 = patcomp.compile_pattern(P1)

P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
| old_comp_for< 'for' any 'in' node=any any* >
| comp_for< 'for' any 'in' node=any any* >
"""
p2 = patcomp.compile_pattern(P2)
Expand Down
4 changes: 2 additions & 2 deletions 4 Lib/lib2to3/fixes/fix_paren.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FixParen(fixer_base.BaseFix):
PATTERN = """
atom< ('[' | '(')
(listmaker< any
comp_for<
old_comp_for<
'for' NAME 'in'
target=testlist_safe< any (',' any)+ [',']
>
Expand All @@ -24,7 +24,7 @@ class FixParen(fixer_base.BaseFix):
>
|
testlist_gexp< any
comp_for<
old_comp_for<
'for' NAME 'in'
target=testlist_safe< any (',' any)+ [',']
>
Expand Down
1 change: 1 addition & 0 deletions 1 Lib/lib2to3/fixes/fix_xrange.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def transform_range(self, node, results):
p1 = patcomp.compile_pattern(P1)

P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
| old_comp_for< 'for' any 'in' node=any any* >
| comp_for< 'for' any 'in' node=any any* >
| comparison< any 'in' node=any any*>
"""
Expand Down
7 changes: 7 additions & 0 deletions 7 Lib/lib2to3/tests/test_parser.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ def test_multiline_str_literals(self):
self.validate(s)


class TestGeneratorExpressions(GrammarTest):

def test_trailing_comma_after_generator_expression_argument_works(self):
# BPO issue 27494
self.validate("set(x for x in [],)")


def diff(fn, result):
try:
with open('@', 'w') as f:
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2017-09-26-01-43-17.bpo-27494.37QnaT.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2to3 doesn't choke on a trailing comma following a generator expression
anymore.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.