Skip to content

Commit 2a38b10

Browse files
committed
[GR-73107] Pattern matching: just fail on object attribute missing.
PullRequest: graalpython/4251
2 parents 6a6cb6e + 05f74d0 commit 2a38b10

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_patmat.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022, 2026, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -262,4 +262,16 @@ def test_unbound_local_variable(self):
262262
match (1, 3):
263263
case (a, 1) | (a, 2):
264264
pass
265-
assert a == 1
265+
assert a == 1
266+
267+
def test_missing_kwarg(self):
268+
def f():
269+
x = sum
270+
match x:
271+
case object(factor=x) if x is not None:
272+
pass
273+
274+
try:
275+
f()
276+
except AttributeError:
277+
self.fail("Invalid keyword argument should not raise AttributeError")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/MatchClassNode.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -139,7 +139,14 @@ Object match(VirtualFrame frame, Object subject, Object type, int nargs, @NeverD
139139
attrs = new Object[kwArgs.length];
140140
}
141141
// Finally, the keyword subpatterns:
142-
getKwArgs(frame, inliningTarget, subject, type, kwArgs, seen, seenLength, attrs, attrsLength, getAttr, eqStrNode, raise);
142+
try {
143+
getKwArgs(frame, inliningTarget, subject, type, kwArgs, seen, seenLength, attrs, attrsLength, getAttr, eqStrNode, raise);
144+
} catch (PException pe) {
145+
// missing keyword argument will throw AttributeError, but in pattern matching, that
146+
// should be ignored
147+
pe.expectAttributeError(inliningTarget, isClassProfile);
148+
return null;
149+
}
143150
return PFactory.createList(language, attrs);
144151
}
145152

0 commit comments

Comments
 (0)