KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > util > InteractiveInterpreter


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.lateralnz.common.util;
3
4 import org.python.core.*;
5 import org.python.util.*;
6
7 // Based on CPython-1.5.2's code module
8

9 // JRB 13/08/2004 : can't pass a new state into this so temporarily reproduced this class here
10

11 public class InteractiveInterpreter extends PythonInterpreter {
12     private InteractiveInterpreter() {
13     }
14     public InteractiveInterpreter(PyObject locals, PySystemState systemState) {
15         super(locals, systemState);
16         cflags = new CompilerFlags();
17     }
18
19     /**
20      * Compile and run some source in the interpreter.
21      *
22      * Arguments are as for compile_command().
23      *
24      * One several things can happen:
25      *
26      * 1) The input is incorrect; compile_command() raised an exception
27      * (SyntaxError or OverflowError). A syntax traceback will be printed
28      * by calling the showsyntaxerror() method.
29      *
30      * 2) The input is incomplete, and more input is required;
31      * compile_command() returned None. Nothing happens.
32      *
33      * 3) The input is complete; compile_command() returned a code object.
34      * The code is executed by calling self.runcode() (which also handles
35      * run-time exceptions, except for SystemExit).
36      *
37      * The return value is 1 in case 2, 0 in the other cases (unless an
38      * exception is raised). The return value can be used to decide
39      * whether to use sys.ps1 or sys.ps2 to prompt the next line.
40      **/

41     public boolean runsource(String JavaDoc source) {
42         return runsource(source, "<input>", "single");
43     }
44
45     public boolean runsource(String JavaDoc source, String JavaDoc filename) {
46         return runsource(source, filename, "single");
47     }
48
49     public boolean runsource(String JavaDoc source, String JavaDoc filename, String JavaDoc symbol) {
50         PyObject code;
51         try {
52             code = org.python.modules.codeop.compile_command_flags(
53                 source, filename, symbol,cflags);
54         } catch (PyException exc) {
55             if (Py.matchException(exc, Py.SyntaxError)) {
56                 // Case 1
57
showexception(exc);
58                 return false;
59             } else if (Py.matchException(exc, Py.ValueError) ||
60                        Py.matchException(exc, Py.OverflowError)) {
61                 // Should not print the stack trace, just the error.
62
showexception(exc);
63                 return false;
64             } else {
65                 throw exc;
66             }
67         }
68         // Case 2
69
if (code == Py.None)
70             return true;
71         // Case 3
72
runcode(code);
73         return false;
74     }
75
76     /**
77      * execute a code object.
78      *
79      * When an exception occurs, self.showtraceback() is called to display
80      * a traceback. All exceptions are caught except SystemExit, which is
81      * reraised.
82      *
83      * A note about KeyboardInterrupt: this exception may occur elsewhere
84      * in this code, and may not always be caught. The caller should be
85      * prepared to deal with it.
86      **/

87
88     // Make this run in another thread somehow????
89
public void runcode(PyObject code) {
90         try {
91             exec(code);
92         } catch (PyException exc) {
93             if (Py.matchException(exc, Py.SystemExit)) throw exc;
94             showexception(exc);
95         }
96     }
97
98     public void showexception(PyException exc) {
99         // Should probably add code to handle skipping top stack frames
100
// somehow...
101
write(exc.toString());
102     }
103
104     public void write(String JavaDoc data) {
105         Py.stderr.write(data);
106     }
107
108     public StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
109     public String JavaDoc filename="<stdin>";
110
111     public void resetbuffer() {
112         buffer.setLength(0);
113     }
114
115     /** Pause the current code, sneak an exception raiser into
116      * sys.trace_func, and then continue the code hoping that JPython will
117      * get control to do the break;
118      **/

119     public void interrupt(ThreadState ts) {
120         TraceFunction breaker = new BreakTraceFunction();
121         TraceFunction oldTrace = ts.systemState.tracefunc;
122         ts.systemState.tracefunc = breaker;
123         if (ts.frame != null)
124             ts.frame.tracefunc = breaker;
125         ts.systemState.tracefunc = oldTrace;
126         //ts.thread.join();
127
}
128 }
129
130 class BreakTraceFunction extends TraceFunction {
131     private void doBreak() {
132         throw new Error JavaDoc("Python interrupt");
133         //Thread.currentThread().interrupt();
134
}
135
136     public TraceFunction traceCall(PyFrame frame) {
137         doBreak();
138         return null;
139     }
140
141     public TraceFunction traceReturn(PyFrame frame, PyObject ret) {
142         doBreak();
143         return null;
144     }
145
146     public TraceFunction traceLine(PyFrame frame, int line) {
147         doBreak();
148         return null;
149     }
150
151     public TraceFunction traceException(PyFrame frame, PyException exc) {
152         doBreak();
153         return null;
154     }
155 }
156
Popular Tags