Skip to content

Commit 2a42430

Browse files
committed
_asyncio: require InvalidStateError to be a type
new_invalid_state_error() called whatever `asyncio.exceptions.InvalidStateError` names and unwrapped the downcast of the result, so a future asked for a result it does not have panicked once that attribute was rebound to something that is not an exception: asyncio.InvalidStateError = lambda *args: 42 _asyncio.Future(loop=object()).result() The type is looked up the way get_cancelled_error_type() looks its own up, and raised with new_exception_msg; a lookup that does not produce an exception type falls back to RuntimeError, as the other arms already did. Assisted-by: Claude
1 parent fafb317 commit 2a42430

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎crates/stdlib/src/_asyncio.rs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,16 +2745,20 @@ pub(crate) mod _asyncio {
27452745
}
27462746
}
27472747

2748+
fn get_invalid_state_error_type(vm: &VirtualMachine) -> PyResult<PyTypeRef> {
2749+
let module = vm.import("asyncio.exceptions", 0)?;
2750+
let exc_type = vm
2751+
.get_attribute_opt(module, vm.ctx.intern_str("InvalidStateError"))?
2752+
.ok_or_else(|| vm.new_attribute_error("InvalidStateError not found"))?;
2753+
exc_type
2754+
.downcast()
2755+
.map_err(|_| vm.new_type_error("InvalidStateError is not a type"))
2756+
}
2757+
27482758
fn new_invalid_state_error(vm: &VirtualMachine, msg: &str) -> PyBaseExceptionRef {
2749-
match vm.import("asyncio.exceptions", 0) {
2750-
Ok(module) => {
2751-
match vm.get_attribute_opt(module, vm.ctx.intern_str("InvalidStateError")) {
2752-
Ok(Some(exc_type)) => match exc_type.call((msg,), vm) {
2753-
Ok(exc) => exc.downcast().unwrap(),
2754-
Err(_) => vm.new_runtime_error(msg.to_string()),
2755-
},
2756-
_ => vm.new_runtime_error(msg.to_string()),
2757-
}
2759+
match get_invalid_state_error_type(vm) {
2760+
Ok(invalid_state_error) => {
2761+
vm.new_exception_msg(invalid_state_error, msg.to_string().into())
27582762
}
27592763
Err(_) => vm.new_runtime_error(msg.to_string()),
27602764
}

‎extra_tests/snippets/stdlib_asyncio.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,27 @@ def __new__(cls, *args):
4949
with assert_raises(TypeError):
5050
future.__await__().throw(BadException)
5151

52+
# InvalidStateError is looked up in asyncio.exceptions every time a pending
53+
# future is asked for its result, so what is found there need not be an
54+
# exception type at all.
55+
import asyncio # noqa: E402
56+
import asyncio.exceptions # noqa: E402
57+
58+
pending = _asyncio.Future(loop=object())
59+
with assert_raises(asyncio.exceptions.InvalidStateError):
60+
pending.result()
61+
62+
saved_invalid_state_error = asyncio.exceptions.InvalidStateError
63+
try:
64+
for replacement in (lambda *args: 42, None):
65+
asyncio.InvalidStateError = replacement
66+
asyncio.exceptions.InvalidStateError = replacement
67+
with assert_raises(RuntimeError):
68+
pending.result()
69+
with assert_raises(RuntimeError):
70+
pending.exception()
71+
finally:
72+
asyncio.InvalidStateError = saved_invalid_state_error
73+
asyncio.exceptions.InvalidStateError = saved_invalid_state_error
74+
5275
print("ok")

0 commit comments

Comments
 (0)