Skip to content

Commit 9cfc7ad

Browse files
committed
repr for getset
1 parent 8443b2c commit 9cfc7ad

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

crates/vm/src/builtins/getset.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
builtins::type_::PointerSlot,
88
class::PyClassImpl,
99
function::{IntoPyGetterFunc, IntoPySetterFunc, PyGetterFunc, PySetterFunc, PySetterValue},
10-
types::GetDescriptor,
10+
types::{GetDescriptor, Representable},
1111
};
1212

1313
#[pyclass(module = false, name = "getset_descriptor")]
@@ -96,7 +96,7 @@ impl PyGetSet {
9696
}
9797
}
9898

99-
#[pyclass(flags(DISALLOW_INSTANTIATION), with(GetDescriptor))]
99+
#[pyclass(flags(DISALLOW_INSTANTIATION), with(GetDescriptor, Representable))]
100100
impl PyGetSet {
101101
// Descriptor methods
102102

@@ -153,6 +153,23 @@ impl PyGetSet {
153153
}
154154
}
155155

156+
impl Representable for PyGetSet {
157+
#[inline]
158+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
159+
let class = unsafe { zelf.class.borrow_static() };
160+
// Special case for object type
161+
if std::ptr::eq(class, vm.ctx.types.object_type) {
162+
Ok(format!("<attribute '{}'>", zelf.name))
163+
} else {
164+
Ok(format!(
165+
"<attribute '{}' of '{}' objects>",
166+
zelf.name,
167+
class.name()
168+
))
169+
}
170+
}
171+
}
172+
156173
pub(crate) fn init(context: &Context) {
157174
PyGetSet::extend_class(context, context.types.getset_type);
158175
}

0 commit comments

Comments
 (0)