Skip to content

Commit 97a4d2b

Browse files
authored
Merge pull request RustPython#3869 from tgsong827/stopiteraion
Fix define_exception! of PyStopIteration to set value of value attribute
2 parents 46073a2 + f438d67 commit 97a4d2b

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Lib/test/test_yield_from.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@ def g2():
338338
"Finishing g1",
339339
])
340340

341-
# TODO: RUSTPYTHON
342-
@unittest.expectedFailure
343341
def test_value_attribute_of_StopIteration_exception(self):
344342
"""
345343
Test 'value' attribute of StopIteration exception

vm/src/exceptions.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl ExceptionZoo {
712712
extend_exception!(PyException, ctx, excs.exception_type);
713713

714714
extend_exception!(PyStopIteration, ctx, excs.stop_iteration, {
715-
"value" => ctx.new_readonly_getset("value", excs.stop_iteration, make_arg_getter(0)),
715+
"value" => ctx.none(),
716716
});
717717
extend_exception!(PyStopAsyncIteration, ctx, excs.stop_async_iteration);
718718

@@ -1098,8 +1098,15 @@ pub(super) mod types {
10981098
PyStopIteration,
10991099
PyException,
11001100
stop_iteration,
1101-
"Signal the end from iterator.__next__()."
1101+
"Signal the end from iterator.__next__().",
1102+
base_exception_new,
1103+
stop_iteration_init
1104+
}
1105+
fn stop_iteration_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
1106+
zelf.set_attr("value", vm.unwrap_or_none(args.args.get(0).cloned()), vm)?;
1107+
Ok(())
11021108
}
1109+
11031110
define_exception! {
11041111
PyStopAsyncIteration,
11051112
PyException,

vm/src/vm/vm_new.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,21 @@ impl VirtualMachine {
265265
}
266266

267267
pub fn new_stop_iteration(&self, value: Option<PyObjectRef>) -> PyBaseExceptionRef {
268+
let dict = self.ctx.new_dict();
268269
let args = if let Some(value) = value {
270+
// manually set `value` attribute like StopIteration.__init__
271+
dict.set_item("value", value.clone(), self)
272+
.expect("dict.__setitem__ never fails");
269273
vec![value]
270274
} else {
271275
Vec::new()
272276
};
273-
self.new_exception(self.ctx.exceptions.stop_iteration.to_owned(), args)
277+
278+
PyRef::new_ref(
279+
PyBaseException::new(args, self),
280+
self.ctx.exceptions.stop_iteration.to_owned(),
281+
Some(dict),
282+
)
274283
}
275284

276285
fn new_downcast_error(

0 commit comments

Comments
 (0)