Skip to content

bpo-45045: Optimize mapping patterns of structural pattern matching #28043

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

Merged
merged 4 commits into from
Aug 30, 2021
Merged
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
13 changes: 13 additions & 0 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,19 @@ def f(x):
self.assertEqual(f((False, range(-1, -11, -1), True)), alts[3])
self.assertEqual(f((False, range(10, 20), True)), alts[4])

def test_patma_248(self):
class C(dict):
@staticmethod
def get(key, default=None):
return 'bar'

x = C({'foo': 'bar'})
match x:
case {'foo': bar}:
y = bar

self.assertEqual(y, 'bar')


class TestSyntaxErrors(unittest.TestCase):

Expand Down
23 changes: 17 additions & 6 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,18 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
PyObject *seen = NULL;
PyObject *dummy = NULL;
PyObject *values = NULL;
PyObject *get_name = NULL;
PyObject *get = NULL;
// We use the two argument form of map.get(key, default) for two reasons:
// - Atomically check for a key and get its value without error handling.
// - Don't cause key creation or resizing in dict subclasses like
// collections.defaultdict that define __missing__ (or similar).
_Py_IDENTIFIER(get);
PyObject *get = _PyObject_GetAttrId(map, &PyId_get);
get_name = _PyUnicode_FromId(&PyId_get); // borrowed
if (get_name == NULL) {
return NULL;
}
int meth_found = _PyObject_GetMethod(map, get_name, &get);
if (get == NULL) {
goto fail;
}
Expand All @@ -859,7 +865,7 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
if (dummy == NULL) {
goto fail;
}
values = PyList_New(0);
values = PyTuple_New(nkeys);
Copy link
Member Author

Choose a reason for hiding this comment

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

The size of the tuple is predictable.

if (values == NULL) {
goto fail;
}
Expand All @@ -873,7 +879,14 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
}
goto fail;
}
PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL);
PyObject *args[] = { map, key, dummy };
PyObject *value = NULL;
if (meth_found) {
value = PyObject_Vectorcall(get, args, 3, NULL);
}
else {
value = PyObject_Vectorcall(get, &args[1], 2, NULL);
}
if (value == NULL) {
goto fail;
}
Expand All @@ -886,10 +899,8 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
values = Py_None;
goto done;
}
PyList_Append(values, value);
Py_DECREF(value);
PyTuple_SET_ITEM(values, i, value);
}
Py_SETREF(values, PyList_AsTuple(values));
// Success:
done:
Py_DECREF(get);
Expand Down