mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Fix string representations of @classmethod and @staticmethod
This commit is contained in:
@@ -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__") {
|
||||
|
||||
@@ -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__") {
|
||||
|
||||
Reference in New Issue
Block a user