Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86de80e

Browse files
committed
x
1 parent 7046f91 commit 86de80e

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

crates/vm/src/stdlib/io.rs

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

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

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

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

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

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

crates/vm/src/vm/vm_new.rs

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

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

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

129143
/// Instantiate an exception with no arguments.
@@ -582,7 +596,6 @@ impl VirtualMachine {
582596
define_exception_fn!(fn new_eof_error, eof_error, EOFError);
583597
define_exception_fn!(fn new_attribute_error, attribute_error, AttributeError);
584598
define_exception_fn!(fn new_type_error, type_error, TypeError);
585-
define_exception_fn!(fn new_os_error, os_error, OSError);
586599
define_exception_fn!(fn new_system_error, system_error, SystemError);
587600

588601
// TODO: remove & replace with new_unicode_decode_error_real

0 commit comments

Comments
 (0)