| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Jython
Revision: 5271
Author: pjenvey
Date: 29 Aug 2008 17:08:52
Changes:PEP 338 (jython -m mod) support
patch from Georgy Berdyshev
| ... | ...@@ -26,11 +26,12 @@ | |
| 26 | 26 | public class jython |
| 27 | 27 | { |
| 28 | 28 | private static final String COPYRIGHT = |
| 29 | "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information."; | |
| 29 | "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information."; | |
| 30 | 30 | |
| 31 | private static final String usage = | |
| 32 | // "usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ...\n" + | |
| 33 | "usage: jython [option] ... [-c cmd | file | -] [arg] ...\n" + | |
| 31 | static final String usageHeader = | |
| 32 | "usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ...\n"; | |
| 33 | ||
| 34 | private static final String usage = usageHeader + | |
| 34 | 35 | "Options and arguments:\n" + //(and corresponding environment variables):\n" + |
| 35 | 36 | "-c cmd : program passed in as string (terminates option list)\n" + |
| 36 | 37 | //"-d : debug output from parser (also PYTHONDEBUG=x)\n" + |
| ... | ...@@ -120,7 +121,10 @@ | |
| 120 | 121 | System.err.println(InteractiveConsole.getDefaultBanner()); |
| 121 | 122 | System.exit(0); |
| 122 | 123 | } |
| 123 | System.err.println(usage); | |
| 124 | if (!opts.runModule) { | |
| 125 | System.err.println(usage); | |
| 126 | } | |
| 127 | ||
| 124 | 128 | int exitcode = opts.help ? 0 : -1; |
| 125 | 129 | System.exit(exitcode); |
| 126 | 130 | } |
| ... | ...@@ -147,14 +151,14 @@ | |
| 147 | 151 | } |
| 148 | 152 | |
| 149 | 153 | // Print banner and copyright information (or not) |
| 150 | if (opts.interactive && opts.notice) { | |
| 154 | if (opts.interactive && opts.notice && !opts.runModule) { | |
| 151 | 155 | System.err.println(InteractiveConsole.getDefaultBanner()); |
| 152 | 156 | } |
| 153 | 157 | |
| 154 | 158 | if (Options.importSite) { |
| 155 | 159 | try { |
| 156 | 160 | imp.load("site"); |
| 157 | if (opts.interactive && opts.notice) { | |
| 161 | if (opts.interactive && opts.notice && !opts.runModule) { | |
| 158 | 162 | System.err.println(COPYRIGHT); |
| 159 | 163 | } |
| 160 | 164 | } catch (PyException pye) { |
| ... | ...@@ -253,6 +257,21 @@ | |
| 253 | 257 | Py.printException(t); |
| 254 | 258 | } |
| 255 | 259 | } |
| 260 | ||
| 261 | if (opts.moduleName != null) { | |
| 262 | // PEP 338 - Execute module as a script | |
| 263 | try { | |
| 264 | interp.exec("import runpy"); | |
| 265 | interp.set("name", Py.newString(opts.moduleName)); | |
| 266 | interp.exec("runpy.run_module(name, run_name='__main__', alter_sys=True)"); | |
| 267 | interp.cleanup(); | |
| 268 | System.exit(0); | |
| 269 | } catch (Throwable t) { | |
| 270 | Py.printException(t); | |
| 271 | interp.cleanup(); | |
| 272 | System.exit(0); | |
| 273 | } | |
| 274 | } | |
| 256 | 275 | } |
| 257 | 276 | |
| 258 | 277 | if (opts.fixInteractive || (opts.filename == null && opts.command == null)) { |
| ... | ...@@ -308,6 +327,7 @@ | |
| 308 | 327 | { |
| 309 | 328 | public String filename; |
| 310 | 329 | public boolean jar, interactive, notice; |
| 330 | public boolean runModule; | |
| 311 | 331 | public boolean fixInteractive; |
| 312 | 332 | public boolean help, version; |
| 313 | 333 | public String[] argv; |
| ... | ...@@ -316,11 +336,13 @@ | |
| 316 | 336 | public java.util.Vector warnoptions = new java.util.Vector(); |
| 317 | 337 | public String encoding; |
| 318 | 338 | public String division; |
| 339 | public String moduleName; | |
| 319 | 340 | |
| 320 | 341 | public CommandLineOptions() { |
| 321 | 342 | filename = null; |
| 322 | 343 | jar = fixInteractive = false; |
| 323 | 344 | interactive = notice = true; |
| 345 | runModule = false; | |
| 324 | 346 | properties = new java.util.Properties(); |
| 325 | 347 | help = version = false; |
| 326 | 348 | } |
| ... | ...@@ -412,6 +434,20 @@ | |
| 412 | 434 | else |
| 413 | 435 | division = args[++index]; |
| 414 | 436 | } |
| 437 | else if (arg.startsWith("-m")) { | |
| 438 | runModule = true; | |
| 439 | if ((index + 1) < args.length) { | |
| 440 | moduleName = args[++index]; | |
| 441 | } else { | |
| 442 | System.err.println("Argument expected for the -m option"); | |
| 443 | System.err.print(jython.usageHeader); | |
| 444 | System.err.println("Try `jython -h' for more information."); | |
| 445 | return false; | |
| 446 | } | |
| 447 | if (!fixInteractive) { | |
| 448 | interactive = false; | |
| 449 | } | |
| 450 | } | |
| 415 | 451 | else { |
| 416 | 452 | String opt = args[index]; |
| 417 | 453 | if (opt.startsWith("--")) |