Skip to content

Commit 12bbb79

Browse files
tgsong827kimnanhee
andcommitted
Fix define_exception! of PyStopIteration to set value of value attribute
Co-authored-by: NanHee Kim <kimnanhee0225@gmail.com>
1 parent 07940cd commit 12bbb79

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ impl VirtualMachine {
270270
} else {
271271
Vec::new()
272272
};
273-
self.new_exception(self.ctx.exceptions.stop_iteration.to_owned(), args)
273+
let exc = self.new_exception(self.ctx.exceptions.stop_iteration.to_owned(), args.clone());
274+
// To initialize the `value` attribute to first argument
275+
self.set_exception_attr(exc, "value", args.get(0).cloned())
274276
}
275277

276278
fn new_downcast_error(
@@ -312,4 +314,19 @@ impl VirtualMachine {
312314
obj.as_object(),
313315
)
314316
}
317+
318+
fn set_exception_attr(
319+
&self,
320+
exc: PyBaseExceptionRef,
321+
attr_name: &str,
322+
attr_value: Option<PyObjectRef>,
323+
) -> PyBaseExceptionRef {
324+
match exc
325+
.as_object()
326+
.set_attr(attr_name, self.unwrap_or_none(attr_value), self)
327+
{
328+
Ok(_) => exc,
329+
Err(e) => e,
330+
}
331+
}
315332
}

0 commit comments

Comments
 (0)