KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 class PythonTraceFunction extends TraceFunction
5 {
6     PyObject tracefunc;
7
8     PythonTraceFunction(PyObject tracefunc) {
9         this.tracefunc = tracefunc;
10     }
11
12     private synchronized TraceFunction safeCall(PyFrame frame, String JavaDoc label,
13                                                 PyObject arg)
14     {
15         ThreadState ts = Py.getThreadState();
16         if (ts.tracing)
17             return null;
18
19         if (tracefunc == null)
20             return null;
21
22         PyObject ret = null;
23         try {
24             ts.tracing = true;
25             ret = tracefunc.__call__(frame, new PyString(label), arg);
26         } catch (PyException exc) {
27             frame.tracefunc = null;
28             ts.systemState.tracefunc = null;
29             ts.systemState.profilefunc = null;
30             throw exc;
31         } finally {
32             ts.tracing = false;
33         }
34         if (ret == tracefunc)
35             return this;
36         if (ret == Py.None)
37             return null;
38         return new PythonTraceFunction(ret);
39     }
40
41     public TraceFunction traceCall(PyFrame frame) {
42         return safeCall(frame, "call", Py.None);
43     }
44
45     public TraceFunction traceReturn(PyFrame frame, PyObject ret) {
46         return safeCall(frame, "return", ret);
47     }
48
49     public TraceFunction traceLine(PyFrame frame, int line) {
50         return safeCall(frame, "line", Py.None);
51     }
52
53     public TraceFunction traceException(PyFrame frame, PyException exc) {
54         return safeCall(frame, "exception",
55                         new PyTuple(new PyObject[] {exc.type, exc.value,
56                                                     exc.traceback
57                         }));
58     }
59 }
60
Popular Tags