-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyMappingProxy.cpp
More file actions
133 lines (112 loc) · 3.86 KB
/
Copy pathPyMappingProxy.cpp
File metadata and controls
133 lines (112 loc) · 3.86 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
#include "PyMappingProxy.hpp"
#include "MemoryError.hpp"
#include "types/api.hpp"
#include "types/builtin.hpp"
namespace py {
PyMappingProxy::PyMappingProxy(PyType *type) : PyBaseObject(type) {}
PyMappingProxy::PyMappingProxy(PyObject *mapping)
: PyBaseObject(types::BuiltinTypes::the().mappingproxy()), m_mapping(mapping)
{}
PyResult<PyObject *> PyMappingProxy::create(PyObject *mapping)
{
if (as<PyList>(mapping) || as<PyTuple>(mapping) || mapping->as_mapping().is_ok()) {
auto *result = VirtualMachine::the().heap().allocate<PyMappingProxy>(mapping);
if (!result) { return Err(memory_error(sizeof(PyMappingProxy))); }
return Ok(result);
}
return Err(
type_error("mappingproxy() argument must be a mapping, not '{}'", mapping->type()->name()));
}
std::string PyMappingProxy::to_string() const { return "mappingproxy"; }
PyResult<PyObject *> PyMappingProxy::__new__(const PyType *type, PyTuple *args, PyDict *kwargs)
{
ASSERT(type == types::mappingproxy());
ASSERT(args && args->size() == 1);
ASSERT(!kwargs || kwargs->map().empty());
return PyObject::from(args->elements()[0]).and_then([](PyObject *mapping) {
return PyMappingProxy::create(mapping);
});
}
PyResult<PyObject *> PyMappingProxy::__repr__() const
{
if (auto r = m_mapping->repr(); r.is_ok()) {
return PyString::create(fmt::format("mappingproxy({})", r.unwrap()->to_string()));
} else {
return r;
}
}
PyResult<PyObject *> PyMappingProxy::__iter__() const { return m_mapping->iter(); }
PyResult<PyObject *> PyMappingProxy::__getitem__(PyObject *index)
{
return m_mapping->as_mapping().and_then(
[index](PyMappingWrapper mapping_wrapper) -> PyResult<PyObject *> {
return mapping_wrapper.getitem(index);
});
}
PyResult<PyObject *> PyMappingProxy::get(PyTuple *args, PyDict *kwargs) const
{
auto parse_result = PyArgsParser<PyObject *, PyObject *>::unpack_tuple(args,
kwargs,
"mappingproxy.get",
std::integral_constant<size_t, 1>{},
std::integral_constant<size_t, 2>{},
py_none());
if (parse_result.is_err()) return Err(parse_result.unwrap_err());
auto [key, default_] = parse_result.unwrap();
if (auto dict = as<PyDict>(m_mapping)) {
if (auto it = dict->map().find(key); it != dict->map().end()) {
return PyObject::from(it->second);
}
return Ok(default_);
}
return m_mapping->contains(key).and_then(
[this, key = key, default_ = default_](bool contains_value) -> PyResult<PyObject *> {
if (contains_value) { return m_mapping->getitem(key); }
return Ok(default_);
});
}
PyResult<PyObject *> PyMappingProxy::items() const
{
return m_mapping->get_method(PyString::create("items").unwrap()).and_then([](PyObject *items) {
return items->call(nullptr, nullptr);
});
}
PyResult<PyObject *> PyMappingProxy::keys() const
{
return m_mapping->get_method(PyString::create("keys").unwrap()).and_then([](PyObject *items) {
return items->call(nullptr, nullptr);
});
}
PyResult<PyObject *> PyMappingProxy::values() const
{
return m_mapping->get_method(PyString::create("values").unwrap()).and_then([](PyObject *items) {
return items->call(nullptr, nullptr);
});
}
PyType *PyMappingProxy::static_type() const { return types::mappingproxy(); }
void PyMappingProxy::visit_graph(Visitor &visitor)
{
PyObject::visit_graph(visitor);
if (m_mapping) { visitor.visit(*m_mapping); }
}
namespace {
std::once_flag mappingproxy_flag;
std::unique_ptr<TypePrototype> mappingproxy_reversed()
{
return std::move(klass<PyMappingProxy>("mappingproxy")
.def("get", &PyMappingProxy::get)
.def("items", &PyMappingProxy::items)
.def("keys", &PyMappingProxy::keys)
.def("value", &PyMappingProxy::values)
.type);
}
}// namespace
std::function<std::unique_ptr<TypePrototype>()> PyMappingProxy::type_factory()
{
return [] {
static std::unique_ptr<TypePrototype> type = nullptr;
std::call_once(mappingproxy_flag, []() { type = mappingproxy_reversed(); });
return std::move(type);
};
}
}// namespace py