mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Add new_exception_type for module-level exception
At previous, `vm.ctx.new_class` did role for adding exception in module-level. This commit add `new_exception_type` for doing that part of role. As cpython implementation, if no bases is given, pass `exception_type` by default Signed-off-by: snowapril <sinjihng@gmail.com>
This commit is contained in:
@@ -12,9 +12,9 @@ use crate::{
|
||||
builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef},
|
||||
bytes,
|
||||
getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet},
|
||||
object, pystr, PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat,
|
||||
PyFrozenSet, PyGenericAlias, PyInt, PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented,
|
||||
PyStr, PyTuple, PyTupleRef, PyType, PyTypeRef,
|
||||
object, pystr, PyBaseException, PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef,
|
||||
PyEllipsis, PyFloat, PyFrozenSet, PyGenericAlias, PyInt, PyIntRef, PyList, PyListRef,
|
||||
PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, PyType, PyTypeRef,
|
||||
},
|
||||
dictdatatype::Dict,
|
||||
exceptions,
|
||||
@@ -242,6 +242,30 @@ impl PyContext {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn new_exception_type(
|
||||
&self,
|
||||
module: &str,
|
||||
name: &str,
|
||||
bases: Option<Vec<PyTypeRef>>,
|
||||
) -> PyTypeRef {
|
||||
let bases = if let Some(bases) = bases {
|
||||
bases
|
||||
} else {
|
||||
vec![self.exceptions.exception_type.clone()]
|
||||
};
|
||||
let mut attrs = PyAttributes::default();
|
||||
attrs.insert("__module__".to_string(), self.new_str(module).into());
|
||||
|
||||
PyType::new_ref(
|
||||
name,
|
||||
bases,
|
||||
attrs,
|
||||
PyBaseException::make_slots(),
|
||||
self.types.type_type.clone(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn make_funcdef<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyNativeFuncDef
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user