This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author brandtbucher
Recipients Mark.Shannon, brandtbucher, gvanrossum, pablogsal
Date 2021-04-06.20:00:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1617739231.38.0.2365910918.issue43754@roundup.psfhosted.org>
In-reply-to
Content
This draft PR contains a couple of pattern matching improvements I'm experimenting with. Most significantly, it implements behavior that several people requested during the PEP 634 feedback process: no more name bindings for partial matches.

(One caveat: all names are bound *before* evaluating the corresponding guard. If the guard fails, these bindings won't be rolled back before matching the next case.)

In short, this is accomplished by storing captured objects on the stack underneath the more immediately important stuff, popping them all on failure or storing them all only once the *entire* pattern for that case matches. The control flow needs to be greatly simplified in order to make this work correctly, so the patch replaces the current "push bools and jump if false a bunch of times" flow with something much more efficient: jumping straight into a run of POP_TOPs on failure. We already do something very similar to this in pattern_helper_sequence_unpack (the comments in the current code explain it well).

Parts of the new code are a bit more complex than before (especially or-patterns, which need to do a bit of juggling on the stack to reorder captured items and avoid popping important data on failure), and other parts can certainly still be improved... but I personally consider it a win to have more intuitive partial match behavior with no performance penalty.

I thought I'd loop a few of you in on it to hear your thoughts before progressing much further.
History
Date User Action Args
2021-04-06 20:00:31brandtbuchersetrecipients: + brandtbucher, gvanrossum, Mark.Shannon, pablogsal
2021-04-06 20:00:31brandtbuchersetmessageid: <1617739231.38.0.2365910918.issue43754@roundup.psfhosted.org>
2021-04-06 20:00:31brandtbucherlinkissue43754 messages
2021-04-06 20:00:30brandtbuchercreate