\#[pyclass] impl Py<PyType> for

This commit is contained in:
Jeong YunWon
2023-03-16 00:21:03 +09:00
parent 1fda3ba969
commit 174ef52df7

View File

@@ -355,6 +355,31 @@ impl PyType {
attributes
}
// bound method for every type
pub(crate) fn __new__(zelf: PyRef<PyType>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
let (subtype, args): (PyRef<Self>, FuncArgs) = args.bind(vm)?;
if !subtype.fast_issubclass(&zelf) {
return Err(vm.new_type_error(format!(
"{zelf}.__new__({subtype}): {subtype} is not a subtype of {zelf}",
zelf = zelf.name(),
subtype = subtype.name(),
)));
}
call_slot_new(zelf, subtype, args, vm)
}
pub fn name(&self) -> BorrowedValue<str> {
PyRwLockReadGuard::map(self.slots.name.read(), |slot_name| {
let name = slot_name.as_ref().unwrap();
if self.slots.flags.has_feature(PyTypeFlags::HEAPTYPE) {
name.as_str()
} else {
name.rsplit('.').next().unwrap()
}
})
.into()
}
}
impl Py<PyType> {
@@ -375,30 +400,10 @@ impl Py<PyType> {
}
#[pyclass(
with(GetAttr, SetAttr, Callable, AsNumber, Representable),
with(Py, GetAttr, SetAttr, Callable, AsNumber, Representable),
flags(BASETYPE)
)]
impl PyType {
// bound method for every type
pub(crate) fn __new__(zelf: PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
let (subtype, args): (PyRef<Self>, FuncArgs) = args.bind(vm)?;
if !subtype.fast_issubclass(&zelf) {
return Err(vm.new_type_error(format!(
"{zelf}.__new__({subtype}): {subtype} is not a subtype of {zelf}",
zelf = zelf.name(),
subtype = subtype.name(),
)));
}
call_slot_new(zelf, subtype, args, vm)
}
#[pygetset(name = "__mro__")]
fn get_mro(zelf: PyRef<Self>) -> PyTuple {
let elements: Vec<PyObjectRef> =
zelf.iter_mro().map(|x| x.as_object().to_owned()).collect();
PyTuple::new_unchecked(elements.into_boxed_slice())
}
#[pygetset(magic)]
fn bases(&self, vm: &VirtualMachine) -> PyTupleRef {
vm.ctx.new_tuple(
@@ -419,48 +424,11 @@ impl PyType {
self.slots.flags.bits()
}
#[pymethod(magic)]
fn dir(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyList {
let attributes: Vec<PyObjectRef> = zelf
.get_attributes()
.into_iter()
.map(|(k, _)| k.to_object())
.collect();
PyList::from(attributes)
}
#[pymethod(magic)]
fn instancecheck(zelf: PyRef<Self>, obj: PyObjectRef) -> bool {
obj.fast_isinstance(&zelf)
}
#[pymethod(magic)]
fn subclasscheck(zelf: PyRef<Self>, subclass: PyTypeRef) -> bool {
subclass.fast_issubclass(&zelf)
}
#[pyclassmethod(magic)]
fn subclasshook(_args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}
#[pygetset]
fn __name__(&self) -> String {
self.name().to_string()
}
pub fn name(&self) -> BorrowedValue<str> {
PyRwLockReadGuard::map(self.slots.name.read(), |slot_name| {
let name = slot_name.as_ref().unwrap();
if self.slots.flags.has_feature(PyTypeFlags::HEAPTYPE) {
name.as_str()
} else {
name.rsplit('.').next().unwrap()
}
})
.into()
}
#[pygetset(magic)]
pub fn qualname(&self, vm: &VirtualMachine) -> PyObjectRef {
self.attributes
@@ -607,11 +575,6 @@ impl PyType {
)
}
#[pymethod]
fn mro(zelf: PyRef<Self>) -> Vec<PyObjectRef> {
zelf.iter_mro().map(|cls| cls.to_owned().into()).collect()
}
#[pymethod(magic)]
pub fn ror(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
or_(other, zelf, vm)
@@ -906,6 +869,46 @@ impl PyType {
}
}
#[pyclass]
impl Py<PyType> {
#[pygetset(name = "__mro__")]
fn get_mro(&self) -> PyTuple {
let elements: Vec<PyObjectRef> =
self.iter_mro().map(|x| x.as_object().to_owned()).collect();
PyTuple::new_unchecked(elements.into_boxed_slice())
}
#[pymethod(magic)]
fn dir(&self) -> PyList {
let attributes: Vec<PyObjectRef> = self
.get_attributes()
.into_iter()
.map(|(k, _)| k.to_object())
.collect();
PyList::from(attributes)
}
#[pymethod(magic)]
fn instancecheck(&self, obj: PyObjectRef) -> bool {
obj.fast_isinstance(self)
}
#[pymethod(magic)]
fn subclasscheck(&self, subclass: PyTypeRef) -> bool {
subclass.fast_issubclass(self)
}
#[pyclassmethod(magic)]
fn subclasshook(_args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}
#[pymethod]
fn mro(&self) -> Vec<PyObjectRef> {
self.iter_mro().map(|cls| cls.to_owned().into()).collect()
}
}
const SIGNATURE_END_MARKER: &str = ")\n--\n\n";
fn get_signature(doc: &str) -> Option<&str> {
doc.find(SIGNATURE_END_MARKER).map(|index| &doc[..=index])