mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Directly instantiate exceptions in the vm.new_*_error functions
This commit is contained in:
24 files changed
+104
-75
No files matched your search
+2
-3
@@ -118,9 +118,8 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
|
||||
ReadlineResult::Interrupt => {
|
||||
continuing = false;
|
||||
full_input.clear();
|
||||
let keyboard_interrupt = vm
|
||||
.new_empty_exception(vm.ctx.exceptions.keyboard_interrupt.clone())
|
||||
.unwrap();
|
||||
let keyboard_interrupt =
|
||||
vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.clone());
|
||||
Err(keyboard_interrupt)
|
||||
}
|
||||
ReadlineResult::EOF => {
|
||||
|
||||
+1
-1
@@ -629,7 +629,7 @@ impl Printer for std::io::StdoutLock<'_> {
|
||||
|
||||
pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
|
||||
let code = exit_code_arg.unwrap_or_else(|| vm.new_int(0));
|
||||
Err(vm.new_exception_obj(vm.ctx.exceptions.system_exit.clone(), vec![code])?)
|
||||
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
|
||||
}
|
||||
|
||||
pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
|
||||
|
||||
+19
-10
@@ -44,20 +44,19 @@ impl PyValue for PyBaseException {
|
||||
|
||||
#[pyimpl]
|
||||
impl PyBaseException {
|
||||
#[pyslot(new)]
|
||||
fn tp_new(
|
||||
cls: PyClassRef,
|
||||
args: PyFuncArgs,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyBaseExceptionRef> {
|
||||
pub(crate) fn new(args: Vec<PyObjectRef>, vm: &VirtualMachine) -> PyBaseException {
|
||||
PyBaseException {
|
||||
traceback: RefCell::new(None),
|
||||
cause: RefCell::new(None),
|
||||
context: RefCell::new(None),
|
||||
suppress_context: Cell::new(false),
|
||||
args: RefCell::new(PyTuple::from(args.args).into_ref(vm)),
|
||||
args: RefCell::new(PyTuple::from(args).into_ref(vm)),
|
||||
}
|
||||
.into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pyslot(new)]
|
||||
fn tp_new(cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
PyBaseException::new(args.args, vm).into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__init__")]
|
||||
@@ -325,10 +324,20 @@ impl TryFromObject for ExceptionCtor {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn invoke(
|
||||
cls: PyClassRef,
|
||||
args: Vec<PyObjectRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyBaseExceptionRef> {
|
||||
// TODO: fast-path built-in exceptions by directly instantiating them? Is that really worth it?
|
||||
let res = vm.invoke(cls.as_object(), args)?;
|
||||
PyBaseExceptionRef::try_from_object(vm, res)
|
||||
}
|
||||
|
||||
impl ExceptionCtor {
|
||||
pub fn instantiate(self, vm: &VirtualMachine) -> PyResult<PyBaseExceptionRef> {
|
||||
match self {
|
||||
Self::Class(cls) => vm.new_empty_exception(cls),
|
||||
Self::Class(cls) => invoke(cls, vec![], vm),
|
||||
Self::Instance(exc) => Ok(exc),
|
||||
}
|
||||
}
|
||||
@@ -357,7 +366,7 @@ impl ExceptionCtor {
|
||||
exc @ PyBaseException => exc.args().elements.clone(),
|
||||
obj => vec![obj],
|
||||
});
|
||||
vm.new_exception_obj(cls, args)
|
||||
invoke(cls, args, vm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -127,7 +127,7 @@ impl ExecutionResult {
|
||||
} else {
|
||||
vec![value]
|
||||
};
|
||||
Err(vm.new_exception_obj(stop_iteration, args).unwrap())
|
||||
Err(vm.new_exception(stop_iteration, args))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -978,7 +978,7 @@ impl Frame {
|
||||
0 => match vm.current_exception() {
|
||||
Some(exc) => exc,
|
||||
None => {
|
||||
return Err(vm.new_exception(
|
||||
return Err(vm.new_exception_msg(
|
||||
vm.ctx.exceptions.runtime_error.clone(),
|
||||
"No active exception to reraise".to_string(),
|
||||
))
|
||||
|
||||
@@ -72,7 +72,6 @@ mod version;
|
||||
mod vm;
|
||||
|
||||
// pub use self::pyobject::Executor;
|
||||
pub use self::exceptions::{print_exception, write_exception};
|
||||
pub use self::vm::{InitParameter, PySettings, VirtualMachine};
|
||||
pub use rustpython_bytecode::*;
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ impl PyCoroutine {
|
||||
vm.frames.borrow_mut().pop();
|
||||
self.closed.set(true);
|
||||
match result {
|
||||
Ok(ExecutionResult::Yield(_)) => Err(vm.new_exception(
|
||||
Ok(ExecutionResult::Yield(_)) => Err(vm.new_exception_msg(
|
||||
vm.ctx.exceptions.runtime_error.clone(),
|
||||
"generator ignored GeneratorExit".to_string(),
|
||||
)),
|
||||
|
||||
@@ -527,7 +527,7 @@ macro_rules! dict_iterator {
|
||||
let mut position = self.position.get();
|
||||
let dict = self.dict.entries.borrow();
|
||||
if dict.has_changed_size(&self.size) {
|
||||
return Err(vm.new_exception(
|
||||
return Err(vm.new_exception_msg(
|
||||
vm.ctx.exceptions.runtime_error.clone(),
|
||||
"dictionary changed size during iteration".to_string(),
|
||||
));
|
||||
|
||||
@@ -102,7 +102,7 @@ impl PyGenerator {
|
||||
vm.frames.borrow_mut().pop();
|
||||
self.closed.set(true);
|
||||
match result {
|
||||
Ok(ExecutionResult::Yield(_)) => Err(vm.new_exception(
|
||||
Ok(ExecutionResult::Yield(_)) => Err(vm.new_exception_msg(
|
||||
vm.ctx.exceptions.runtime_error.clone(),
|
||||
"generator ignored GeneratorExit".to_string(),
|
||||
)),
|
||||
|
||||
@@ -70,7 +70,7 @@ pub fn get_all<T: TryFromObject>(vm: &VirtualMachine, iter_obj: &PyObjectRef) ->
|
||||
|
||||
pub fn new_stop_iteration(vm: &VirtualMachine) -> PyBaseExceptionRef {
|
||||
let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone();
|
||||
vm.new_empty_exception(stop_iteration_type).unwrap()
|
||||
vm.new_exception_empty(stop_iteration_type)
|
||||
}
|
||||
|
||||
pub fn stop_iter_value(vm: &VirtualMachine, exc: &PyBaseExceptionRef) -> PyResult {
|
||||
|
||||
@@ -41,7 +41,7 @@ impl PyWeakProxy {
|
||||
fn getattr(&self, attr_name: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
match self.weak.upgrade() {
|
||||
Some(obj) => vm.get_attribute(obj, attr_name),
|
||||
None => Err(vm.new_exception(
|
||||
None => Err(vm.new_exception_msg(
|
||||
vm.ctx.exceptions.reference_error.clone(),
|
||||
"weakly-referenced object no longer exists".to_string(),
|
||||
)),
|
||||
@@ -52,7 +52,7 @@ impl PyWeakProxy {
|
||||
fn setattr(&self, attr_name: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
match self.weak.upgrade() {
|
||||
Some(obj) => vm.set_attr(&obj, attr_name, value),
|
||||
None => Err(vm.new_exception(
|
||||
None => Err(vm.new_exception_msg(
|
||||
vm.ctx.exceptions.reference_error.clone(),
|
||||
"weakly-referenced object no longer exists".to_string(),
|
||||
)),
|
||||
|
||||
+6
-2
@@ -619,7 +619,7 @@ impl<T: PyValue> PyRef<T> {
|
||||
if obj.payload_is::<T>() {
|
||||
Ok(Self::new_ref_unchecked(obj))
|
||||
} else {
|
||||
Err(vm.new_exception(
|
||||
Err(vm.new_exception_msg(
|
||||
vm.ctx.exceptions.runtime_error.clone(),
|
||||
format!("Unexpected payload for type {:?}", obj.class().name),
|
||||
))
|
||||
@@ -1085,7 +1085,7 @@ pub trait PyValue: fmt::Debug + Sized + 'static {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef;
|
||||
|
||||
fn into_ref(self, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
PyRef::new_ref_unchecked(PyObject::new(self, Self::class(vm), None))
|
||||
self.into_ref_with_type_unchecked(Self::class(vm))
|
||||
}
|
||||
|
||||
fn into_ref_with_type(self, vm: &VirtualMachine, cls: PyClassRef) -> PyResult<PyRef<Self>> {
|
||||
@@ -1103,6 +1103,10 @@ pub trait PyValue: fmt::Debug + Sized + 'static {
|
||||
Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype)))
|
||||
}
|
||||
}
|
||||
|
||||
fn into_ref_with_type_unchecked(self, cls: PyClassRef) -> PyRef<Self> {
|
||||
PyRef::new_ref_unchecked(PyObject::new(self, cls, None))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait PyObjectPayload: Any + fmt::Debug + 'static {
|
||||
|
||||
@@ -26,7 +26,7 @@ fn functools_reduce(
|
||||
objiter::call_next(vm, &iterator).map_err(|err| {
|
||||
if objtype::isinstance(&err, &vm.ctx.exceptions.stop_iteration) {
|
||||
let exc_type = vm.ctx.exceptions.type_error.clone();
|
||||
vm.new_exception(
|
||||
vm.new_exception_msg(
|
||||
exc_type,
|
||||
"reduce() of empty sequence with no initial value".to_string(),
|
||||
)
|
||||
|
||||
+4
-4
@@ -386,7 +386,7 @@ fn io_base_checkclosed(
|
||||
let msg = msg
|
||||
.flat_option()
|
||||
.unwrap_or_else(|| vm.new_str("I/O operation on closed file.".to_string()));
|
||||
Err(vm.new_exception_obj(vm.ctx.exceptions.value_error.clone(), vec![msg])?)
|
||||
Err(vm.new_exception(vm.ctx.exceptions.value_error.clone(), vec![msg]))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -401,7 +401,7 @@ fn io_base_checkreadable(
|
||||
let msg = msg
|
||||
.flat_option()
|
||||
.unwrap_or_else(|| vm.new_str("File or stream is not readable.".to_string()));
|
||||
Err(vm.new_exception_obj(vm.ctx.exceptions.value_error.clone(), vec![msg])?)
|
||||
Err(vm.new_exception(vm.ctx.exceptions.value_error.clone(), vec![msg]))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -416,7 +416,7 @@ fn io_base_checkwritable(
|
||||
let msg = msg
|
||||
.flat_option()
|
||||
.unwrap_or_else(|| vm.new_str("File or stream is not writable.".to_string()));
|
||||
Err(vm.new_exception_obj(vm.ctx.exceptions.value_error.clone(), vec![msg])?)
|
||||
Err(vm.new_exception(vm.ctx.exceptions.value_error.clone(), vec![msg]))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -431,7 +431,7 @@ fn io_base_checkseekable(
|
||||
let msg = msg
|
||||
.flat_option()
|
||||
.unwrap_or_else(|| vm.new_str("File or stream is not seekable.".to_string()));
|
||||
Err(vm.new_exception_obj(vm.ctx.exceptions.value_error.clone(), vec![msg])?)
|
||||
Err(vm.new_exception(vm.ctx.exceptions.value_error.clone(), vec![msg]))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ pub fn json_loads(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
.unwrap();
|
||||
let json_decode_error = vm.get_attribute(module, "JSONDecodeError").unwrap();
|
||||
let json_decode_error = json_decode_error.downcast().unwrap();
|
||||
let exc = vm.new_exception(json_decode_error, format!("{}", err));
|
||||
let exc = vm.new_exception_msg(json_decode_error, format!("{}", err));
|
||||
vm.set_attr(exc.as_object(), "lineno", vm.ctx.new_int(err.line()))
|
||||
.unwrap();
|
||||
vm.set_attr(exc.as_object(), "colno", vm.ctx.new_int(err.column()))
|
||||
|
||||
+5
-5
@@ -171,7 +171,7 @@ pub fn convert_io_error(vm: &VirtualMachine, err: io::Error) -> PyBaseExceptionR
|
||||
_ => vm.ctx.exceptions.os_error.clone(),
|
||||
},
|
||||
};
|
||||
let os_error = vm.new_exception(exc_type, err.to_string());
|
||||
let os_error = vm.new_exception_msg(exc_type, err.to_string());
|
||||
let errno = match err.raw_os_error() {
|
||||
Some(errno) => vm.new_int(errno),
|
||||
None => vm.get_none(),
|
||||
@@ -185,19 +185,19 @@ pub fn convert_nix_error(vm: &VirtualMachine, err: nix::Error) -> PyBaseExceptio
|
||||
let nix_error = match err {
|
||||
nix::Error::InvalidPath => {
|
||||
let exc_type = vm.ctx.exceptions.file_not_found_error.clone();
|
||||
vm.new_exception(exc_type, err.to_string())
|
||||
vm.new_exception_msg(exc_type, err.to_string())
|
||||
}
|
||||
nix::Error::InvalidUtf8 => {
|
||||
let exc_type = vm.ctx.exceptions.unicode_error.clone();
|
||||
vm.new_exception(exc_type, err.to_string())
|
||||
vm.new_exception_msg(exc_type, err.to_string())
|
||||
}
|
||||
nix::Error::UnsupportedOperation => {
|
||||
let exc_type = vm.ctx.exceptions.runtime_error.clone();
|
||||
vm.new_exception(exc_type, err.to_string())
|
||||
vm.new_exception_msg(exc_type, err.to_string())
|
||||
}
|
||||
nix::Error::Sys(errno) => {
|
||||
let exc_type = convert_nix_errno(vm, errno);
|
||||
vm.new_exception(exc_type, err.to_string())
|
||||
vm.new_exception_msg(exc_type, err.to_string())
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
|
||||
fn random_normalvariate(mu: f64, sigma: f64, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let normal = Normal::new(mu, sigma).map_err(|rand_err| {
|
||||
vm.new_exception(
|
||||
vm.new_exception_msg(
|
||||
vm.ctx.exceptions.arithmetic_error.clone(),
|
||||
format!("invalid normal distribution: {:?}", rand_err),
|
||||
)
|
||||
|
||||
@@ -108,7 +108,7 @@ pub fn check_signals(vm: &VirtualMachine) -> PyResult<()> {
|
||||
}
|
||||
|
||||
fn default_int_handler(_signum: PyObjectRef, _arg: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
Err(vm.new_empty_exception(vm.ctx.exceptions.keyboard_interrupt.clone())?)
|
||||
Err(vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.clone()))
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
|
||||
@@ -485,7 +485,7 @@ fn socket_getaddrinfo(opts: GAIOptions, vm: &VirtualMachine) -> PyResult {
|
||||
|
||||
let addrs = dns_lookup::getaddrinfo(host, port, Some(hints)).map_err(|err| {
|
||||
let error_type = vm.class("_socket", "gaierror");
|
||||
vm.new_exception(error_type, io::Error::from(err).to_string())
|
||||
vm.new_exception_msg(error_type, io::Error::from(err).to_string())
|
||||
})?;
|
||||
|
||||
let list = addrs
|
||||
@@ -537,7 +537,7 @@ where
|
||||
Ok(mut sock_addrs) => {
|
||||
if sock_addrs.len() == 0 {
|
||||
let error_type = vm.class("_socket", "gaierror");
|
||||
Err(vm.new_exception(
|
||||
Err(vm.new_exception_msg(
|
||||
error_type,
|
||||
"nodename nor servname provided, or not known".to_string(),
|
||||
))
|
||||
@@ -547,7 +547,7 @@ where
|
||||
}
|
||||
Err(e) => {
|
||||
let error_type = vm.class("_socket", "gaierror");
|
||||
Err(vm.new_exception(error_type, e.to_string()))
|
||||
Err(vm.new_exception_msg(error_type, e.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -593,7 +593,7 @@ fn invalid_sock() -> Socket {
|
||||
fn convert_sock_error(vm: &VirtualMachine, err: io::Error) -> PyBaseExceptionRef {
|
||||
if err.kind() == io::ErrorKind::TimedOut {
|
||||
let socket_timeout = vm.class("_socket", "timeout");
|
||||
vm.new_exception(socket_timeout, "Timed out".to_string())
|
||||
vm.new_exception_msg(socket_timeout, "Timed out".to_string())
|
||||
} else {
|
||||
convert_io_error(vm, err)
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ impl PopenRef {
|
||||
.map_err(|s| vm.new_os_error(format!("Could not start program: {}", s)))?;
|
||||
if timeout.is_none() {
|
||||
let timeout_expired = vm.try_class("_subprocess", "TimeoutExpired")?;
|
||||
Err(vm.new_exception(timeout_expired, "Timeout".to_string()))
|
||||
Err(vm.new_exception_msg(timeout_expired, "Timeout".to_string()))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -121,5 +121,5 @@ fn zlib_error(message: &str, vm: &VirtualMachine) -> PyBaseExceptionRef {
|
||||
let zlib_error = vm.get_attribute(module, "error").unwrap();
|
||||
let zlib_error = zlib_error.downcast().unwrap();
|
||||
|
||||
vm.new_exception(zlib_error, message.to_string())
|
||||
vm.new_exception_msg(zlib_error, message.to_string())
|
||||
}
|
||||
+1
-1
@@ -198,7 +198,7 @@ fn sys_git_info(vm: &VirtualMachine) -> PyObjectRef {
|
||||
|
||||
fn sys_exit(code: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
|
||||
let code = code.unwrap_or_else(|| vm.new_int(0));
|
||||
Err(vm.new_exception_obj(vm.ctx.exceptions.system_exit.clone(), vec![code])?)
|
||||
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectRef) {
|
||||
|
||||
+45
-27
@@ -19,7 +19,7 @@ use rustpython_compiler::{compile, error::CompileError};
|
||||
|
||||
use crate::builtins::{self, to_ascii};
|
||||
use crate::bytecode;
|
||||
use crate::exceptions::PyBaseExceptionRef;
|
||||
use crate::exceptions::{PyBaseException, PyBaseExceptionRef};
|
||||
use crate::frame::{ExecutionResult, Frame, FrameRef};
|
||||
use crate::frozen;
|
||||
use crate::function::PyFuncArgs;
|
||||
@@ -329,45 +329,63 @@ impl VirtualMachine {
|
||||
PyObject::new(PyModule {}, self.ctx.types.module_type.clone(), Some(dict))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "flame-it", flame("VirtualMachine"))]
|
||||
pub fn new_exception_obj(
|
||||
/// Instantiate an exception with arguments.
|
||||
/// This function should only be used with builtin exception types; if a user-defined exception
|
||||
/// type is passed in, it may not be fully initialized; try using [`exceptions::invoke`](invoke)
|
||||
/// or [`exceptions::ExceptionCtor`](ctor) instead.
|
||||
///
|
||||
/// [invoke]: rustpython_vm::exceptions::invoke
|
||||
/// [ctor]: rustpython_vm::exceptions::ExceptionCtor
|
||||
pub fn new_exception(
|
||||
&self,
|
||||
exc_type: PyClassRef,
|
||||
args: Vec<PyObjectRef>,
|
||||
) -> PyResult<PyBaseExceptionRef> {
|
||||
) -> PyBaseExceptionRef {
|
||||
// TODO: add repr of args into logging?
|
||||
vm_trace!("New exception created: {}", exc_type.name);
|
||||
PyBaseExceptionRef::try_from_object(self, self.invoke(&exc_type.into_object(), args)?)
|
||||
PyBaseException::new(args, self).into_ref_with_type_unchecked(exc_type)
|
||||
}
|
||||
|
||||
pub fn new_empty_exception(&self, exc_type: PyClassRef) -> PyResult<PyBaseExceptionRef> {
|
||||
self.new_exception_obj(exc_type, vec![])
|
||||
/// Instantiate an exception with no arguments.
|
||||
/// This function should only be used with builtin exception types; if a user-defined exception
|
||||
/// type is passed in, it may not be fully initialized; try using [`exceptions::invoke`](invoke)
|
||||
/// or [`exceptions::ExceptionCtor`](ctor) instead.
|
||||
///
|
||||
/// [invoke]: rustpython_vm::exceptions::invoke
|
||||
/// [ctor]: rustpython_vm::exceptions::ExceptionCtor
|
||||
pub fn new_exception_empty(&self, exc_type: PyClassRef) -> PyBaseExceptionRef {
|
||||
self.new_exception(exc_type, vec![])
|
||||
}
|
||||
|
||||
/// Create Python instance of `exc_type` with message as first element of `args` tuple
|
||||
pub fn new_exception(&self, exc_type: PyClassRef, msg: String) -> PyBaseExceptionRef {
|
||||
let pystr_msg = self.new_str(msg);
|
||||
self.new_exception_obj(exc_type, vec![pystr_msg]).unwrap()
|
||||
/// Instantiate an exception with `msg` as the only argument.
|
||||
/// This function should only be used with builtin exception types; if a user-defined exception
|
||||
/// type is passed in, it may not be fully initialized; try using [`exceptions::invoke`](invoke)
|
||||
/// or [`exceptions::ExceptionCtor`](ctor) instead.
|
||||
///
|
||||
/// [invoke]: rustpython_vm::exceptions::invoke
|
||||
/// [ctor]: rustpython_vm::exceptions::ExceptionCtor
|
||||
pub fn new_exception_msg(&self, exc_type: PyClassRef, msg: String) -> PyBaseExceptionRef {
|
||||
self.new_exception(exc_type, vec![self.new_str(msg)])
|
||||
}
|
||||
|
||||
pub fn new_lookup_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let lookup_error = self.ctx.exceptions.lookup_error.clone();
|
||||
self.new_exception(lookup_error, msg)
|
||||
self.new_exception_msg(lookup_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_attribute_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let attribute_error = self.ctx.exceptions.attribute_error.clone();
|
||||
self.new_exception(attribute_error, msg)
|
||||
self.new_exception_msg(attribute_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_type_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let type_error = self.ctx.exceptions.type_error.clone();
|
||||
self.new_exception(type_error, msg)
|
||||
self.new_exception_msg(type_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_name_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let name_error = self.ctx.exceptions.name_error.clone();
|
||||
self.new_exception(name_error, msg)
|
||||
self.new_exception_msg(name_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_unsupported_operand_error(
|
||||
@@ -386,54 +404,54 @@ impl VirtualMachine {
|
||||
|
||||
pub fn new_os_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let os_error = self.ctx.exceptions.os_error.clone();
|
||||
self.new_exception(os_error, msg)
|
||||
self.new_exception_msg(os_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_unicode_decode_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let unicode_decode_error = self.ctx.exceptions.unicode_decode_error.clone();
|
||||
self.new_exception(unicode_decode_error, msg)
|
||||
self.new_exception_msg(unicode_decode_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_unicode_encode_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let unicode_encode_error = self.ctx.exceptions.unicode_encode_error.clone();
|
||||
self.new_exception(unicode_encode_error, msg)
|
||||
self.new_exception_msg(unicode_encode_error, msg)
|
||||
}
|
||||
|
||||
/// Create a new python ValueError object. Useful for raising errors from
|
||||
/// python functions implemented in rust.
|
||||
pub fn new_value_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let value_error = self.ctx.exceptions.value_error.clone();
|
||||
self.new_exception(value_error, msg)
|
||||
self.new_exception_msg(value_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_key_error(&self, obj: PyObjectRef) -> PyBaseExceptionRef {
|
||||
let key_error = self.ctx.exceptions.key_error.clone();
|
||||
self.new_exception_obj(key_error, vec![obj]).unwrap()
|
||||
self.new_exception(key_error, vec![obj])
|
||||
}
|
||||
|
||||
pub fn new_index_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let index_error = self.ctx.exceptions.index_error.clone();
|
||||
self.new_exception(index_error, msg)
|
||||
self.new_exception_msg(index_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_not_implemented_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let not_implemented_error = self.ctx.exceptions.not_implemented_error.clone();
|
||||
self.new_exception(not_implemented_error, msg)
|
||||
self.new_exception_msg(not_implemented_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_recursion_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let recursion_error = self.ctx.exceptions.recursion_error.clone();
|
||||
self.new_exception(recursion_error, msg)
|
||||
self.new_exception_msg(recursion_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_zero_division_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let zero_division_error = self.ctx.exceptions.zero_division_error.clone();
|
||||
self.new_exception(zero_division_error, msg)
|
||||
self.new_exception_msg(zero_division_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_overflow_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let overflow_error = self.ctx.exceptions.overflow_error.clone();
|
||||
self.new_exception(overflow_error, msg)
|
||||
self.new_exception_msg(overflow_error, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
@@ -445,7 +463,7 @@ impl VirtualMachine {
|
||||
} else {
|
||||
self.ctx.exceptions.syntax_error.clone()
|
||||
};
|
||||
let syntax_error = self.new_exception(syntax_error_type, error.to_string());
|
||||
let syntax_error = self.new_exception_msg(syntax_error_type, error.to_string());
|
||||
let lineno = self.new_int(error.location.row());
|
||||
let offset = self.new_int(error.location.column());
|
||||
self.set_attr(syntax_error.as_object(), "lineno", lineno)
|
||||
@@ -469,7 +487,7 @@ impl VirtualMachine {
|
||||
|
||||
pub fn new_import_error(&self, msg: String) -> PyBaseExceptionRef {
|
||||
let import_error = self.ctx.exceptions.import_error.clone();
|
||||
self.new_exception(import_error, msg)
|
||||
self.new_exception_msg(import_error, msg)
|
||||
}
|
||||
|
||||
pub fn new_scope_with_builtins(&self) -> Scope {
|
||||
|
||||
@@ -57,9 +57,9 @@ pub fn js_err_to_py_err(vm: &VirtualMachine, js_err: &JsValue) -> PyBaseExceptio
|
||||
_ => &vm.ctx.exceptions.exception_type,
|
||||
}
|
||||
.clone();
|
||||
vm.new_exception(exc_type, err.message().into())
|
||||
vm.new_exception_msg(exc_type, err.message().into())
|
||||
}
|
||||
None => vm.new_exception(
|
||||
None => vm.new_exception_msg(
|
||||
vm.ctx.exceptions.exception_type.clone(),
|
||||
format!("{:?}", js_err),
|
||||
),
|
||||
|
||||
@@ -235,7 +235,7 @@ struct NewObjectOptions {
|
||||
}
|
||||
|
||||
fn new_js_error(vm: &VirtualMachine, err: JsValue) -> PyBaseExceptionRef {
|
||||
let exc = vm.new_exception(vm.class("_js", "JsError"), format!("{:?}", err));
|
||||
let exc = vm.new_exception_msg(vm.class("_js", "JsError"), format!("{:?}", err));
|
||||
vm.set_attr(
|
||||
exc.as_object(),
|
||||
"js_value",
|
||||
|
||||
Reference in new issue
Block a user