001: // Copyright 2001 Finn Bock
002:
003: package org.python.core;
004:
005: /**
006: * The builtin exceptions module. The entire module should be imported from
007: * python. None of the methods defined here should be called from java.
008: */
009:
010: public class exceptions implements ClassDictInit {
011:
012: public static String __doc__ = "Python's standard exception class hierarchy.\n"
013: + "\n"
014: + "Here is a rundown of the class hierarchy. The classes found here are\n"
015: + "inserted into both the exceptions module and the `built-in' module. "
016: + "It is\n"
017: + "recommended that user defined class based exceptions be derived from the\n"
018: + "`Exception' class, although this is currently not enforced.\n"
019: + "\n"
020: + "Exception\n"
021: + " |\n"
022: + " +-- SystemExit\n"
023: + " +-- StopIteration\n"
024: + " +-- StandardError\n"
025: + " | |\n"
026: + " | +-- KeyboardInterrupt\n"
027: + " | +-- ImportError\n"
028: + " | +-- EnvironmentError\n"
029: + " | | |\n"
030: + " | | +-- IOError\n"
031: + " | | +-- OSError\n"
032: + " | | |\n"
033: + " | | +-- WindowsError\n"
034: + " | |\n"
035: + " | +-- EOFError\n"
036: + " | +-- RuntimeError\n"
037: + " | | |\n"
038: + " | | +-- NotImplementedError\n"
039: + " | |\n"
040: + " | +-- NameError\n"
041: + " | | |\n"
042: + " | | +-- UnboundLocalError\n"
043: + " | |\n"
044: + " | +-- AttributeError\n"
045: + " | +-- SyntaxError\n"
046: + " | | |\n"
047: + " | | +-- IndentationError\n"
048: + " | | |\n"
049: + " | | +-- TabError\n"
050: + " | |\n"
051: + " | +-- TypeError\n"
052: + " | +-- AssertionError\n"
053: + " | +-- LookupError\n"
054: + " | | |\n"
055: + " | | +-- IndexError\n"
056: + " | | +-- KeyError\n"
057: + " | |\n"
058: + " | +-- ArithmeticError\n"
059: + " | | |\n"
060: + " | | +-- OverflowError\n"
061: + " | | +-- ZeroDivisionError\n"
062: + " | | +-- FloatingPointError\n"
063: + " | |\n"
064: + " | +-- ValueError\n"
065: + " | | |\n"
066: + " | | +-- UnicodeError\n"
067: + " | |\n"
068: + " | +-- ReferenceError\n"
069: + " | +-- SystemError\n"
070: + " | +-- MemoryError\n"
071: + " |\n"
072: + " +---Warning\n"
073: + " |\n"
074: + " +-- UserWarning\n"
075: + " +-- DeprecationWarning\n"
076: + " +-- SyntaxWarning\n"
077: + " +-- OverflowWarning\n"
078: + " +-- RuntimeWarning";
079:
080: private exceptions() {
081: ;
082: }
083:
084: /** <i>Internal use only. Do not call this method explicit.</i> */
085: public static void classDictInit(PyObject dict) {
086: dict.invoke("clear");
087: dict.__setitem__("__name__", new PyString("exceptions"));
088: dict.__setitem__("__doc__", new PyString(__doc__));
089:
090: ThreadState ts = Py.getThreadState();
091: if (ts.systemState == null) {
092: ts.systemState = Py.defaultSystemState;
093: }
094: // Push frame
095: PyFrame frame = new PyFrame(null, new PyStringMap());
096: frame.f_back = ts.frame;
097: if (frame.f_builtins == null) {
098: if (frame.f_back != null) {
099: frame.f_builtins = frame.f_back.f_builtins;
100: } else {
101: frame.f_builtins = ts.systemState.builtins;
102: }
103: }
104: ts.frame = frame;
105:
106: buildClass(dict, "Exception", null, "Exception",
107: "Proposed base class for all exceptions.");
108:
109: buildClass(dict, "StandardError", "Exception", "empty__init__",
110: "Base class for all standard Python exceptions.");
111:
112: buildClass(dict, "SyntaxError", "StandardError", "SyntaxError",
113: "Invalid syntax");
114:
115: buildClass(dict, "IndentationError", "SyntaxError",
116: "empty__init__", "Improper indentation");
117:
118: buildClass(dict, "TabError", "IndentationError",
119: "empty__init__", "Improper mixture of spaces and tabs.");
120:
121: buildClass(dict, "EnvironmentError", "StandardError",
122: "EnvironmentError",
123: "Base class for I/O related errors.");
124:
125: buildClass(dict, "IOError", "EnvironmentError",
126: "empty__init__", "I/O operation failed.");
127:
128: buildClass(dict, "OSError", "EnvironmentError",
129: "empty__init__", "OS system call failed.");
130:
131: buildClass(dict, "RuntimeError", "StandardError",
132: "empty__init__", "Unspecified run-time error.");
133:
134: buildClass(dict, "NotImplementedError", "RuntimeError",
135: "empty__init__",
136: "Method or function hasn't been implemented yet.");
137:
138: buildClass(
139: dict,
140: "SystemError",
141: "StandardError",
142: "empty__init__",
143: "Internal error in the Python interpreter.\n\n"
144: + "Please report this to the Python maintainer, "
145: + "along with the traceback,\n"
146: + "the Python version, and the hardware/OS "
147: + "platform and version.");
148:
149: buildClass(dict, "ReferenceError", "StandardError",
150: "empty__init__",
151: "Weak ref proxy used after referent went away.");
152:
153: buildClass(dict, "EOFError", "StandardError", "empty__init__",
154: "Read beyond end of file.");
155:
156: buildClass(dict, "ImportError", "StandardError",
157: "empty__init__",
158: "Import can't find module, or can't find name in module.");
159:
160: buildClass(dict, "TypeError", "StandardError", "empty__init__",
161: "Inappropriate argument type.");
162:
163: buildClass(dict, "ValueError", "StandardError",
164: "empty__init__",
165: "Inappropriate argument value (of correct type).");
166:
167: buildClass(dict, "UnicodeError", "ValueError", "empty__init__",
168: "Unicode related error.");
169:
170: buildClass(dict, "KeyboardInterrupt", "StandardError",
171: "empty__init__", "Program interrupted by user.");
172:
173: buildClass(dict, "AssertionError", "StandardError",
174: "empty__init__", "Assertion failed.");
175:
176: buildClass(dict, "ArithmeticError", "StandardError",
177: "empty__init__", "Base class for arithmetic errors.");
178:
179: buildClass(dict, "OverflowError", "ArithmeticError",
180: "empty__init__", "Result too large to be represented.");
181:
182: buildClass(dict, "FloatingPointError", "ArithmeticError",
183: "empty__init__", "Floating point operation failed.");
184:
185: buildClass(dict, "ZeroDivisionError", "ArithmeticError",
186: "empty__init__",
187: "Second argument to a division or modulo operation "
188: + "was zero.");
189:
190: buildClass(dict, "LookupError", "StandardError",
191: "empty__init__", "Base class for lookup errors.");
192:
193: buildClass(dict, "IndexError", "LookupError", "empty__init__",
194: "Sequence index out of range.");
195:
196: buildClass(dict, "KeyError", "LookupError", "empty__init__",
197: "Mapping key not found.");
198:
199: buildClass(dict, "AttributeError", "StandardError",
200: "empty__init__", "Attribute not found.");
201:
202: buildClass(dict, "NameError", "StandardError", "empty__init__",
203: "Name not found globally.");
204:
205: buildClass(dict, "UnboundLocalError", "NameError",
206: "empty__init__",
207: "Local name referenced but not bound to a value.");
208:
209: buildClass(dict, "MemoryError", "StandardError",
210: "empty__init__", "Out of memory.");
211:
212: buildClass(dict, "SystemExit", "Exception", "SystemExit",
213: "Request to exit from the interpreter.");
214:
215: buildClass(dict, "StopIteration", "Exception", "empty__init__",
216: "Signal the end from iterator.next().");
217:
218: buildClass(dict, "Warning", "Exception", "empty__init__",
219: "Base class for warning categories.");
220:
221: buildClass(dict, "UserWarning", "Warning", "empty__init__",
222: "Base class for warnings generated by user code.");
223:
224: buildClass(dict, "DeprecationWarning", "Warning",
225: "empty__init__",
226: "Base class for warnings about deprecated features.");
227:
228: buildClass(dict, "SyntaxWarning", "Warning", "empty__init__",
229: "Base class for warnings about dubious syntax.");
230:
231: buildClass(dict, "RuntimeWarning", "Warning", "empty__init__",
232: "Base class for warnings about dubious runtime behavior.");
233:
234: buildClass(dict, "OverflowWarning", "Warning", "empty__init__",
235: "Base class for warnings about numeric overflow.");
236:
237: ts.frame = ts.frame.f_back;
238: }
239:
240: // An empty __init__ method
241: public static PyObject empty__init__(PyObject[] arg, String[] kws) {
242: PyObject dict = new PyStringMap();
243: dict.__setitem__("__module__", new PyString("exceptions"));
244: return dict;
245: }
246:
247: public static PyObject Exception(PyObject[] arg, String[] kws) {
248: PyObject dict = empty__init__(arg, kws);
249: dict.__setitem__("__init__", getJavaFunc("Exception__init__"));
250: dict.__setitem__("__str__", getJavaFunc("Exception__str__"));
251: dict.__setitem__("__getitem__",
252: getJavaFunc("Exception__getitem__"));
253: return dict;
254: }
255:
256: public static void Exception__init__(PyObject[] arg, String[] kws) {
257: ArgParser ap = new ArgParser("__init__", arg, kws, "self",
258: "args");
259: PyObject self = ap.getPyObject(0);
260: PyObject args = ap.getList(1);
261:
262: self.__setattr__("args", args);
263: }
264:
265: public static PyString Exception__str__(PyObject[] arg, String[] kws) {
266: ArgParser ap = new ArgParser("__str__", arg, kws, "self");
267: PyObject self = ap.getPyObject(0);
268:
269: PyObject args = self.__getattr__("args");
270: if (!args.__nonzero__()) {
271: return new PyString("");
272: } else if (args.__len__() == 1) {
273: return args.__getitem__(0).__str__();
274: } else {
275: return args.__str__();
276: }
277: }
278:
279: public static PyObject Exception__getitem__(PyObject[] arg,
280: String[] kws) {
281: ArgParser ap = new ArgParser("__getitem__", arg, kws, "self",
282: "i");
283: PyObject self = ap.getPyObject(0);
284: PyObject i = ap.getPyObject(1);
285:
286: return self.__getattr__("args").__getitem__(i);
287: }
288:
289: public static PyObject SyntaxError(PyObject[] arg, String[] kws) {
290: PyObject __dict__ = empty__init__(arg, kws);
291: __dict__.__setitem__("filename", Py.None);
292: __dict__.__setitem__("lineno", Py.None);
293: __dict__.__setitem__("offset", Py.None);
294: __dict__.__setitem__("text", Py.None);
295: __dict__.__setitem__("msg", new PyString(""));
296:
297: __dict__.__setitem__("__init__",
298: getJavaFunc("SyntaxError__init__"));
299: __dict__.__setitem__("__str__",
300: getJavaFunc("SyntaxError__str__"));
301: return __dict__;
302: }
303:
304: public static void SyntaxError__init__(PyObject[] arg, String[] kws) {
305: ArgParser ap = new ArgParser("__init__", arg, kws, "self",
306: "args");
307: PyObject self = ap.getPyObject(0);
308: PyObject args = ap.getList(1);
309:
310: self.__setattr__("args", args);
311: if (args.__len__() >= 1) {
312: self.__setattr__("msg", args.__getitem__(0));
313: }
314: if (args.__len__() == 2) {
315: PyObject info = args.__getitem__(1);
316: try {
317: PyObject[] tmp = Py.unpackSequence(info, 4);
318: self.__setattr__("filename", tmp[0]);
319: self.__setattr__("lineno", tmp[1]);
320: self.__setattr__("offset", tmp[2]);
321: self.__setattr__("text", tmp[3]);
322: } catch (PyException exc) {
323: ;
324: }
325: }
326: }
327:
328: public static PyString SyntaxError__str__(PyObject[] arg,
329: String[] kws) {
330: ArgParser ap = new ArgParser("__init__", arg, kws, "self",
331: "args");
332: PyObject self = ap.getPyObject(0);
333: PyString str = self.__getattr__("msg").__str__();
334: PyObject filename = basename(self.__findattr__("filename"));
335: PyObject lineno = self.__findattr__("lineno");
336: if (filename instanceof PyString && lineno instanceof PyInteger) {
337: return new PyString(str + " (" + filename + ", line "
338: + lineno + ")");
339: } else if (filename instanceof PyString) {
340: return new PyString(str + " (" + filename + ")");
341: } else if (lineno instanceof PyInteger) {
342: return new PyString(str + " (line " + lineno + ")");
343: }
344: return str;
345: }
346:
347: private static PyObject basename(PyObject filename) {
348: if (filename instanceof PyString) {
349: int i = ((PyString) filename).rfind(java.io.File.separator);
350: if (i >= 0) {
351: return filename.__getslice__(new PyInteger(i + 1),
352: new PyInteger(Integer.MAX_VALUE));
353: }
354: }
355: return filename;
356: }
357:
358: public static PyObject EnvironmentError(PyObject[] arg, String[] kws) {
359: PyObject dict = empty__init__(arg, kws);
360: dict.__setitem__("__init__",
361: getJavaFunc("EnvironmentError__init__"));
362: dict.__setitem__("__str__",
363: getJavaFunc("EnvironmentError__str__"));
364: return dict;
365: }
366:
367: public static void EnvironmentError__init__(PyObject[] arg,
368: String[] kws) {
369: ArgParser ap = new ArgParser("__init__", arg, kws, "self",
370: "args");
371: PyObject self = ap.getPyObject(0);
372: PyObject args = ap.getList(1);
373:
374: self.__setattr__("args", args);
375: self.__setattr__("errno", Py.None);
376: self.__setattr__("strerror", Py.None);
377: self.__setattr__("filename", Py.None);
378: if (args.__len__() == 3) {
379: // open() errors give third argument which is the filename. BUT,
380: // so common in-place unpacking doesn't break, e.g.:
381: //
382: // except IOError, (errno, strerror):
383: //
384: // we hack args so that it only contains two items. This also
385: // means we need our own __str__() which prints out the filename
386: // when it was supplied.
387: PyObject[] tmp = Py.unpackSequence(args, 3);
388: self.__setattr__("errno", tmp[0]);
389: self.__setattr__("strerror", tmp[1]);
390: self.__setattr__("filename", tmp[2]);
391: self.__setattr__("args", args.__getslice__(Py.Zero, Py
392: .newInteger(2), Py.One));
393: }
394: if (args.__len__() == 2) {
395: // common case: PyErr_SetFromErrno()
396: PyObject[] tmp = Py.unpackSequence(args, 2);
397: self.__setattr__("errno", tmp[0]);
398: self.__setattr__("strerror", tmp[1]);
399: }
400: }
401:
402: public static PyString EnvironmentError__str__(PyObject[] arg,
403: String[] kws) {
404: ArgParser ap = new ArgParser("__init__", arg, kws, "self");
405: PyObject self = ap.getPyObject(0);
406:
407: if (self.__getattr__("filename") != Py.None) {
408: return Py.newString("[Errno %s] %s: %s").__mod__(
409: new PyTuple(new PyObject[] {
410: self.__getattr__("errno"),
411: self.__getattr__("strerror"),
412: self.__getattr__("filename") })).__str__();
413: } else if (self.__getattr__("errno").__nonzero__()
414: && self.__getattr__("strerror").__nonzero__()) {
415: return Py.newString("[Errno %s] %s").__mod__(
416: new PyTuple(new PyObject[] {
417: self.__getattr__("errno"),
418: self.__getattr__("strerror") })).__str__();
419: } else {
420: return Exception__str__(arg, kws);
421: }
422: }
423:
424: public static PyObject SystemExit(PyObject[] arg, String[] kws) {
425: PyObject dict = empty__init__(arg, kws);
426: dict.__setitem__("__init__", getJavaFunc("SystemExit__init__"));
427: return dict;
428: }
429:
430: public static void SystemExit__init__(PyObject[] arg, String[] kws) {
431: ArgParser ap = new ArgParser("__init__", arg, kws, "self",
432: "args");
433: PyObject self = ap.getPyObject(0);
434: PyObject args = ap.getList(1);
435:
436: self.__setattr__("args", args);
437: if (args.__len__() == 0) {
438: self.__setattr__("code", Py.None);
439: } else if (args.__len__() == 1) {
440: self.__setattr__("code", args.__getitem__(0));
441: } else {
442: self.__setattr__("code", args);
443: }
444: }
445:
446: private static PyObject getJavaFunc(String name) {
447: return Py.newJavaFunc(exceptions.class, name);
448: }
449:
450: private static PyObject buildClass(PyObject dict, String classname,
451: String super class, String classCodeName, String doc) {
452: PyObject[] sclass = Py.EmptyObjects;
453: if (super class != null) {
454: sclass = new PyObject[] { dict.__getitem__(new PyString(
455: super class)) };
456: }
457: PyObject cls = Py.makeClass(classname, sclass, Py.newJavaCode(
458: exceptions.class, classCodeName), new PyString(doc));
459: dict.__setitem__(classname, cls);
460: return cls;
461: }
462: }
|