Skip to content

Commit 99a3b04

Browse files
committed
UNSUPPORTED_OPERATION
1 parent 0379914 commit 99a3b04

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

crates/vm/src/stdlib/io.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
8787
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
8888
fileio::extend_module(vm, &module).unwrap();
8989

90-
let unsupported_operation = _io::UNSUPPORTED_OPERATION
91-
.get_or_init(|| _io::make_unsupportedop(ctx))
92-
.clone();
90+
let unsupported_operation = _io::unsupported_operation().to_owned();
9391
extend_module!(vm, &module, {
9492
"UnsupportedOperation" => unsupported_operation,
9593
"BlockingIOError" => ctx.exceptions.blocking_io_error.to_owned(),
@@ -204,7 +202,8 @@ mod _io {
204202
}
205203

206204
pub fn new_unsupported_operation(vm: &VirtualMachine, msg: String) -> PyBaseExceptionRef {
207-
vm.new_exception_msg(UNSUPPORTED_OPERATION.get().unwrap().clone(), msg)
205+
vm.new_os_subtype_error(unsupported_operation().to_owned(), None, msg)
206+
.upcast()
208207
}
209208

210209
fn _unsupported<T>(vm: &VirtualMachine, zelf: &PyObject, operation: &str) -> PyResult<T> {
@@ -4243,11 +4242,7 @@ mod _io {
42434242
}
42444243
}
42454244

4246-
rustpython_common::static_cell! {
4247-
pub(super) static UNSUPPORTED_OPERATION: PyTypeRef;
4248-
}
4249-
4250-
pub(super) fn make_unsupportedop(ctx: &Context) -> PyTypeRef {
4245+
fn create_unsupported_operation(ctx: &Context) -> PyTypeRef {
42514246
use crate::types::PyTypeSlots;
42524247
PyType::new_heap(
42534248
"UnsupportedOperation",
@@ -4263,6 +4258,13 @@ mod _io {
42634258
.unwrap()
42644259
}
42654260

4261+
pub fn unsupported_operation() -> &'static Py<PyType> {
4262+
rustpython_common::static_cell! {
4263+
static CELL: PyTypeRef;
4264+
}
4265+
CELL.get_or_init(|| create_unsupported_operation(Context::genesis()))
4266+
}
4267+
42664268
#[pyfunction]
42674269
fn text_encoding(
42684270
encoding: PyObjectRef,

crates/vm/src/vm/vm_new.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,38 @@ impl VirtualMachine {
107107
)
108108
}
109109

110+
pub fn new_os_error(&self, msg: impl ToPyObject) -> PyRef<PyBaseException> {
111+
self.new_os_subtype_error(self.ctx.exceptions.os_error.to_owned(), None, msg)
112+
.upcast()
113+
}
114+
110115
pub fn new_os_subtype_error(
111116
&self,
112117
exc_type: PyTypeRef,
113118
errno: Option<i32>,
114119
msg: impl ToPyObject,
115120
) -> PyRef<PyOSError> {
116121
debug_assert_eq!(exc_type.slots.basicsize, std::mem::size_of::<PyOSError>());
122+
let msg = msg.to_pyobject(self);
123+
124+
fn new_os_subtype_error_impl(
125+
vm: &VirtualMachine,
126+
exc_type: PyTypeRef,
127+
errno: Option<i32>,
128+
msg: PyObjectRef,
129+
) -> PyRef<PyOSError> {
130+
let args = match errno {
131+
Some(e) => vec![vm.new_pyobj(e), msg],
132+
None => vec![msg],
133+
};
134+
let payload =
135+
PyOSError::py_new(&exc_type, args.into(), vm).expect("new_os_error usage error");
136+
payload
137+
.into_ref_with_type(vm, exc_type)
138+
.expect("new_os_error usage error")
139+
}
117140

118-
let errno_obj: PyObjectRef = match errno {
119-
Some(e) => self.new_pyobj(e),
120-
None => self.ctx.none(),
121-
};
122-
let args = vec![errno_obj, msg.to_pyobject(self)];
123-
let payload =
124-
PyOSError::py_new(&exc_type, args.into(), self).expect("new_os_error usage error");
125-
payload
126-
.into_ref_with_type(self, exc_type)
127-
.expect("new_os_error usage error")
141+
new_os_subtype_error_impl(self, exc_type, errno, msg)
128142
}
129143

130144
/// Instantiate an exception with no arguments.
@@ -583,7 +597,6 @@ impl VirtualMachine {
583597
define_exception_fn!(fn new_eof_error, eof_error, EOFError);
584598
define_exception_fn!(fn new_attribute_error, attribute_error, AttributeError);
585599
define_exception_fn!(fn new_type_error, type_error, TypeError);
586-
define_exception_fn!(fn new_os_error, os_error, OSError);
587600
define_exception_fn!(fn new_system_error, system_error, SystemError);
588601

589602
// TODO: remove & replace with new_unicode_decode_error_real

extra_tests/snippets/stdlib_ctypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,4 @@ def get_win_folder_via_ctypes(csidl_name: str) -> str:
398398

399399
# print(get_win_folder_via_ctypes("CSIDL_DOWNLOADS"))
400400

401-
print('done')
401+
print("done")

0 commit comments

Comments
 (0)