Skip to content

Commit ef1ed94

Browse files
committed
Fix patma guard
1 parent d92250b commit ef1ed94

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

Lib/test/test_patma.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,6 @@ def test_patma_051(self):
586586
self.assertEqual(y, 1)
587587
self.assertEqual(z, 0)
588588

589-
@unittest.expectedFailure # TODO: RUSTPYTHON
590589
def test_patma_052(self):
591590
x = [1, 0]
592591
match x:
@@ -1904,7 +1903,6 @@ def whereis(points):
19041903
self.assertEqual(whereis([Point(0, 0), Point(0, 0), Point(0, 0)]), "Something else")
19051904
self.assertEqual(whereis([Point(0, 1), Point(0, 1), Point(0, 1)]), "Something else")
19061905

1907-
@unittest.expectedFailure # TODO: RUSTPYTHON
19081906
def test_patma_183(self):
19091907
def whereis(point):
19101908
match point:
@@ -2695,7 +2693,6 @@ def f(self, x):
26952693
setattr(c, "__attr", "spam") # setattr is needed because we're in a class scope
26962694
self.assertEqual(Outer().f(c), "spam")
26972695

2698-
@unittest.expectedFailure # TODO: RUSTPYTHON
26992696
def test_patma_250(self):
27002697
def f(x):
27012698
match x:
@@ -2707,7 +2704,6 @@ def f(x):
27072704
self.assertIs(f({"foo": 1}), True)
27082705
self.assertIs(f({"foo": -1}), False)
27092706

2710-
@unittest.expectedFailure # TODO: RUSTPYTHON
27112707
def test_patma_251(self):
27122708
def f(v, x):
27132709
match v:
@@ -2726,7 +2722,6 @@ def __init__(self, attr):
27262722
self.assertIs(f(-1, X(-1)), False)
27272723
self.assertIs(f(1, X(-1)), None)
27282724

2729-
@unittest.expectedFailure # TODO: RUSTPYTHON
27302725
def test_patma_252(self):
27312726
# Side effects must be possible in guards:
27322727
effects = []
@@ -2764,7 +2759,6 @@ def f(v):
27642759
self.assertEqual(f(1), 1)
27652760
self.assertEqual(f({"x": 1}), 1)
27662761

2767-
@unittest.expectedFailure # TODO: RUSTPYTHON
27682762
def test_patma_255(self):
27692763
x = []
27702764
match x:
@@ -3259,7 +3253,6 @@ class Class:
32593253
self.assertIs(y, None)
32603254
self.assertIs(z, None)
32613255

3262-
@unittest.expectedFailure # TODO: RUSTPYTHON
32633256
def test_accepts_positional_subpatterns_1(self):
32643257
x = range(10)
32653258
y = None

compiler/codegen/src/compile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4011,10 +4011,10 @@ impl Compiler {
40114011
self.ensure_fail_pop(pattern_context, 0)?;
40124012
// Compile the guard expression
40134013
self.compile_expression(guard)?;
4014-
// If guard is false, jump to fail_pop[0]
4014+
emit!(self, Instruction::ToBool);
40154015
emit!(
40164016
self,
4017-
Instruction::JumpIfFalseOrPop {
4017+
Instruction::PopJumpIfFalse {
40184018
target: pattern_context.fail_pop[0]
40194019
}
40204020
);

vm/src/frame.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,14 +1439,18 @@ impl ExecutingFrame<'_> {
14391439
extracted.push(subject.clone());
14401440
} else if nargs_val > 1 {
14411441
// Too many positional arguments for MATCH_SELF
1442-
self.push_value(vm.ctx.none());
1443-
return Ok(None);
1442+
return Err(vm.new_type_error(
1443+
"class pattern accepts at most 1 positional sub-pattern for MATCH_SELF types"
1444+
.to_string(),
1445+
));
14441446
}
14451447
} else {
14461448
// No __match_args__ and not a MATCH_SELF type
14471449
if nargs_val > 0 {
1448-
self.push_value(vm.ctx.none());
1449-
return Ok(None);
1450+
return Err(vm.new_type_error(
1451+
"class pattern defines no positional sub-patterns (__match_args__ missing)"
1452+
.to_string(),
1453+
));
14501454
}
14511455
}
14521456
}
@@ -1457,11 +1461,11 @@ impl ExecutingFrame<'_> {
14571461
let name_str = name.downcast_ref::<PyStr>().unwrap();
14581462
match subject.get_attr(name_str, vm) {
14591463
Ok(value) => extracted.push(value),
1460-
Err(_) => {
1461-
// Attribute doesn't exist
1464+
Err(e) if e.fast_isinstance(vm.ctx.exceptions.attribute_error) => {
14621465
self.push_value(vm.ctx.none());
14631466
return Ok(None);
14641467
}
1468+
Err(e) => return Err(e),
14651469
}
14661470
}
14671471

0 commit comments

Comments
 (0)