01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.core;
03:
04: class PythonTraceFunction extends TraceFunction {
05:
06: PyObject tracefunc;
07:
08: PythonTraceFunction(PyObject tracefunc) {
09: this .tracefunc = tracefunc;
10: }
11:
12: private TraceFunction safeCall(PyFrame frame, String label,
13: PyObject arg) {
14: synchronized (imp.class) {
15: synchronized (this ) {
16: ThreadState ts = Py.getThreadState();
17: if (ts.tracing)
18: return null;
19: if (tracefunc == null)
20: return null;
21: PyObject ret = null;
22: try {
23: ts.tracing = true;
24: ret = tracefunc.__call__(frame,
25: 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: }
42:
43: public TraceFunction traceCall(PyFrame frame) {
44: return safeCall(frame, "call", Py.None);
45: }
46:
47: public TraceFunction traceReturn(PyFrame frame, PyObject ret) {
48: return safeCall(frame, "return", ret);
49: }
50:
51: public TraceFunction traceLine(PyFrame frame, int line) {
52: return safeCall(frame, "line", Py.None);
53: }
54:
55: public TraceFunction traceException(PyFrame frame, PyException exc) {
56: return safeCall(frame, "exception", new PyTuple(new PyObject[] {
57: exc.type, exc.value, exc.traceback }));
58: }
59: }
|