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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) Corporation for National Research Initiatives 1 // Copyright (c) Corporation for National Research Initiatives
2 package org.python.util; 2 package org.python.util;
3 3
4 import java.io.File; 4 import java.io.File;
5 import java.io.IOException; 5 import java.io.IOException;
6 import java.io.FileInputStream; 6 import java.io.FileInputStream;
7 import java.io.InputStream; 7 import java.io.InputStream;
8 import java.nio.charset.Charset; 8 import java.nio.charset.Charset;
9 import java.util.zip.ZipEntry; 9 import java.util.zip.ZipEntry;
10 import java.util.zip.ZipFile; 10 import java.util.zip.ZipFile;
11 11
12 import org.python.core.Options; 12 import org.python.core.Options;
13 import org.python.core.Py; 13 import org.python.core.Py;
14 import org.python.core.PyCode; 14 import org.python.core.PyCode;
15 import org.python.core.PyException; 15 import org.python.core.PyException;
16 import org.python.core.PyFile; 16 import org.python.core.PyFile;
17 import org.python.core.PyModule; 17 import org.python.core.PyModule;
18 import org.python.core.PyObject;
19 import org.python.core.PyString; 18 import org.python.core.PyString;
20 import org.python.core.PyStringMap; 19 import org.python.core.PyStringMap;
21 import org.python.core.PySystemState; 20 import org.python.core.PySystemState;
22 import org.python.core.imp; 21 import org.python.core.imp;
23 import org.python.core.util.RelativeFile; 22 import org.python.core.util.RelativeFile;
23 import org.python.modules.thread.thread;
24 24
25 public class jython 25 public class jython
26 { 26 {
27 private static final String COPYRIGHT = 27 private static final String COPYRIGHT =
28 "Type \"help\", \"copyright\", \"credits\" or \"license\" for more i nformation."; 28 "Type \"help\", \"copyright\", \"credits\" or \"license\" for more i nformation.";
29 29
30 private static final String usage = 30 private static final String usage =
31 // "usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ...\n" + 31 // "usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ...\n" +
32 "usage: jython [option] ... [-c cmd | file | -] [arg] ...\n" + 32 "usage: jython [option] ... [-c cmd | file | -] [arg] ...\n" +
33 "Options and arguments:\n" + //(and corresponding environment variables) :\n" + 33 "Options and arguments:\n" + //(and corresponding environment variables) :\n" +
(...skipping 17 matching lines...) Expand all
51 "-v : verbose (trace import statements)\n" + // (also PYTHONVERBOS E=x)\n" + 51 "-v : verbose (trace import statements)\n" + // (also PYTHONVERBOS E=x)\n" +
52 "-V : print the Python version number and exit (also --version)\n" + 52 "-V : print the Python version number and exit (also --version)\n" +
53 "-W arg : warning control (arg is action:message:category:module:linen o)\n" + 53 "-W arg : warning control (arg is action:message:category:module:linen o)\n" +
54 //"-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n" + 54 //"-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n" +
55 "file : program read from script file\n" + 55 "file : program read from script file\n" +
56 "- : program read from stdin (default; interactive mode if a tty) \n" + 56 "- : program read from stdin (default; interactive mode if a tty) \n" +
57 "arg ... : arguments passed to program in sys.argv[1:]\n" + 57 "arg ... : arguments passed to program in sys.argv[1:]\n" +
58 "Other environment variables:\n" + 58 "Other environment variables:\n" +
59 "JYTHONPATH: '" + java.io.File.pathSeparator + "'-separated list of dire ctories prefixed to the default module\n" + 59 "JYTHONPATH: '" + java.io.File.pathSeparator + "'-separated list of dire ctories prefixed to the default module\n" +
60 " search path. The result is sys.path."; 60 " search path. The result is sys.path.";
61
62 private static boolean restart;
Nicholas Riley 2008/08/04 02:59:41 perhaps "shouldRestart"?
61 63
62 public static void runJar(String filename) { 64 public static void runJar(String filename) {
63 // TBD: this is kind of gross because a local called `zipfile' just 65 // TBD: this is kind of gross because a local called `zipfile' just
64 // magically shows up in the module's globals. Either `zipfile' 66 // magically shows up in the module's globals. Either `zipfile'
65 // should be called `__zipfile__' or (preferrably, IMO), __run__.py 67 // should be called `__zipfile__' or (preferrably, IMO), __run__.py
66 // should be imported and a main() function extracted. This 68 // should be imported and a main() function extracted. This
67 // function should be called passing zipfile in as an argument. 69 // function should be called passing zipfile in as an argument.
68 // 70 //
69 // Probably have to keep this code around for backwards 71 // Probably have to keep this code around for backwards
70 // compatibility (?) 72 // compatibility (?)
(...skipping 24 matching lines...) Expand all
95 } finally { 97 } finally {
96 file.close(); 98 file.close();
97 } 99 }
98 Py.runCode(code, locals, locals); 100 Py.runCode(code, locals, locals);
99 } catch (IOException e) { 101 } catch (IOException e) {
100 throw Py.IOError(e); 102 throw Py.IOError(e);
101 } 103 }
102 } 104 }
103 105
104 public static void main(String[] args) { 106 public static void main(String[] args) {
107 do {
108 restart = false;
109 run(args);
110 } while (restart);
111 }
112
113 public static void run(String[] args) {
105 // Parse the command line options 114 // Parse the command line options
106 CommandLineOptions opts = new CommandLineOptions(); 115 CommandLineOptions opts = new CommandLineOptions();
107 if (!opts.parse(args)) { 116 if (!opts.parse(args)) {
108 if (opts.version) { 117 if (opts.version) {
109 PySystemState.determinePlatform(System.getProperties()); 118 PySystemState.determinePlatform(System.getProperties());
110 System.err.println(InteractiveConsole.getDefaultBanner()); 119 System.err.println(InteractiveConsole.getDefaultBanner());
111 System.exit(0); 120 System.exit(0);
112 } 121 }
113 System.err.println(usage); 122 System.err.println(usage);
114 int exitcode = opts.help ? 0 : -1; 123 int exitcode = opts.help ? 0 : -1;
115 System.exit(exitcode); 124 System.exit(exitcode);
116 } 125 }
117 126
118 // Setup the basic python system state from these options 127 // Setup the basic python system state from these options
119 PySystemState.initialize(PySystemState.getBaseProperties(), 128 PySystemState.initialize(PySystemState.getBaseProperties(),
120 opts.properties, opts.argv); 129 opts.properties, opts.argv);
121 130
122 // Now create an interpreter 131 // Now create an interpreter
123 InteractiveConsole interp = null; 132 InteractiveConsole interp = newInterpreter();
124 try {
125 String interpClass = PySystemState.registry.getProperty(
126 "python.console",
127 "org.python.util.InteractiveConsole");
128 interp = (InteractiveConsole)
129 Class.forName(interpClass).newInstance();
130 } catch (Exception e) {
131 interp = new InteractiveConsole();
132 }
133
134 //System.err.println("interp");
135 PyModule mod = imp.addModule("__main__");
136 interp.setLocals(mod.__dict__);
137 //System.err.println("imp");
138 133
139 for (int i = 0; i < opts.warnoptions.size(); i++) { 134 for (int i = 0; i < opts.warnoptions.size(); i++) {
140 String wopt = (String) opts.warnoptions.elementAt(i); 135 String wopt = (String) opts.warnoptions.elementAt(i);
141 PySystemState.warnoptions.append(new PyString(wopt)); 136 PySystemState.warnoptions.append(new PyString(wopt));
142 } 137 }
143 138
144 // Decide if stdin is interactive 139 // Decide if stdin is interactive
145 if (!opts.fixInteractive && opts.interactive) { 140 if (!opts.fixInteractive && opts.interactive) {
146 opts.interactive = ((PyFile)Py.defaultSystemState.stdin).isatty(); 141 opts.interactive = ((PyFile)Py.defaultSystemState.stdin).isatty();
147 if (!opts.interactive) { 142 if (!opts.interactive) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 interp.locals.__setitem__(new PyString("__file__"), 197 interp.locals.__setitem__(new PyString("__file__"),
203 new PyString("<stdin>")); 198 new PyString("<stdin>"));
204 interp.execfile(System.in, "<stdin>"); 199 interp.execfile(System.in, "<stdin>");
205 } catch (Throwable t) { 200 } catch (Throwable t) {
206 Py.printException(t); 201 Py.printException(t);
207 } 202 }
208 } else { 203 } else {
209 try { 204 try {
210 interp.locals.__setitem__(new PyString("__file__"), 205 interp.locals.__setitem__(new PyString("__file__"),
211 new PyString(opts.filename)); 206 new PyString(opts.filename));
212
213 FileInputStream file; 207 FileInputStream file;
214 try { 208 try {
215 file = new java.io.FileInputStream(new RelativeFile(opts. filename)); 209 file = new java.io.FileInputStream(new RelativeFile(opts. filename));
216 } catch (java.io.FileNotFoundException e) { 210 } catch (java.io.FileNotFoundException e) {
217 throw Py.IOError(e); 211 throw Py.IOError(e);
218 } 212 }
219 if (imp.load("os").__getattr__("isatty").__call__(Py.java2py( file.getFD())).__nonzero__()) { 213 if (imp.load("os").__getattr__("isatty").__call__(Py.java2py( file.getFD())).__nonzero__()) {
220 opts.interactive = true; 214 opts.interactive = true;
221 interp.interact(null, new PyFile(file)); 215 interp.interact(null, new PyFile(file));
222 System.exit(0); 216 System.exit(0);
223 } else { 217 } else {
224 interp.execfile(file, opts.filename); 218 interp.execfile(file, opts.filename);
225 } 219 }
226 } catch(Throwable t) { 220 } catch(Throwable t) {
221 if (t instanceof PyException &&
222 Py.matchException((PyException)t, Py.SystemRestart)) {
223 // Stop current threads...
224 thread.interruptAllThreads();
225 // ..reset the state...
226 Py.setSystemState(new PySystemState());
227 // ...and start again
228 restart = true;
229 } else {
227 Py.printException(t); 230 Py.printException(t);
228 if (!opts.interactive) { 231 if (!opts.interactive) {
229 interp.cleanup(); 232 interp.cleanup();
230 System.exit(-1); 233 System.exit(-1);
231 } 234 }
232 } 235 }
233 } 236 }
237 }
234 } 238 }
235 else { 239 else {
236 // if there was no file name on the command line, then "" is 240 // if there was no file name on the command line, then "" is
237 // the first element on sys.path. This is here because if 241 // the first element on sys.path. This is here because if
238 // there /was/ a filename on the c.l., and say the -i option 242 // there /was/ a filename on the c.l., and say the -i option
239 // was given, sys.path[0] will have gotten filled in with the 243 // was given, sys.path[0] will have gotten filled in with the
240 // dir of the argument filename. 244 // dir of the argument filename.
241 Py.getSystemState().path.insert(0, new PyString("")); 245 Py.getSystemState().path.insert(0, new PyString(""));
242 246
243 if (opts.command != null) { 247 if (opts.command != null) {
(...skipping 21 matching lines...) Expand all
265 try { 269 try {
266 interp.interact(null, null); 270 interp.interact(null, null);
267 } catch (Throwable t) { 271 } catch (Throwable t) {
268 Py.printException(t); 272 Py.printException(t);
269 } 273 }
270 } 274 }
271 interp.cleanup(); 275 interp.cleanup();
272 if (opts.fixInteractive || opts.interactive) { 276 if (opts.fixInteractive || opts.interactive) {
273 System.exit(0); 277 System.exit(0);
274 } 278 }
279 }
280
281 /**
fwierzbicki 2008/08/04 13:42:57 I like that this has been broken out into its own
282 * @return a new python interpreter, instantiating the right
283 * InteractiveConsole subclass for the configured <tt>python.console</tt>
284 */
285 private static InteractiveConsole newInterpreter() {
286 InteractiveConsole interp = null;
287 try {
288 String interpClass = PySystemState.registry.getProperty(
289 "python.console",
290 "org.python.util.InteractiveConsole");
291 interp = (InteractiveConsole)
292 Class.forName(interpClass).newInstance();
293 } catch (Exception e) {
294 interp = new InteractiveConsole();
295 }
296
297 //System.err.println("interp");
298 PyModule mod = imp.addModule("__main__");
299 interp.setLocals(mod.__dict__);
300 //System.err.println("imp");
301 return interp;
275 } 302 }
276 } 303 }
277 304
278 class CommandLineOptions 305 class CommandLineOptions
279 { 306 {
280 public String filename; 307 public String filename;
281 public boolean jar, interactive, notice; 308 public boolean jar, interactive, notice;
282 public boolean fixInteractive; 309 public boolean fixInteractive;
283 public boolean help, version; 310 public boolean help, version;
284 public String[] argv; 311 public String[] argv;
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 else 441 else
415 argv[0] = ""; 442 argv[0] = "";
416 443
417 for(int i=1; i<n; i++, index++) { 444 for(int i=1; i<n; i++, index++) {
418 argv[i] = args[index]; 445 argv[i] = args[index];
419 } 446 }
420 447
421 return true; 448 return true;
422 } 449 }
423 } 450 }
OLDNEW

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