Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions mypyc/irbuild/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
)
from mypy.traverser import TraverserVisitor
from mypy.types import Instance, LiteralType, TupleType, get_proper_type
from mypyc.ir.ops import BasicBlock, Value
from mypyc.ir.ops import BasicBlock, LoadAddress, Value
from mypyc.ir.rtypes import object_rprimitive
from mypyc.irbuild.builder import IRBuilder
from mypyc.primitives.dict_ops import (
dict_copy,
dict_del_item,
mapping_has_key,
supports_mapping_protocol,
)
from mypyc.primitives.dict_ops import dict_copy, dict_del_item, supports_mapping_protocol
from mypyc.primitives.generic_ops import generic_ssize_t_len_op
from mypyc.primitives.list_ops import (
sequence_get_item,
Expand Down Expand Up @@ -220,16 +215,22 @@ def visit_mapping_pattern(self, pattern: MappingPattern) -> None:
key_value = self.builder.accept(key)
keys.append(key_value)

exists = self.builder.call_c(mapping_has_key, [self.subject, key_value], pattern.line)
# Like CPython, look up the key using x.get(key, <sentinel>). The sentinel is an
# object that is private to mypyc, but a user-defined get() method can see it.
# If the subject is a dict, the dict.get primitive is used, which doesn't call
# a get() method overridden in a dict subclass.
missing = self.builder.add(
LoadAddress(object_rprimitive, "_CPy_ExcDummyStruct", pattern.line)
)
item = self.builder.gen_method_call(
self.subject, "get", [key_value, missing], object_rprimitive, pattern.line
)
exists = self.builder.binary_op(item, missing, "is not", pattern.line)

self.builder.add_bool_branch(exists, self.code_block, self.next_block)
self.builder.activate_block(self.code_block)
self.code_block = BasicBlock()

item = self.builder.gen_method_call(
self.subject, "__getitem__", [key_value], object_rprimitive, pattern.line
)

with self.enter_subpattern(item):
value.accept(self)

Expand Down
7 changes: 0 additions & 7 deletions mypyc/primitives/dict_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,3 @@
c_function_name="CPyMapping_Check",
error_kind=ERR_NEVER,
)

mapping_has_key = custom_op(
arg_types=[object_rprimitive, object_rprimitive],
return_type=c_int_rprimitive,
c_function_name="PyMapping_HasKey",
error_kind=ERR_NEVER,
)
168 changes: 91 additions & 77 deletions mypyc/test-data/irbuild-match.test
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ L2:
L3:
r9 = box(None, 1)
return r9
[case testMatchMappingPatternWithKeys_python3_10]
[case testMatchMappingPatternWithKeys_python3_10_64bit]
def f(x):
match x:
case {"key": "value"}:
Expand All @@ -1061,52 +1061,59 @@ def f(x):
r0 :: i32
r1 :: bit
r2 :: str
r3 :: i32
r4 :: bit
r5 :: object
r6 :: str
r3 :: object
r4 :: str
r5 :: object[3]
r6 :: object_ptr
r7 :: object
r8 :: i32
r9 :: bit
r10 :: bool
r11 :: str
r12 :: object
r13 :: str
r14 :: object
r15 :: object[1]
r16 :: object_ptr
r17, r18 :: object
r8 :: bit
r9 :: str
r10 :: object
r11 :: i32
r12 :: bit
r13 :: bool
r14 :: str
r15 :: object
r16 :: str
r17 :: object
r18 :: object[1]
r19 :: object_ptr
r20, r21 :: object
L0:
r0 = CPyMapping_Check(x)
r1 = r0 != 0
if r1 goto L1 else goto L4 :: bool
L1:
r2 = 'key'
r3 = PyMapping_HasKey(x, r2)
r4 = r3 != 0
if r4 goto L2 else goto L4 :: bool
r3 = load_address _CPy_ExcDummyStruct
r4 = 'get'
r5 = [x, r2, r3]
r6 = load_address r5
r7 = PyObject_VectorcallMethod(r4, r6, 9223372036854775811, 0)
keep_alive x, r2, r3
r8 = r7 != r3
if r8 goto L2 else goto L4 :: bool
L2:
r5 = PyObject_GetItem(x, r2)
r6 = 'value'
r7 = PyObject_RichCompare(r5, r6, 2)
r8 = PyObject_IsTrue(r7)
r9 = r8 >= 0 :: signed
r10 = truncate r8: i32 to builtins.bool
if r10 goto L3 else goto L4 :: bool
r9 = 'value'
r10 = PyObject_RichCompare(r7, r9, 2)
r11 = PyObject_IsTrue(r10)
r12 = r11 >= 0 :: signed
r13 = truncate r11: i32 to builtins.bool
if r13 goto L3 else goto L4 :: bool
L3:
r11 = 'matched'
r12 = builtins :: module
r13 = 'print'
r14 = CPyObject_GetAttr(r12, r13)
r15 = [r11]
r16 = load_address r15
r17 = PyObject_Vectorcall(r14, r16, 1, 0)
keep_alive r11
r14 = 'matched'
r15 = builtins :: module
r16 = 'print'
r17 = CPyObject_GetAttr(r15, r16)
r18 = [r14]
r19 = load_address r18
r20 = PyObject_Vectorcall(r17, r19, 1, 0)
keep_alive r14
goto L5
L4:
L5:
r18 = box(None, 1)
return r18
r21 = box(None, 1)
return r21
[case testMatchMappingPatternWithRest_python3_10]
def f(x):
match x:
Expand Down Expand Up @@ -1146,7 +1153,7 @@ L3:
L4:
r10 = box(None, 1)
return r10
[case testMatchMappingPatternWithRestPopKeys_python3_10]
[case testMatchMappingPatternWithRestPopKeys_python3_10_64bit]
def f(x):
match x:
case {"key": "value", **rest}:
Expand All @@ -1157,60 +1164,67 @@ def f(x):
r0 :: i32
r1 :: bit
r2 :: str
r3 :: i32
r4 :: bit
r5 :: object
r6 :: str
r3 :: object
r4 :: str
r5 :: object[3]
r6 :: object_ptr
r7 :: object
r8 :: i32
r9 :: bit
r10 :: bool
r11, rest :: dict
r12 :: i32
r13 :: bit
r14 :: str
r15 :: object
r16 :: str
r17 :: object
r18 :: object[1]
r19 :: object_ptr
r20, r21 :: object
r8 :: bit
r9 :: str
r10 :: object
r11 :: i32
r12 :: bit
r13 :: bool
r14, rest :: dict
r15 :: i32
r16 :: bit
r17 :: str
r18 :: object
r19 :: str
r20 :: object
r21 :: object[1]
r22 :: object_ptr
r23, r24 :: object
L0:
r0 = CPyMapping_Check(x)
r1 = r0 != 0
if r1 goto L1 else goto L5 :: bool
L1:
r2 = 'key'
r3 = PyMapping_HasKey(x, r2)
r4 = r3 != 0
if r4 goto L2 else goto L5 :: bool
r3 = load_address _CPy_ExcDummyStruct
r4 = 'get'
r5 = [x, r2, r3]
r6 = load_address r5
r7 = PyObject_VectorcallMethod(r4, r6, 9223372036854775811, 0)
keep_alive x, r2, r3
r8 = r7 != r3
if r8 goto L2 else goto L5 :: bool
L2:
r5 = PyObject_GetItem(x, r2)
r6 = 'value'
r7 = PyObject_RichCompare(r5, r6, 2)
r8 = PyObject_IsTrue(r7)
r9 = r8 >= 0 :: signed
r10 = truncate r8: i32 to builtins.bool
if r10 goto L3 else goto L5 :: bool
r9 = 'value'
r10 = PyObject_RichCompare(r7, r9, 2)
r11 = PyObject_IsTrue(r10)
r12 = r11 >= 0 :: signed
r13 = truncate r11: i32 to builtins.bool
if r13 goto L3 else goto L5 :: bool
L3:
r11 = CPyDict_FromAny(x)
rest = r11
r12 = PyDict_DelItem(r11, r2)
r13 = r12 >= 0 :: signed
r14 = CPyDict_FromAny(x)
rest = r14
r15 = PyDict_DelItem(r14, r2)
r16 = r15 >= 0 :: signed
L4:
r14 = 'matched'
r15 = builtins :: module
r16 = 'print'
r17 = CPyObject_GetAttr(r15, r16)
r18 = [r14]
r19 = load_address r18
r20 = PyObject_Vectorcall(r17, r19, 1, 0)
keep_alive r14
r17 = 'matched'
r18 = builtins :: module
r19 = 'print'
r20 = CPyObject_GetAttr(r18, r19)
r21 = [r17]
r22 = load_address r21
r23 = PyObject_Vectorcall(r20, r22, 1, 0)
keep_alive r17
goto L6
L5:
L6:
r21 = box(None, 1)
return r21
r24 = box(None, 1)
return r24
[case testMatchEmptySequencePattern_python3_10]
def f(x):
match x:
Expand Down
50 changes: 50 additions & 0 deletions mypyc/test-data/run-match.test
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,53 @@ for fn in (f, g):
[out]
RuntimeError boom
RuntimeError boom

[case testMappingPatternUsesGet_python3_10]
from typing import Any, Dict

def f(x: object) -> object:
match x:
case {"a": v}:
return v
return "no match"

def g(x: Dict[str, int]) -> object:
match x:
case {"a": v}:
return v
return "no match"

[file driver.py]
from collections import defaultdict
from testutil import assertRaises

from native import f, g

class VirtualKey(dict):
def get(self, key, default=None):
if key == "a":
return 1
return super().get(key, default)

class GetRaises(dict):
def get(self, key, default=None):
raise RuntimeError("get")

class GetItemRaises(dict):
def __getitem__(self, key):
raise RuntimeError("__getitem__")

# defaultdict.get() doesn't call __missing__
d = defaultdict(int)
assert f(d) == "no match"
assert g(d) == "no match"
assert d == {}

assert f({"a": None}) is None
assert f({"b": 1}) == "no match"
assert g({"a": 5}) == 5

assert f(VirtualKey()) == 1
assert f(GetItemRaises(a=3)) == 3
with assertRaises(RuntimeError, "get"):
f(GetRaises(a=1))
Loading