repr for getset (#6514)

This commit is contained in:
Jeong, YunWon authored and GitHub committed 2025-12-25 16:55:19 +09:00
1 parent 7eb0fe4984
commit aaecdd1747
1 file changed
+19 -2
+19 -2
View File
@@ -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);
}