Visitar URL original
More Pattern matching implementation mapping + class by youknowone · Pull Request #6110 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix defaultdict
  • Loading branch information
youknowone committed Aug 26, 2025
commit f4543f5f51c3e07bfb15817906f08998df1015e2
3 changes: 0 additions & 3 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,6 @@ def test_patma_132(self):
self.assertEqual(x, [0, 1, 2])
self.assertEqual(y, 0)

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_patma_133(self):
x = collections.defaultdict(int, {0: 1})
match x:
Expand All @@ -1350,7 +1349,6 @@ def test_patma_133(self):
self.assertEqual(x, {0: 1})
self.assertEqual(y, 2)

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_patma_134(self):
x = collections.defaultdict(int, {0: 1})
match x:
Expand All @@ -1364,7 +1362,6 @@ def test_patma_134(self):
self.assertEqual(y, 2)
self.assertEqual(z, {0: 1})

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_patma_135(self):
x = collections.defaultdict(int, {0: 1})
match x:
Expand Down
47 changes: 40 additions & 7 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,14 +1332,47 @@ impl ExecutingFrame<'_> {
let mut values = Vec::new();
let mut all_match = true;

for key in keys {
match subject.get_item(key.as_object(), vm) {
Ok(value) => values.push(value),
Err(e) if e.fast_isinstance(vm.ctx.exceptions.key_error) => {
all_match = false;
break;
// 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).
// See CPython's _PyEval_MatchKeys

if let Some(get_method) = vm
.get_method(subject.to_owned(), vm.ctx.intern_str("get"))
.transpose()?
{
// dummy = object()
// CPython: dummy = _PyObject_CallNoArgs((PyObject *)&PyBaseObject_Type);
let dummy = vm
.ctx
.new_base_object(vm.ctx.types.object_type.to_owned(), None);

for key in keys {
// value = map.get(key, dummy)
match get_method.call((key.as_object(), dummy.clone()), vm) {
Ok(value) => {
// if value == dummy: key not in map!
if value.is(&dummy) {
all_match = false;
break;
}
values.push(value);
}
Err(e) => return Err(e),
}
}
} else {
// Fallback if .get() method is not available (shouldn't happen for mappings)
for key in keys {
match subject.get_item(key.as_object(), vm) {
Ok(value) => values.push(value),
Err(e) if e.fast_isinstance(vm.ctx.exceptions.key_error) => {
all_match = false;
break;
}
Err(e) => return Err(e),
}
Err(e) => return Err(e),
}
}

Expand Down