-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyGetSetDescriptor.cpp
More file actions
127 lines (105 loc) · 3.69 KB
/
Copy pathPyGetSetDescriptor.cpp
File metadata and controls
127 lines (105 loc) · 3.69 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
#include "PyGetSetDescriptor.hpp"
#include "AttributeError.hpp"
#include "MemoryError.hpp"
#include "PyString.hpp"
#include "PyType.hpp"
#include "TypeError.hpp"
#include "interpreter/Interpreter.hpp"
#include "types/api.hpp"
#include "types/builtin.hpp"
#include "vm/VM.hpp"
namespace py {
template<> PyGetSetDescriptor *as(PyObject *obj)
{
if (obj->type() == types::getset_descriptor()) {
return static_cast<PyGetSetDescriptor *>(obj);
}
return nullptr;
}
template<> const PyGetSetDescriptor *as(const PyObject *obj)
{
if (obj->type() == types::getset_descriptor()) {
return static_cast<const PyGetSetDescriptor *>(obj);
}
return nullptr;
}
PyGetSetDescriptor::PyGetSetDescriptor(PyType *type) : PyBaseObject(type) {}
PyGetSetDescriptor::PyGetSetDescriptor(PyString *name,
PyType *underlying_type,
PropertyDefinition &getset)
: PyBaseObject(types::BuiltinTypes::the().getset_descriptor()), m_name(std::move(name)),
m_underlying_type(underlying_type), m_getset(getset)
{}
PyResult<PyGetSetDescriptor *>
PyGetSetDescriptor::create(PyString *name, PyType *underlying_type, PropertyDefinition &getset)
{
auto *obj =
VirtualMachine::the().heap().allocate<PyGetSetDescriptor>(name, underlying_type, getset);
if (!obj) { return Err(memory_error(sizeof(PyGetSetDescriptor))); }
return Ok(obj);
}
void PyGetSetDescriptor::visit_graph(Visitor &visitor)
{
PyObject::visit_graph(visitor);
visitor.visit(*m_name);
visitor.visit(*m_underlying_type);
}
std::string PyGetSetDescriptor::to_string() const
{
return fmt::format(
"<attribute '{}' of '{}' objects>", m_name->to_string(), m_underlying_type->name());
}
PyResult<PyObject *> PyGetSetDescriptor::__repr__() const { return PyString::create(to_string()); }
PyResult<PyObject *> PyGetSetDescriptor::__get__(PyObject *instance, PyObject * /*owner*/) const
{
if (!instance) { return Ok(const_cast<PyGetSetDescriptor *>(this)); }
if ((instance->type() != m_underlying_type)
&& !instance->type()->issubclass(m_underlying_type)) {
return Err(
type_error("descriptor '{}' for '{}' objects "
"doesn't apply to a '{}' object",
m_name->value(),
m_underlying_type->underlying_type().__name__,
instance->type()->underlying_type().__name__));
}
ASSERT(m_getset.has_value());
if (m_getset->get().member_getter.has_value()) {
return m_getset->get().member_getter->operator()(instance);
}
return Err(attribute_error("attribute '{}' of '{}' objects is not readable",
m_name->value(),
m_underlying_type->name()));
}
PyResult<std::monostate> PyGetSetDescriptor::__set__(PyObject *obj, PyObject *value)
{
if (obj->type() != m_underlying_type && !obj->type()->issubclass(m_underlying_type)) {
return Err(type_error("descriptor '{}' for '{}' objects doesn't apply to a '{}' object",
m_name->value(),
m_underlying_type->underlying_type().__name__,
obj->type()->underlying_type().__name__));
}
ASSERT(m_getset.has_value());
if (m_getset->get().member_setter.has_value()) {
return m_getset->get().member_setter->operator()(obj, value);
}
return Err(attribute_error("attribute '{}' of '{}' objects is not writable",
m_name->value(),
m_underlying_type->name()));
}
PyType *PyGetSetDescriptor::static_type() const { return types::getset_descriptor(); }
namespace {
std::once_flag getset_descriptor_flag;
std::unique_ptr<TypePrototype> register_getset_descriptor()
{
return std::move(klass<PyGetSetDescriptor>("getset_descriptor").type);
}
}// namespace
std::function<std::unique_ptr<TypePrototype>()> PyGetSetDescriptor::type_factory()
{
return [] {
static std::unique_ptr<TypePrototype> type = nullptr;
std::call_once(getset_descriptor_flag, []() { type = register_getset_descriptor(); });
return std::move(type);
};
}
}// namespace py