Remove Display impl for PyObject

This commit is contained in:
Jeong YunWon
2022-08-12 08:36:15 +09:00
parent b476c56deb
commit 2bb9aee5d3
6 changed files with 19 additions and 20 deletions

View File

@@ -190,7 +190,8 @@ impl ByteInnerPaddingOptions {
.ok_or_else(|| {
vm.new_type_error(format!(
"{}() argument 2 must be a byte string of length 1, not {}",
fn_name, &v
fn_name,
v.class().name()
))
})?
} else {

View File

@@ -101,7 +101,7 @@ macro_rules! py_namespace {
/// let int_value = match_class!(match obj {
/// i @ PyInt => i.as_bigint().clone(),
/// f @ PyFloat => f.to_f64().to_bigint().unwrap(),
/// obj => panic!("non-numeric object {}", obj),
/// obj => panic!("non-numeric object {:?}", obj),
/// });
///
/// assert!(int_value.is_zero());

View File

@@ -29,18 +29,6 @@ Basically reference counting, but then done by rust.
/// since exceptions are also python objects.
pub type PyResult<T = PyObjectRef> = Result<T, PyBaseExceptionRef>; // A valid value, or an exception
// TODO: remove these 2 impls
impl fmt::Display for PyObjectRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(**self).fmt(f)
}
}
impl fmt::Display for PyObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "'{}' object", self.class().name())
}
}
impl<T: fmt::Display> fmt::Display for PyRef<T>
where
T: PyObjectPayload + fmt::Display,

View File

@@ -184,7 +184,13 @@ impl PyObject {
pub fn generic_getattr(&self, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
self.generic_getattr_opt(name.clone(), None, vm)?
.ok_or_else(|| vm.new_attribute_error(format!("{} has no attribute '{}'", self, name)))
.ok_or_else(|| {
vm.new_attribute_error(format!(
"'{}' object has no attribute '{}'",
self.class().name(),
name
))
})
}
/// CPython _PyObject_GenericGetAttrWithDict
@@ -527,8 +533,12 @@ impl PyObject {
}
pub fn length(&self, vm: &VirtualMachine) -> PyResult<usize> {
self.length_opt(vm)
.ok_or_else(|| vm.new_type_error(format!("object of type '{}' has no len()", &self)))?
self.length_opt(vm).ok_or_else(|| {
vm.new_type_error(format!(
"object of type '{}' has no len()",
self.class().name()
))
})?
}
pub fn get_item<K: DictKey + ?Sized>(&self, needle: &K, vm: &VirtualMachine) -> PyResult {

View File

@@ -349,7 +349,7 @@ pub(crate) mod _thread {
.ok_or_else(|| {
vm.new_attribute_error(format!(
"{} has no attribute '{}'",
zelf.as_object(),
zelf.class().name(),
attr
))
})
@@ -367,7 +367,7 @@ pub(crate) mod _thread {
if attr.as_str() == "__dict__" {
Err(vm.new_attribute_error(format!(
"{} attribute '__dict__' is read-only",
zelf.as_object()
zelf.class().name()
)))
} else {
let dict = zelf.ldict(vm);

View File

@@ -416,7 +416,7 @@ impl VirtualMachine {
.unwrap_or_else(|_| panic!("unable to import {}", module));
let class = module
.get_attr(class, self)
.unwrap_or_else(|_| panic!("module {} has no class {}", module, class));
.unwrap_or_else(|_| panic!("module {:?} has no class {}", module, class));
class.downcast().expect("not a class")
}