001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: import java.io.*;
005:
006: /**
007: * A wrapper for all python exception. Note that the wellknown
008: * python exception are <b>not</b> subclasses of PyException.
009: * Instead the python exception class is stored in the
010: * <code>type</code> field and value or class instance is stored
011: * in the <code>value</code> field.
012: */
013:
014: public class PyException extends RuntimeException {
015: /**
016: * The python exception class (for class exception) or
017: * identifier (for string exception).
018: */
019: public PyObject type;
020:
021: /**
022: * The exception instance (for class exception) or exception
023: * value (for string exception).
024: */
025: public PyObject value = Py.None;
026:
027: public PyTraceback traceback;
028: private boolean instantiated = false;
029:
030: public void instantiate() {
031: if (!instantiated) {
032: // repeatedly, replace a tuple exception with its first item
033: while (type instanceof PyTuple && type.__len__() > 0) {
034: type = type.__getitem__(0);
035: }
036: if (type instanceof PyClass
037: && (!(value instanceof PyInstance && __builtin__
038: .isinstance(value, (PyClass) type)))) {
039: //System.out.println("value: "+value);
040: if (value instanceof PyTuple) {
041: value = ((PyClass) type).__call__(((PyTuple) value)
042: .getArray());
043: } else {
044: if (value == Py.None) {
045: value = ((PyClass) type)
046: .__call__(Py.EmptyObjects);
047: } else {
048: value = ((PyClass) type)
049: .__call__(new PyObject[] { value });
050: }
051: }
052: }
053: instantiated = true;
054: }
055: }
056:
057: public PyException() {
058: //System.out.println("PyException");
059: //super.printStackTrace();
060: this (Py.None, Py.None);
061: }
062:
063: public PyException(PyObject type) {
064: this (type, Py.None);
065: }
066:
067: public PyException(PyObject type, PyObject value) {
068: this .type = type;
069: this .value = value;
070:
071: PyFrame frame = Py.getFrame();
072: traceback = new PyTraceback(frame);
073: if (frame != null && frame.tracefunc != null) {
074: frame.tracefunc = frame.tracefunc.traceException(frame,
075: this );
076: }
077: }
078:
079: public PyException(PyObject type, String value) {
080: this (type, new PyString(value));
081: }
082:
083: public PyException(PyObject type, PyObject value,
084: PyTraceback traceback) {
085: this .type = type;
086: this .value = value;
087: this .traceback = traceback;
088: }
089:
090: private boolean printingStackTrace = false;
091:
092: public void printStackTrace() {
093: Py.printException(this );
094: }
095:
096: public synchronized void printStackTrace(PrintStream s) {
097: //System.err.println("printStackTrace: "+s+", "+printingStackTrace);
098: if (printingStackTrace) {
099: super .printStackTrace(s);
100: } else {
101: try {
102: printingStackTrace = true;
103: Py.displayException(type, value, traceback, new PyFile(
104: s));
105: } finally {
106: printingStackTrace = false;
107: }
108: }
109: }
110:
111: public synchronized void super __printStackTrace(PrintWriter w) {
112: try {
113: printingStackTrace = true;
114: super .printStackTrace(w);
115: } finally {
116: printingStackTrace = false;
117: }
118: //Py.printException(this, null, new PyFile(s));
119: }
120:
121: public synchronized String toString() {
122: ByteArrayOutputStream buf = new ByteArrayOutputStream();
123: if (!printingStackTrace) {
124: printStackTrace(new PrintStream(buf));
125: }
126: return buf.toString();
127: }
128: }
|