Skip to content
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.

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

Enable parenthesized context managers #4814

Merged
merged 5 commits into from
Jul 17, 2022
Merged

Conversation

da-woods
Copy link
Contributor

@da-woods da-woods commented May 29, 2022

As described in https://docs.python.org/3/whatsnew/3.10.html#parenthesized-context-managers

The approach to parsing is largely copied from the CPython parser
(with comments to support it) - closer to the PEG approach of
"try the bracketed case first, and let it fail silently then
try the unbracketed case".

Fixes one of the items listed in #4595

@da-woods da-woods marked this pull request as draft May 29, 2022 09:12
else:
body = p_suite(s)
return Nodes.WithStatNode(pos, manager=manager, target=target, body=body, is_async=is_async)
return "with", pos, manager, target
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative approach (which might/might not be better) would be to create the nodes with an empty body, then fill in the body in the p_with_items loop. It might be a little more readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's certainly unusual to return anything but a node from a p_* parser function. I wouldn't mind setting the body after the fact. Seems simple and reasonable here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that there's quite a bit of logic in the constructor of GilStatNode that means that you have to know what body is when you construct it and can't set it later.

I've therefore gone back to something closer to what I had before where the construction of the nodes is delay. Although hopefully it's a little neater now.

As described in https://docs.python.org/3/whatsnew/3.10.html#parenthesized-context-managers

The approach to parsing is largely copied from the CPython parser
(with comments to support it) - closer to the PEG approach of
"try the bracketed case first, and let it fail silently then
try the unbracketed case".
@da-woods da-woods marked this pull request as ready for review July 16, 2022 15:42
@da-woods
Copy link
Contributor Author

Marked as non-draft now that #4813 is merged

tests/run/test_grammar.py Show resolved Hide resolved
else:
body = p_suite(s)
return Nodes.WithStatNode(pos, manager=manager, target=target, body=body, is_async=is_async)
return "with", pos, manager, target
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's certainly unusual to return anything but a node from a p_* parser function. I wouldn't mind setting the body after the fact. Seems simple and reasonable here.

@da-woods
Copy link
Contributor Author

It's certainly unusual to return anything but a node from a p_* parser function. I wouldn't mind setting the body after the fact. Seems simple and reasonable here.

Done

@scoder scoder merged commit 9c6140b into cython:master Jul 17, 2022
@scoder
Copy link
Contributor

scoder commented Jul 17, 2022

Thanks!

@da-woods da-woods deleted the with-parentheses branch July 17, 2022 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants