forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.rs
More file actions
143 lines (133 loc) · 4.93 KB
/
Copy pathmethod.rs
File metadata and controls
143 lines (133 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! This module will be replaced once #3100 is done
//! Do not expose this type to outside of this crate
use super::VirtualMachine;
use crate::{
builtins::{PyBaseObject, PyStr, PyStrInterned, descriptor::PyMethodDescriptor},
function::{IntoFuncArgs, PyMethodFlags},
object::{AsObject, Py, PyObject, PyObjectRef, PyResult},
types::PyTypeFlags,
};
#[derive(Debug)]
pub(crate) enum PyMethod {
Function {
target: PyObjectRef,
func: PyObjectRef,
},
Attribute(PyObjectRef),
}
impl PyMethod {
pub(crate) fn get(obj: PyObjectRef, name: &Py<PyStr>, vm: &VirtualMachine) -> PyResult<Self> {
let cls = obj.class();
let getattro = cls.slots.getattro.load().unwrap();
if getattro as usize != PyBaseObject::getattro as *const () as usize {
return obj.get_attr(name, vm).map(Self::Attribute);
}
// any correct method name is always interned already.
let interned_name = vm.ctx.interned_str(name);
let mut is_method = false;
let cls_attr = match interned_name.and_then(|name| cls.get_attr(name)) {
Some(descr) => {
let descr_cls = descr.class();
let descr_get = if descr_cls
.slots
.flags
.has_feature(PyTypeFlags::METHOD_DESCRIPTOR)
{
// For classmethods, we need descr_get to convert instance to class
if let Some(method_descr) = descr.downcast_ref::<PyMethodDescriptor>()
&& method_descr.method.flags.contains(PyMethodFlags::CLASS)
{
descr_cls.slots.descr_get.load()
} else {
is_method = true;
None
}
} else {
let descr_get = descr_cls.slots.descr_get.load();
if let Some(descr_get) = descr_get
&& descr_cls.slots.descr_set.load().is_some()
{
let cls = cls.to_owned().into();
return descr_get(descr, Some(obj), Some(cls), vm).map(Self::Attribute);
}
descr_get
};
Some((descr, descr_get))
}
None => None,
};
if let Some(dict) = obj.dict()
&& let Some(attr) = dict.get_item_opt(name, vm)?
{
return Ok(Self::Attribute(attr));
}
if let Some((attr, descr_get)) = cls_attr {
match descr_get {
None if is_method => Ok(Self::Function {
target: obj,
func: attr,
}),
Some(descr_get) => {
let cls = cls.to_owned().into();
descr_get(attr, Some(obj), Some(cls), vm).map(Self::Attribute)
}
None => Ok(Self::Attribute(attr)),
}
} else if let Some(getter) = cls.get_attr(identifier!(vm, __getattr__)) {
getter.call((obj, name.to_owned()), vm).map(Self::Attribute)
} else {
Err(vm.new_no_attribute_error(obj.clone(), name.to_owned()))
}
}
pub(crate) fn get_special<const DIRECT: bool>(
obj: &PyObject,
name: &'static PyStrInterned,
vm: &VirtualMachine,
) -> PyResult<Option<Self>> {
let obj_cls = obj.class();
let attr = if DIRECT {
obj_cls.get_direct_attr(name)
} else {
obj_cls.get_attr(name)
};
let func = match attr {
Some(f) => f,
None => {
return Ok(None);
}
};
let meth = if func
.class()
.slots
.flags
.has_feature(PyTypeFlags::METHOD_DESCRIPTOR)
{
Self::Function {
target: obj.to_owned(),
func,
}
} else {
let obj_cls = obj_cls.to_owned().into();
let attr = vm
.call_get_descriptor_specific(&func, Some(obj.to_owned()), Some(obj_cls))
.unwrap_or(Ok(func))?;
Self::Attribute(attr)
};
Ok(Some(meth))
}
pub(crate) fn invoke(self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
let (func, args) = match self {
Self::Function { target, func } => (func, args.into_method_args(target, vm)),
Self::Attribute(func) => (func, args.into_args(vm)),
};
func.call(args, vm)
}
#[allow(dead_code)]
pub(crate) fn invoke_ref(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
let (func, args) = match self {
Self::Function { target, func } => (func, args.into_method_args(target.clone(), vm)),
Self::Attribute(func) => (func, args.into_args(vm)),
};
func.call(args, vm)
}
}