Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(67)

Unified Diff: src/org/python/util/jython.java

Issue 2810: SystemRestart exception to restart the interpreter (Closed) SVN Base: https://jython.svn.sourceforge.net/svnroot/jython/branches/asm
Patch Set: Created 1 year, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side by-side-diff with in-line comments
Download patch
Index: src/org/python/util/jython.java
===================================================================
--- src/org/python/util/jython.java (revisión: 5073)
+++ src/org/python/util/jython.java (copia de trabajo)
@@ -15,12 +15,12 @@
import org.python.core.PyException;
import org.python.core.PyFile;
import org.python.core.PyModule;
-import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyStringMap;
import org.python.core.PySystemState;
import org.python.core.imp;
import org.python.core.util.RelativeFile;
+import org.python.modules.thread.thread;
public class jython
{
@@ -59,6 +59,8 @@
"JYTHONPATH: '" + java.io.File.pathSeparator + "'-separated list of directories prefixed to the default module\n" +
" search path. The result is sys.path.";
+ private static boolean restart;
Nicholas Riley 2008/08/04 02:59:41 perhaps "shouldRestart"?
+
public static void runJar(String filename) {
// TBD: this is kind of gross because a local called `zipfile' just
// magically shows up in the module's globals. Either `zipfile'
@@ -102,6 +104,13 @@
}
public static void main(String[] args) {
+ do {
+ restart = false;
+ run(args);
+ } while (restart);
+ }
+
+ public static void run(String[] args) {
// Parse the command line options
CommandLineOptions opts = new CommandLineOptions();
if (!opts.parse(args)) {
@@ -120,22 +129,8 @@
opts.properties, opts.argv);
// Now create an interpreter
- InteractiveConsole interp = null;
- try {
- String interpClass = PySystemState.registry.getProperty(
- "python.console",
- "org.python.util.InteractiveConsole");
- interp = (InteractiveConsole)
- Class.forName(interpClass).newInstance();
- } catch (Exception e) {
- interp = new InteractiveConsole();
- }
+ InteractiveConsole interp = newInterpreter();
- //System.err.println("interp");
- PyModule mod = imp.addModule("__main__");
- interp.setLocals(mod.__dict__);
- //System.err.println("imp");
-
for (int i = 0; i < opts.warnoptions.size(); i++) {
String wopt = (String) opts.warnoptions.elementAt(i);
PySystemState.warnoptions.append(new PyString(wopt));
@@ -209,7 +204,6 @@
try {
interp.locals.__setitem__(new PyString("__file__"),
new PyString(opts.filename));
-
FileInputStream file;
try {
file = new java.io.FileInputStream(new RelativeFile(opts.filename));
@@ -224,6 +218,15 @@
interp.execfile(file, opts.filename);
}
} catch(Throwable t) {
+ if (t instanceof PyException &&
+ Py.matchException((PyException)t, Py.SystemRestart)) {
+ // Stop current threads...
+ thread.interruptAllThreads();
+ // ..reset the state...
+ Py.setSystemState(new PySystemState());
+ // ...and start again
+ restart = true;
+ } else {
Py.printException(t);
if (!opts.interactive) {
interp.cleanup();
@@ -232,6 +235,7 @@
}
}
}
+ }
else {
// if there was no file name on the command line, then "" is
// the first element on sys.path. This is here because if
@@ -273,8 +277,31 @@
System.exit(0);
}
}
+
+ /**
fwierzbicki 2008/08/04 13:42:57 I like that this has been broken out into its own
+ * @return a new python interpreter, instantiating the right
+ * InteractiveConsole subclass for the configured <tt>python.console</tt>
+ */
+ private static InteractiveConsole newInterpreter() {
+ InteractiveConsole interp = null;
+ try {
+ String interpClass = PySystemState.registry.getProperty(
+ "python.console",
+ "org.python.util.InteractiveConsole");
+ interp = (InteractiveConsole)
+ Class.forName(interpClass).newInstance();
+ } catch (Exception e) {
+ interp = new InteractiveConsole();
}
+ //System.err.println("interp");
+ PyModule mod = imp.addModule("__main__");
+ interp.setLocals(mod.__dict__);
+ //System.err.println("imp");
+ return interp;
+ }
+}
+
class CommandLineOptions
{
public String filename;

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497