1 package org.python.core; 3 import java.io.*; 4 5 12 13 public class PyException extends RuntimeException 14 { 15 19 public PyObject type; 20 21 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 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 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 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 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 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 } 116 117 public synchronized String toString() { 118 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 119 if (!printingStackTrace) { 120 printStackTrace(new PrintStream(buf)); 121 } 122 return buf.toString(); 123 } 124 } 125 | Popular Tags |