-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathPyBaseException.java
More file actions
290 lines (245 loc) · 8.63 KB
/
PyBaseException.java
File metadata and controls
290 lines (245 loc) · 8.63 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
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Copyright (c) 2008 Jython Developers */
package org.python.core;
import org.python.expose.ExposedDelete;
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 = "exceptions.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.EmptyString;
/** Exception's arguments. */
@ExposedGet(doc = BuiltinDocs.BaseException_args_doc)
public PyObject args = Py.EmptyTuple;
/** Exception's underlying dictionary, lazily created. */
public PyObject __dict__;
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];
}
}
@Override
public PyObject __getitem__(PyObject index) {
return BaseException___getitem__(index);
}
@ExposedMethod(doc = BuiltinDocs.BaseException___getitem___doc)
final PyObject BaseException___getitem__(PyObject index) {
Py.warnPy3k("__getitem__ not supported for exception classes in 3.x; use args "
+ "attribute");
return args.__getitem__(index);
}
@Override
public PyObject __getslice__(PyObject start, PyObject stop) {
return BaseException___getslice__(start, stop);
}
@ExposedMethod(doc = BuiltinDocs.BaseException___getslice___doc)
final PyObject BaseException___getslice__(PyObject start, PyObject stop) {
Py.warnPy3k("__getslice__ not supported for exception classes in 3.x; use args "
+ "attribute");
return args.__getslice__(start, stop);
}
@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 AbstractDict)) {
throw Py.TypeError("state is not a dictionary");
}
for (PyObject key : state.asIterable()) {
__setattr__((PyString)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;
}
private void ensureDict() {
// XXX: __dict__ should really be volatile
if (__dict__ == null) {
__dict__ = new PyStringMap();
}
}
@Override
public PyString __str__() {
return BaseException___str__();
}
@ExposedMethod(doc = BuiltinDocs.BaseException___str___doc)
final PyString BaseException___str__() {
switch (args.__len__()) {
case 0:
return Py.EmptyString;
case 1:
PyObject arg = args.__getitem__(0);
if (arg instanceof PyString) {
return (PyString)arg;
} else {
return arg.__str__();
}
default:
return args.__str__();
}
}
@Override
public PyUnicode __unicode__() {
return BaseException___unicode__();
}
@ExposedMethod(doc = BuiltinDocs.BaseException___unicode___doc)
final PyUnicode BaseException___unicode__() {
// 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__().__unicode__();
}
switch (args.__len__()) {
case 0:
return new PyUnicode("");
case 1:
return args.__getitem__(0).__unicode__();
default:
return args.__unicode__();
}
}
@Override
public String toString() {
return BaseException_toString();
}
@ExposedMethod(names = "__repr__", doc = BuiltinDocs.BaseException___repr___doc)
final String BaseException_toString() {
PyObject reprSuffix = args.__repr__();
String name = getType().fastGetName();
int lastDot = name.lastIndexOf('.');
if (lastDot != -1) {
name = name.substring(lastDot + 1);
}
return name + reprSuffix.toString();
}
@ExposedSet(name = "args")
public void setArgs(PyObject val) {
args = PyTuple.fromIterable(val);
}
@ExposedGet(name = "message", doc = BuiltinDocs.BaseException_message_doc)
public PyObject getMessage() {
PyObject message;
// if "message" is in self->dict, accessing a user-set message attribute
if (__dict__ != null && (message = __dict__.__finditem__("message")) != null) {
return message;
}
if (this.message == null) {
throw Py.AttributeError("message attribute was deleted");
}
Py.DeprecationWarning("BaseException.message has been deprecated as of Python 2.6");
return this.message;
}
@ExposedSet(name = "message")
public void setMessage(PyObject value) {
getDict().__setitem__("message", value);
}
@ExposedDelete(name = "message")
public void delMessage() {
if (__dict__ != null && (message = __dict__.__finditem__("message")) != null) {
__dict__.__delitem__("message");
}
message = null;
}
/* 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__);
}
}