KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyException


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3 import java.io.*;
4
5 /**
6  * A wrapper for all python exception. Note that the wellknown
7  * python exception are <b>not</b> subclasses of PyException.
8  * Instead the python exception class is stored in the
9  * <code>type</code> field and value or class instance is stored
10  * in the <code>value</code> field.
11  */

12
13 public class PyException extends RuntimeException JavaDoc
14 {
15     /**
16      * The python exception class (for class exception) or
17      * identifier (for string exception).
18      */

19     public PyObject type;
20
21     /**
22      * The exception instance (for class exception) or exception
23      * value (for string exception).
24      */

25     public PyObject value = Py.None;
26
27     public PyTraceback traceback;
28     private boolean instantiated=false;
29
30     public void instantiate() {
31         if (!instantiated) {
32             // repeatedly, replace a tuple exception with its first item
33
while (type instanceof PyTuple && type.__len__() > 0) {
34                 type = type.__getitem__(0);
35             }
36             if (type instanceof PyClass &&
37                 (!(value instanceof PyInstance &&
38                    __builtin__.isinstance(value, (PyClass)type))))
39             {
40                 //System.out.println("value: "+value);
41
if (value instanceof PyTuple) {
42                     value = ((PyClass)type).__call__(
43                             ((PyTuple)value).getArray());
44                 } else {
45                     if (value == Py.None) {
46                         value = ((PyClass)type).__call__(Py.EmptyObjects);
47                     } else {
48                         value = ((PyClass)type).__call__(
49                             new PyObject[] {value});
50                     }
51                 }
52             }
53             instantiated = true;
54         }
55     }
56
57     public PyException() {
58         //System.out.println("PyException");
59
//super.printStackTrace();
60
this(Py.None, Py.None);
61     }
62
63     public PyException(PyObject type) {
64         this(type, Py.None);
65     }
66
67     public PyException(PyObject type, PyObject value) {
68         this.type = type;
69         this.value = value;
70
71         PyFrame frame = Py.getFrame();
72         traceback = new PyTraceback(frame);
73         if (frame != null && frame.tracefunc != null) {
74             frame.tracefunc = frame.tracefunc.traceException(frame, this);
75         }
76     }
77
78     public PyException(PyObject type, String JavaDoc value) {
79         this(type, new PyString(value));
80     }
81
82     public PyException(PyObject type, PyObject value, PyTraceback traceback) {
83         this.type = type;
84         this.value = value;
85         this.traceback = traceback;
86     }
87
88     private boolean printingStackTrace = false;
89     public void printStackTrace() {
90         Py.printException(this);
91     }
92
93     public synchronized void printStackTrace(PrintStream s) {
94         //System.err.println("printStackTrace: "+s+", "+printingStackTrace);
95
if (printingStackTrace) {
96             super.printStackTrace(s);
97         } else {
98             try {
99                 printingStackTrace = true;
100                 Py.displayException(type, value, traceback, new PyFile(s));
101             } finally {
102                 printingStackTrace = false;
103             }
104         }
105     }
106
107     public synchronized void super__printStackTrace(PrintWriter w) {
108         try {
109             printingStackTrace = true;
110             super.printStackTrace(w);
111         } finally {
112             printingStackTrace = false;
113         }
114         //Py.printException(this, null, new PyFile(s));
115
}
116
117     public synchronized String JavaDoc toString() {
118         ByteArrayOutputStream buf = new ByteArrayOutputStream();
119         if (!printingStackTrace) {
120             printStackTrace(new PrintStream(buf));
121         }
122         return buf.toString();
123     }
124 }
125
Popular Tags