|
| 1 | +use super::{PyStr, PyType, PyTypeRef}; |
| 2 | +use crate::{ |
| 3 | + class::PyClassImpl, |
| 4 | + types::{Constructor, GetDescriptor, Unconstructible}, |
| 5 | + AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, |
| 6 | +}; |
| 7 | +use rustpython_common::lock::PyRwLock; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub struct DescrObject { |
| 11 | + pub typ: PyTypeRef, |
| 12 | + pub name: String, |
| 13 | + pub qualname: PyRwLock<Option<String>>, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +pub enum MemberKind { |
| 18 | + ObjectEx = 16, |
| 19 | +} |
| 20 | + |
| 21 | +pub enum MemberGetter { |
| 22 | + Getter(fn(&VirtualMachine, PyObjectRef) -> PyResult), |
| 23 | + Offset(usize), |
| 24 | +} |
| 25 | + |
| 26 | +pub struct MemberDef { |
| 27 | + pub name: String, |
| 28 | + pub kind: MemberKind, |
| 29 | + pub getter: MemberGetter, |
| 30 | + pub doc: Option<String>, |
| 31 | +} |
| 32 | + |
| 33 | +impl MemberDef { |
| 34 | + fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { |
| 35 | + match self.getter { |
| 36 | + MemberGetter::Getter(getter) => (getter)(vm, obj), |
| 37 | + MemberGetter::Offset(offset) => get_slot_from_object(obj, offset, self, vm), |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl std::fmt::Debug for MemberDef { |
| 43 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 44 | + f.debug_struct("MemberDef") |
| 45 | + .field("name", &self.name) |
| 46 | + .field("kind", &self.kind) |
| 47 | + .field("doc", &self.doc) |
| 48 | + .finish() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[pyclass(name = "member_descriptor", module = false)] |
| 53 | +#[derive(Debug)] |
| 54 | +pub struct MemberDescrObject { |
| 55 | + pub common: DescrObject, |
| 56 | + pub member: MemberDef, |
| 57 | +} |
| 58 | + |
| 59 | +impl PyPayload for MemberDescrObject { |
| 60 | + fn class(vm: &VirtualMachine) -> &'static Py<PyType> { |
| 61 | + vm.ctx.types.member_descriptor_type |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn calculate_qualname(descr: &DescrObject, vm: &VirtualMachine) -> PyResult<Option<String>> { |
| 66 | + if let Some(qualname) = vm.get_attribute_opt(descr.typ.to_owned().into(), "__qualname__")? { |
| 67 | + let str = qualname.downcast::<PyStr>().map_err(|_| { |
| 68 | + vm.new_type_error( |
| 69 | + "<descriptor>.__objclass__.__qualname__ is not a unicode object".to_owned(), |
| 70 | + ) |
| 71 | + })?; |
| 72 | + Ok(Some(format!("{}.{}", str, descr.name))) |
| 73 | + } else { |
| 74 | + Ok(None) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[pyclass(with(GetDescriptor, Constructor), flags(BASETYPE))] |
| 79 | +impl MemberDescrObject { |
| 80 | + #[pymethod(magic)] |
| 81 | + fn repr(zelf: PyRef<Self>) -> String { |
| 82 | + format!( |
| 83 | + "<member '{}' of '{}' objects>", |
| 84 | + zelf.common.name, |
| 85 | + zelf.common.typ.name(), |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + #[pyproperty(magic)] |
| 90 | + fn doc(zelf: PyRef<Self>) -> Option<String> { |
| 91 | + zelf.member.doc.to_owned() |
| 92 | + } |
| 93 | + |
| 94 | + #[pyproperty(magic)] |
| 95 | + fn qualname(&self, vm: &VirtualMachine) -> PyResult<Option<String>> { |
| 96 | + let qualname = self.common.qualname.read(); |
| 97 | + Ok(if qualname.is_none() { |
| 98 | + drop(qualname); |
| 99 | + let calculated = calculate_qualname(&self.common, vm)?; |
| 100 | + *self.common.qualname.write() = calculated.to_owned(); |
| 101 | + calculated |
| 102 | + } else { |
| 103 | + qualname.to_owned() |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// PyMember_GetOne |
| 109 | +fn get_slot_from_object( |
| 110 | + obj: PyObjectRef, |
| 111 | + offset: usize, |
| 112 | + member: &MemberDef, |
| 113 | + vm: &VirtualMachine, |
| 114 | +) -> PyResult { |
| 115 | + let slot = match member.kind { |
| 116 | + MemberKind::ObjectEx => obj.get_slot(offset).ok_or_else(|| { |
| 117 | + vm.new_attribute_error(format!( |
| 118 | + "'{}' object has no attribute '{}'", |
| 119 | + obj.class().name(), |
| 120 | + member.name |
| 121 | + )) |
| 122 | + })?, |
| 123 | + }; |
| 124 | + Ok(slot) |
| 125 | +} |
| 126 | + |
| 127 | +impl Unconstructible for MemberDescrObject {} |
| 128 | + |
| 129 | +impl GetDescriptor for MemberDescrObject { |
| 130 | + fn descr_get( |
| 131 | + zelf: PyObjectRef, |
| 132 | + obj: Option<PyObjectRef>, |
| 133 | + _cls: Option<PyObjectRef>, |
| 134 | + vm: &VirtualMachine, |
| 135 | + ) -> PyResult { |
| 136 | + match obj { |
| 137 | + Some(x) => { |
| 138 | + let zelf = Self::_zelf(zelf, vm)?; |
| 139 | + zelf.member.get(x, vm) |
| 140 | + } |
| 141 | + None => Ok(zelf), |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +pub fn init(context: &Context) { |
| 147 | + let member_descriptor_type = &context.types.member_descriptor_type; |
| 148 | + MemberDescrObject::extend_class(context, member_descriptor_type); |
| 149 | +} |
0 commit comments