KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > util > InteractiveInterpreter


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

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

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

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

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