mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
repr for getset (#6514)
This commit is contained in:
1 parent
7eb0fe4984
commit
aaecdd1747
1 file changed
+19
-2
@@ -7,7 +7,7 @@ use crate::{
|
||||
builtins::type_::PointerSlot,
|
||||
class::PyClassImpl,
|
||||
function::{IntoPyGetterFunc, IntoPySetterFunc, PyGetterFunc, PySetterFunc, PySetterValue},
|
||||
types::GetDescriptor,
|
||||
types::{GetDescriptor, Representable},
|
||||
};
|
||||
|
||||
#[pyclass(module = false, name = "getset_descriptor")]
|
||||
@@ -96,7 +96,7 @@ impl PyGetSet {
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(flags(DISALLOW_INSTANTIATION), with(GetDescriptor))]
|
||||
#[pyclass(flags(DISALLOW_INSTANTIATION), with(GetDescriptor, Representable))]
|
||||
impl PyGetSet {
|
||||
// Descriptor methods
|
||||
|
||||
@@ -153,6 +153,23 @@ impl PyGetSet {
|
||||
}
|
||||
}
|
||||
|
||||
impl Representable for PyGetSet {
|
||||
#[inline]
|
||||
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let class = unsafe { zelf.class.borrow_static() };
|
||||
// Special case for object type
|
||||
if std::ptr::eq(class, vm.ctx.types.object_type) {
|
||||
Ok(format!("<attribute '{}'>", zelf.name))
|
||||
} else {
|
||||
Ok(format!(
|
||||
"<attribute '{}' of '{}' objects>",
|
||||
zelf.name,
|
||||
class.name()
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn init(context: &Context) {
|
||||
PyGetSet::extend_class(context, context.types.getset_type);
|
||||
}
|
||||
Reference in new issue
Block a user