Fix string representations of @classmethod and @staticmethod

This commit is contained in:
ChJR
2022-06-13 01:19:23 +09:00
parent 4c97056157
commit ced6b682bf
2 changed files with 41 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
use super::{PyBoundMethod, PyType, PyTypeRef};
use super::{PyBoundMethod, PyStr, PyType, PyTypeRef};
use crate::{
class::PyClassImpl,
common::lock::PyMutex,
@@ -135,6 +135,26 @@ impl PyClassMethod {
self.callable.lock().get_attr("__annotations__", vm)
}
#[pymethod(magic)]
fn repr(&self, vm: &VirtualMachine) -> Option<String> {
let callable = self.callable.lock().repr(vm).unwrap();
let class = Self::class(vm);
match (
class
.qualname(vm)
.downcast_ref::<PyStr>()
.map(|n| n.as_str()),
class.module(vm).downcast_ref::<PyStr>().map(|m| m.as_str()),
) {
(None, _) => None,
(Some(qualname), Some(module)) if module != "builtins" => {
Some(format!("<{}.{}({})>", module, qualname, callable))
}
_ => Some(format!("<{}({})>", class.slot_name(), callable)),
}
}
#[pyproperty(magic)]
fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef {
match vm.get_attribute_opt(self.callable.lock().clone(), "__isabstractmethod__") {

View File

@@ -108,6 +108,26 @@ impl PyStaticMethod {
self.callable.get_attr("__annotations__", vm)
}
#[pymethod(magic)]
fn repr(&self, vm: &VirtualMachine) -> Option<String> {
let callable = self.callable.repr(vm).unwrap();
let class = Self::class(vm);
match (
class
.qualname(vm)
.downcast_ref::<PyStr>()
.map(|n| n.as_str()),
class.module(vm).downcast_ref::<PyStr>().map(|m| m.as_str()),
) {
(None, _) => None,
(Some(qualname), Some(module)) if module != "builtins" => {
Some(format!("<{}.{}({})>", module, qualname, callable))
}
_ => Some(format!("<{}({})>", class.slot_name(), callable)),
}
}
#[pyproperty(magic)]
fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef {
match vm.get_attribute_opt(self.callable.clone(), "__isabstractmethod__") {