Skip to content

[mypyc] Use get() to look up keys in mapping patterns - #22563

Open
Jahongir-Qurbonov wants to merge 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-match-mapping-get
Open

Jahongir-Qurbonov wants to merge 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-match-mapping-get

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

Mapping patterns (case {"a": v}) were compiled using PyMapping_HasKey() followed by __getitem__, while CPython looks up each key using x.get(key, <sentinel>). This caused several differences:

  • With a defaultdict, the pattern always matched and the key was added to the dict, since __getitem__ calls __missing__.
  • An overridden get method was ignored, and an overridden __getitem__ was called instead.
  • PyMapping_HasKey() swallows exceptions, so an exception raised by __getitem__ was ignored (and printed as "Exception ignored ..." on 3.13+).

Now we call get with a sentinel object, as CPython does. If the subject has a dict type, this uses the existing dict.get primitive. The sentinel is the existing _CPy_ExcDummy object, which isn't otherwise visible to Python code.

CPython looks up keys in mapping patterns using get() with a sentinel
default. We used PyMapping_HasKey() and __getitem__, which called
__missing__ in defaultdict (adding the key), ignored overridden get()
methods and swallowed exceptions.
The expected output includes the 64-bit value of PY_VECTORCALL_ARGUMENTS_OFFSET.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant