-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyBaseException.java
More file actions
277 lines (235 loc) · 8.24 KB
/
Copy pathPyBaseException.java
File metadata and controls
277 lines (235 loc) · 8.24 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Copyright (c) 2008 Jython Developers */
package org.python.core;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedSet;
import org.python.expose.ExposedType;
/**
* The base class for all standard Python Exceptions.
*
*/
@ExposedType(name = "BaseException", doc = BuiltinDocs.BaseException_doc)
public class PyBaseException extends PyObject implements Traverseproc {
public static final PyType TYPE = PyType.fromClass(PyBaseException.class);
/** Exception message. */
private PyObject message = Py.EmptyByte;
/** Exception's arguments. */
@ExposedGet(doc = BuiltinDocs.BaseException_args_doc)
public PyObject args = Py.EmptyTuple;
/** Exception's underlying dictionary, lazily created. */
public PyObject __dict__;
/** The reference to the wrapping PyException instance */
protected PyException wrapper;
@ExposedGet(doc = BuiltinDocs.BaseException___cause___doc)
public PyObject __cause__;
@ExposedGet(doc = BuiltinDocs.BaseException___suppress_context___doc)
@ExposedSet
public PyObject __suppress_context__;
@ExposedGet(doc = BuiltinDocs.BaseException___context___doc)
public PyObject __context__;
@ExposedGet(doc = BuiltinDocs.BaseException___traceback___doc)
public PyObject __traceback__;
public PyBaseException() {
super();
}
public PyBaseException(PyType subType) {
super(subType);
}
public void __init__(PyObject[] args, String[] keywords) {
BaseException___init__(args, keywords);
}
@ExposedNew
@ExposedMethod(doc = BuiltinDocs.BaseException___init___doc)
final void BaseException___init__(PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser(getType().getName(), args, keywords, "args");
ap.noKeywords();
this.args = ap.getList(0);
if (args.length == 1) {
message = args[0];
}
PyException pye = Py.getThreadState().exceptions.peek();
if (pye != null) {
__context__ = pye.value;
} else {
__context__ = Py.None;
}
__suppress_context__ = Py.False;
}
public PyObject with_traceback(PyObject tb) {
return BaseException_with_traceback(tb);
}
@ExposedMethod(doc = BuiltinDocs.BaseException_with_traceback_doc)
final PyObject BaseException_with_traceback(PyObject tb) {
__traceback__ = tb;
return this;
}
@Override
public PyObject __reduce__() {
return BaseException___reduce__();
}
@ExposedMethod(doc = BuiltinDocs.BaseException___reduce___doc)
final PyObject BaseException___reduce__() {
if (__dict__ != null) {
return new PyTuple(getType(), args, __dict__);
} else {
return new PyTuple(getType(), args);
}
}
public PyObject __setstate__(PyObject state) {
return BaseException___setstate__(state);
}
@ExposedMethod(doc = BuiltinDocs.BaseException___setstate___doc)
final PyObject BaseException___setstate__(PyObject state) {
if (state != Py.None) {
if (!(state instanceof PyStringMap) && !(state instanceof PyDictionary)) {
throw Py.TypeError("state is not a dictionary");
}
for (PyObject key : state.asIterable()) {
__setattr__((PyUnicode)key, state.__finditem__(key));
}
}
return Py.None;
}
@Override
public PyObject __findattr_ex__(String name) {
return BaseException___findattr__(name);
}
final PyObject BaseException___findattr__(String name) {
if (__dict__ != null) {
PyObject attr = __dict__.__finditem__(name);
if (attr != null) {
return attr;
}
}
return super.__findattr_ex__(name);
}
@Override
public void __setattr__(String name, PyObject value) {
BaseException___setattr__(name, value);
}
@ExposedMethod(doc = BuiltinDocs.BaseException___setattr___doc)
final void BaseException___setattr__(String name, PyObject value) {
ensureDict();
super.__setattr__(name, value);
}
@Override
public PyObject fastGetDict() {
return __dict__;
}
@Override
@ExposedGet(name = "__dict__", doc = BuiltinDocs.BaseException___dict___doc)
public PyObject getDict() {
ensureDict();
return __dict__;
}
@Override
@ExposedSet(name = "__dict__")
public void setDict(PyObject val) {
if (!(val instanceof PyStringMap) && !(val instanceof PyDictionary)) {
throw Py.TypeError("__dict__ must be a dictionary");
}
__dict__ = val;
}
@ExposedSet(name = "__traceback__")
public void setTraceback(PyObject val) {
if (val != Py.None && !Py.isInstance(val, PyTraceback.TYPE)) {
throw Py.TypeError("__traceback__ must be a traceback");
}
__traceback__ = val;
}
@ExposedSet(name = "__context__")
public void setContext(PyObject val) {
ensureException(val);
__context__ = val;
}
@ExposedSet(name = "__cause__")
public void setCause(PyObject val) {
ensureException(val);
if (PyException.isExceptionClass(val)) {
__cause__ = val.__call__(Py.EmptyObjects);
} else {
__cause__ = val;
}
if (val != null) {
__suppress_context__ = Py.True;
}
}
private void ensureDict() {
// XXX: __dict__ should really be volatile
if (__dict__ == null) {
__dict__ = new PyStringMap();
}
}
private void ensureException(PyObject val) {
if (val != Py.None && !PyException.isExceptionClass(val) && !PyException.isExceptionInstance(val)) {
throw Py.TypeError("exception cause must be None or derive from BaseException");
}
}
@Override
public PyUnicode __str__() {
return BaseException___str__();
}
@ExposedMethod(doc = BuiltinDocs.BaseException___str___doc)
final PyUnicode BaseException___str__() {
// CPython issue6108: if __str__ has been overridden in the subclass, unicode()
// should return the message returned by __str__ as used to happen before this
// method was implemented
PyType type = getType();
PyObject[] where = new PyObject[1];
PyObject str = type.lookup_where("__str__", where);
if (str != null && where[0] != TYPE) {
// Unlike str(), __str__ can return unicode (i.e. return the equivalent
// of unicode(e.__str__()) instead of unicode(str(e)))
return str.__get__(this, type).__call__().__str__();
}
switch (args.__len__()) {
case 0:
return new PyUnicode("");
case 1:
return args.__getitem__(0).__str__();
default:
return args.__str__();
}
}
@Override
public String toString() {
return BaseException_toString().asString();
}
@ExposedMethod(names = "__repr__", doc = BuiltinDocs.BaseException___repr___doc)
final PyUnicode BaseException_toString() {
PyObject reprSuffix = args.__repr__();
String name = getType().fastGetName();
int lastDot = name.lastIndexOf('.');
if (lastDot != -1) {
name = name.substring(lastDot + 1);
}
return new PyUnicode(name + reprSuffix.toString());
}
@ExposedSet(name = "args")
public void setArgs(PyObject val) {
args = PyTuple.fromIterable(val);
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retValue;
if (message != null) {
retValue = visit.visit(message, arg);
if (retValue != 0) {
return retValue;
}
}
if (args != null) {
retValue = visit.visit(args, arg);
if (retValue != 0) {
return retValue;
}
}
return __dict__ != null ? visit.visit(__dict__, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == message || ob == args || ob == __dict__);
}
}