-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBaseException.cpp
More file actions
210 lines (188 loc) · 6.13 KB
/
Copy pathBaseException.cpp
File metadata and controls
210 lines (188 loc) · 6.13 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "BaseException.hpp"
#include "MemoryError.hpp"
#include "PyBool.hpp"
#include "PyCode.hpp"
#include "PyFrame.hpp"
#include "PyNone.hpp"
#include "PyString.hpp"
#include "PyTraceback.hpp"
#include "PyTuple.hpp"
#include "PyType.hpp"
#include "SourceManager.hpp"
#include "types/api.hpp"
#include "types/builtin.hpp"
#include "vm/VM.hpp"
namespace py {
template<> BaseException *as(PyObject *obj)
{
ASSERT(types::base_exception());
if (obj->type() == types::base_exception()) { return static_cast<BaseException *>(obj); }
return nullptr;
}
template<> const BaseException *as(const PyObject *obj)
{
ASSERT(types::base_exception());
if (obj->type() == types::base_exception()) { return static_cast<const BaseException *>(obj); }
return nullptr;
}
BaseException::BaseException(PyType *type) : PyBaseObject(type->underlying_type()) {}
BaseException::BaseException(PyType *type, PyTuple *args) : PyBaseObject(type), m_args(args) {}
BaseException::BaseException(PyTuple *args)
: PyBaseObject(types::BuiltinTypes::the().base_exception()), m_args(args)
{}
BaseException::BaseException(const TypePrototype &type, PyTuple *args)
: PyBaseObject(type), m_args(args)
{}
PyResult<BaseException *> BaseException::create(PyTuple *args)
{
auto &heap = VirtualMachine::the().heap();
auto *result = heap.allocate<BaseException>(args);
if (!result) { return Err(memory_error(sizeof(BaseException))); }
return Ok(result);
}
PyResult<PyObject *> BaseException::__new__(const PyType *type, PyTuple *args, PyDict *kwargs)
{
ASSERT(type == types::base_exception());
ASSERT(!kwargs || kwargs->map().empty());
if (auto result = BaseException::create(args); result.is_ok()) {
return Ok(static_cast<PyObject *>(result.unwrap()));
} else {
return Err(result.unwrap_err());
}
}
PyResult<int32_t> BaseException::__init__(PyTuple *args, PyDict *kwargs)
{
// takes no keyword arguments
ASSERT(!kwargs || kwargs->map().empty());
m_args = args;
return Ok(0);
}
PyType *BaseException::static_type() const
{
ASSERT(types::base_exception());
return types::base_exception();
}
PyType *BaseException::class_type()
{
ASSERT(types::base_exception());
return types::base_exception();
}
std::string BaseException::what() const { return BaseException::to_string(); }
void BaseException::visit_graph(Visitor &visitor)
{
PyObject::visit_graph(visitor);
if (m_args) visitor.visit(*m_args);
if (m_dict) visitor.visit(*m_dict);
if (m_traceback) visitor.visit(*m_traceback);
if (m_context) visitor.visit(*m_context);
if (m_cause) visitor.visit(*m_cause);
}
std::string BaseException::to_string() const
{
if (m_args) {
if (m_args->size() == 1) {
auto obj_ = PyObject::from(m_args->elements()[0]);
ASSERT(obj_.is_ok());
return obj_.unwrap()->to_string();
} else {
return m_args->to_string();
}
} else {
return "";
}
}
std::string BaseException::format_traceback() const
{
std::ostringstream out;
out << "Traceback (most recent call last):\n";
auto *tb = m_traceback;
while (tb) {
const auto &filename = tb->m_tb_frame->code()->m_filename;
out << fmt::format(" File \"{}\", line {}, in {}\n",
filename,
tb->m_tb_lineno,
tb->m_tb_frame->code()->name());
const auto source = SourceManager::the().line(filename, tb->m_tb_lineno);
const auto trimmed = SourceManager::strip_leading_whitespace(source);
if (!trimmed.empty()) { out << " " << trimmed << "\n"; }
tb = tb->m_tb_next;
}
out << type()->name() << ": " << what() << "\n";
return out.str();
}
PyResult<PyObject *> BaseException::__repr__() const
{
std::string args_part;
if (m_args && m_args->size() == 1) {
auto r = repr_value(m_args->elements()[0]);
if (r.is_err()) { return r; }
args_part = fmt::format("({})", r.unwrap()->to_string());
} else if (m_args) {
auto r = m_args->repr();
if (r.is_err()) { return r; }
args_part = r.unwrap()->to_string();
} else {
args_part = "()";
}
return PyString::create(fmt::format("{}{}", type()->name(), args_part));
}
PyResult<PyObject *> BaseException::__str__() const { return PyString::create(to_string()); }
namespace {
std::once_flag base_exception_flag;
std::unique_ptr<TypePrototype> register_base_exception()
{
return std::move(klass<BaseException>("BaseException")
.property_readonly("args",
[](BaseException *self) -> PyResult<PyObject *> {
// args is always a tuple (empty when constructed without args).
if (auto args = self->args()) { return Ok(args); }
return PyTuple::create();
})
.property_readonly("__traceback__",
[](BaseException *self) -> PyResult<PyObject *> {
return Ok(self->traceback() ? self->traceback() : py_none());
})
.property(
"__cause__",
[](BaseException *self) -> PyResult<PyObject *> {
return Ok(self->cause() ? self->cause() : py_none());
},
[](BaseException *self, PyObject *value) -> PyResult<std::monostate> {
// Per the data model, setting __cause__ also suppresses the
// implicit context display (raise ... from ...).
self->set_cause(value == py_none() ? nullptr : value);
self->set_suppress_context(true);
return Ok(std::monostate{});
})
.property(
"__context__",
[](BaseException *self) -> PyResult<PyObject *> {
return Ok(self->context() ? self->context() : py_none());
},
[](BaseException *self, PyObject *value) -> PyResult<std::monostate> {
self->set_context(value == py_none() ? nullptr : value);
return Ok(std::monostate{});
})
.property(
"__suppress_context__",
[](BaseException *self) -> PyResult<PyObject *> {
return Ok(self->suppress_context() ? py_true() : py_false());
},
[](BaseException *self, PyObject *value) -> PyResult<std::monostate> {
auto truthy = value->true_();
if (truthy.is_err()) { return Err(truthy.unwrap_err()); }
self->set_suppress_context(truthy.unwrap());
return Ok(std::monostate{});
})
.type);
}
}// namespace
std::function<std::unique_ptr<TypePrototype>()> BaseException::type_factory()
{
return []() {
static std::unique_ptr<TypePrototype> type = nullptr;
std::call_once(base_exception_flag, []() { type = register_base_exception(); });
return std::move(type);
};
}
}// namespace py