-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathjython.java
More file actions
697 lines (638 loc) · 27 KB
/
Copy pathjython.java
File metadata and controls
697 lines (638 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright (c) Corporation for National Research Initiatives
package org.python.util;
import org.python.Version;
import org.python.core.CodeFlag;
import org.python.core.CompileMode;
import org.python.core.Options;
import org.python.core.Py;
import org.python.core.PyCode;
import org.python.core.PyException;
import org.python.core.PyFile;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyStringMap;
import org.python.core.PySystemState;
import org.python.core.PyUnicode;
import org.python.core.imp;
import org.python.core.util.RelativeFile;
import org.python.modules._systemrestart;
import org.python.modules.posix.PosixModule;
import org.python.modules.thread._thread;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class jython {
// An instance of this class will provide the console (python.console) by default.
private static final String PYTHON_CONSOLE_CLASS = "org.python.util.JLineConsole";
private static final String COPYRIGHT =
"Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.";
static final String usageHeader =
"usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
private static final String usage = usageHeader
+ "Options and arguments:\n"
// + "(and corresponding environment variables):\n"
+ "-B : don't write bytecode files on import\n"
// + "also PYTHONDONTWRITEBYTECODE=x\n" +
+ "-c cmd : program passed in as string (terminates option list)\n"
// + "-d : debug output from parser (also PYTHONDEBUG=x)\n"
+ "-Dprop=v : Set the property `prop' to value `v'\n"
+ "-E : ignore environment variables (such as PYTHONPATH)\n"
+ "-h : print this help message and exit (also --help)\n"
+ "-i : inspect interactively after running script\n"
// + ", (also PYTHONINSPECT=x)\n"
+ " and force prompts, even if stdin does not appear to be a terminal\n"
+ "-jar jar : program read from __run__.py in jar file\n"
+ "-m mod : run library module as a script (terminates option list)\n"
// + "-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n"
// + "-OO : remove doc-strings in addition to the -O optimizations\n"
+ "-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n"
+ "-s : don't add user site directory to sys.path;\n"
// + "also PYTHONNOUSERSITE\n"
+ "-S : don't imply 'import site' on initialization\n"
// + "-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n"
+ "-u : unbuffered binary stdout and stderr\n"
// + "(also PYTHONUNBUFFERED=x)\n"
// + " see man page for details on internal buffering relating to '-u'\n"
+ "-v : verbose (trace import statements)\n"
// + "(also PYTHONVERBOSE=x)\n"
+ " can be supplied multiple times to increase verbosity\n"
+ "-V : print the Python version number and exit (also --version)\n"
+ "-W arg : warning control (arg is action:message:category:module:lineno)\n"
// + "-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n"
+ "-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n"
+ "file : program read from script file\n"
+ "- : program read from stdin (default; interactive mode if a tty)\n"
+ "arg ... : arguments passed to program in sys.argv[1:]\n" + "\n"
+ "Other environment variables:\n" //
+ "PYTHONPATH: '" + File.pathSeparator
+ "'-separated list of directories prefixed to the default module\n"
+ " search path. The result is sys.path.\n"
+ "PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.";
public static boolean shouldRestart;
/**
* Runs a JAR file, by executing the code found in the file __run__.py, which should be in the
* root of the JAR archive. Note that the __name__ is set to the base name of the JAR file and
* not to "__main__" (for historic reasons). This method do NOT handle Exceptions. the caller
* SHOULD handle any (Py)Exceptions thrown by the code.
*
* @param filename The path to the filename to run.
*/
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' should be called
// `__zipfile__' or (preferably, IMO), __run__.py should be imported and a main()
// function extracted. This function should be called passing zipfile in as an
// argument.
//
// Probably have to keep this code around for backwards compatibility (?)
try {
ZipFile zip = new ZipFile(filename);
ZipEntry runit = zip.getEntry("__run__.py");
if (runit == null) {
throw Py.ValueError("jar file missing '__run__.py'");
}
PyStringMap locals = Py.newStringMap();
// Stripping the stuff before the last File.separator fixes Bug #931129 by
// keeping illegal characters out of the generated proxy class name
int beginIndex;
if ((beginIndex = filename.lastIndexOf(File.separator)) != -1) {
filename = filename.substring(beginIndex + 1);
}
locals.__setitem__("__name__", new PyUnicode(filename));
locals.__setitem__("zipfile", Py.java2py(zip));
InputStream file = zip.getInputStream(runit);
PyCode code;
try {
code = Py.compile(file, "__run__", CompileMode.exec);
} finally {
file.close();
}
Py.runCode(code, locals, locals);
} catch (IOException e) {
throw Py.IOError(e);
}
}
public static void main(String[] args) {
do {
shouldRestart = false;
run(args);
} while (shouldRestart);
}
private static List<String> warnOptionsFromEnv() {
ArrayList<String> opts = new ArrayList<String>();
try {
String envVar = System.getenv("PYTHONWARNINGS");
if (envVar == null) {
return opts;
}
for (String opt : envVar.split(",")) {
opt = opt.trim();
if (opt.length() == 0) {
continue;
}
opts.add(opt);
}
} catch (SecurityException e) {
// Continue
}
return opts;
}
private static List<String> validWarnActions = Arrays.asList("error", "ignore", "always",
"default", "module", "once");
private static void addWarnings(List<String> from, PyList to) {
outerLoop : for (String opt : from) {
String action = opt.split(":")[0];
for (String validWarnAction : validWarnActions) {
if (validWarnAction.startsWith(action)) {
if (opt.contains(":")) {
to.append(Py.newString(validWarnAction + opt.substring(opt.indexOf(":"))));
} else {
to.append(Py.newString(validWarnAction));
}
continue outerLoop;
}
}
System.err.println(String.format("Invalid -W option ignored: invalid action: '%s'",
action));
}
}
private static boolean runModule(InteractiveConsole interp, String moduleName) {
return runModule(interp, moduleName, true);
}
private static boolean runModule(InteractiveConsole interp, String moduleName, boolean set_argv0) {
// PEP 338 - Execute module as a script
PyObject runpy = imp.importName("runpy", true);
PyObject runmodule = runpy.__findattr__("_run_module_as_main");
runmodule.__call__(Py.newUnicode(moduleName), Py.newBoolean(set_argv0));
return true;
}
private static boolean runMainFromImporter(InteractiveConsole interp, String filename) {
// Support http://bugs.python.org/issue1739468 - Allow interpreter to execute a zip file or directory
PyUnicode argv0 = Py.newUnicode(filename);
Py.getSystemState().path.insert(0, argv0);
try {
return runModule(interp, "__main__", false);
} catch (Throwable t) {
return false;
}
}
public static void run(String[] args) {
// Parse the command line options
CommandLineOptions opts = new CommandLineOptions();
if (!opts.parse(args)) {
if (opts.version) {
System.err.println("Jython " + Version.PY_VERSION);
System.exit(0);
}
if (opts.help) {
System.err.println(usage);
} else if (!opts.runCommand && !opts.runModule) {
System.err.print(usageHeader);
System.err.println("Try `jython -h' for more information.");
}
int exitcode = opts.help ? 0 : -1;
System.exit(exitcode);
}
// Get system properties (or empty set if we're prevented from accessing them)
Properties preProperties = PySystemState.getBaseProperties();
// Read environment variable PYTHONIOENCODING into properties (registry)
String pythonIoEncoding = getenv("PYTHONIOENCODING");
if (pythonIoEncoding != null) {
String[] spec = splitString(pythonIoEncoding, ':', 2);
// Note that if encoding or errors is blank (=null), the registry value wins.
addDefault(preProperties, PySystemState.PYTHON_IO_ENCODING, spec[0]);
addDefault(preProperties, PySystemState.PYTHON_IO_ERRORS, spec[1]);
}
// Decide if System.in is interactive
if (!opts.fixInteractive || opts.interactive) {
// The options suggest System.in is interactive: but only if isatty() agrees
opts.interactive = Py.isInteractive();
if (opts.interactive) {
// Set the default console type if nothing else has
addDefault(preProperties, "python.console", PYTHON_CONSOLE_CLASS);
}
}
// Setup the basic python system state from these options
PySystemState.initialize(preProperties, opts.properties, opts.argv);
PySystemState systemState = Py.getSystemState();
PyList warnoptions = new PyList();
addWarnings(opts.warnoptions, warnoptions);
if (!Options.ignore_environment) {
addWarnings(warnOptionsFromEnv(), warnoptions);
}
systemState.setWarnoptions(warnoptions);
// Make sure warnings module is loaded if there are warning options
// Not sure this is needed, but test_warnings.py expects this
if (warnoptions.size() > 0) {
imp.load("warnings");
}
// Now create an interpreter
if (!opts.interactive) {
// Not (really) interactive, so do not use console prompts
systemState.ps1 = systemState.ps2 = Py.EmptyUnicode;
}
InteractiveConsole interp = new InteractiveConsole();
// Print banner and copyright information (or not)
if (opts.interactive && opts.notice && !opts.runModule) {
System.err.println(InteractiveConsole.getDefaultBanner());
}
if (Py.importSiteIfSelected()) {
if (opts.interactive && opts.notice && !opts.runModule) {
System.err.println(COPYRIGHT);
}
}
if (opts.division != null) {
if ("old".equals(opts.division)) {
Options.division_warning = 0;
} else if ("warn".equals(opts.division)) {
Options.division_warning = 1;
} else if ("warnall".equals(opts.division)) {
Options.division_warning = 2;
} else if ("new".equals(opts.division)) {
Options.Qnew = true;
// interp.cflags.setFlag(CodeFlag.CO_FUTURE_DIVISION);
}
}
// was there a filename on the command line?
if (opts.filename != null) {
if (runMainFromImporter(interp, opts.filename)) {
interp.cleanup();
return;
}
String path;
try {
path = new File(opts.filename).getCanonicalFile().getParent();
} catch (IOException ioe) {
path = new File(opts.filename).getAbsoluteFile().getParent();
}
if (path == null) {
path = "";
}
Py.getSystemState().path.insert(0, Py.newUnicode(path));
if (opts.jar) {
try {
runJar(opts.filename);
} catch (Throwable t) {
Py.printException(t);
System.exit(-1);
}
} else if (opts.filename.equals("-")) {
try {
interp.globals.__setitem__(new PyUnicode("__file__"), new PyUnicode("<stdin>"));
interp.execfile(System.in, "<stdin>");
} catch (Throwable t) {
Py.printException(t);
}
} else {
try {
interp.globals.__setitem__(new PyUnicode("__file__"),
new PyUnicode(opts.filename));
FileInputStream file;
try {
file = new FileInputStream(new RelativeFile(opts.filename));
} catch (FileNotFoundException e) {
throw Py.IOError(e);
}
try {
boolean isInteractive = false;
try {
isInteractive = PosixModule.getPOSIX().isatty(file.getFD());
} catch (SecurityException ex) {}
if (isInteractive) {
opts.interactive = true;
interp.interact(null, new PyFile(file));
return;
} else {
interp.execfile(file, opts.filename);
}
} finally {
file.close();
}
} catch (Throwable t) {
if (t instanceof PyException
&& ((PyException)t).match(_systemrestart.SystemRestart)) {
// Shutdown this instance...
shouldRestart = true;
shutdownInterpreter();
interp.cleanup();
// ..reset the state...
Py.setSystemState(new PySystemState());
// ...and start again
return;
} else {
Py.printException(t);
interp.cleanup();
System.exit(-1);
}
}
}
} else {
// if there was no file name on the command line, then "" is the first element
// on sys.path. This is here because if there /was/ a filename on the c.l.,
// and say the -i option was given, sys.path[0] will have gotten filled in
// with the dir of the argument filename.
Py.getSystemState().path.insert(0, Py.EmptyUnicode);
if (opts.command != null) {
try {
interp.exec(opts.command);
} catch (Throwable t) {
Py.printException(t);
System.exit(1);
}
}
if (opts.moduleName != null) {
try {
runModule(interp, opts.moduleName);
} catch (Throwable t) {
Py.printException(t);
System.exit(1);
} finally {
interp.cleanup();
}
return;
}
}
if (opts.fixInteractive || (opts.filename == null && opts.command == null)) {
// Go interactive with the console: the parser needs to know the encoding.
String encoding = Py.getConsole().getEncoding();
// Run the interpreter interactively
try {
interp.cflags.encoding = encoding;
if (!opts.interactive) {
// Don't print prompts. http://bugs.jython.org/issue2325
interp._interact(null, null);
}
else {
interp.interact(null, null);
}
} catch (Throwable t) {
Py.printException(t);
}
}
interp.cleanup();
}
/**
* Run any finalizations on the current interpreter in preparation for a SytemRestart.
*/
public static void shutdownInterpreter() {
// Stop all the active threads and signal the SystemRestart
_thread.interruptAllThreads();
Py.getSystemState()._systemRestart = true;
// Close all sockets -- not all of their operations are stopped by
// Thread.interrupt (in particular pre-nio sockets)
try {
imp.load("socket").__findattr__("_closeActiveSockets").__call__();
} catch (PyException pye) {
// continue
}
}
/**
* Return an array of trimmed strings by splitting the argument at each occurrence of a
* separator character. (Helper for configuration variable processing.) Segments of zero length
* after trimming emerge as <code>null</code>. If there are more than the specified number of
* segments the last element of the array contains all of the source string after the
* <code>(n-1)</code>th occurrence of <code>sep</code>.
*
* @param spec to split
* @param sep character on which to split
* @param n number of parts to split into
* @return <code>n</code>-element array of strings (or <code>null</code>s)
*/
private static String[] splitString(String spec, char sep, int n) {
String[] list = new String[n];
int p = 0, i = 0, L = spec.length();
while (p < L) {
int c = spec.indexOf(sep, p);
if (c < 0 || i >= n - 1) {
// No more seps, or no more space: i.th piece is the rest of spec.
c = L;
}
String s = spec.substring(p, c).trim();
list[i++] = (s.length() > 0) ? s : null;
p = c + 1;
}
return list;
}
/**
* If the key is not currently present and the passed value is not <code>null</code>, sets the
* <code>key</code> to the <code>value</code> in the given <code>Properties</code> object. Thus,
* it provides a default value for a subsequent <code>getProperty()</code>.
*
* @param registry to be (possibly) updated
* @param key at which to set value
* @param value to set (or <code>null</code> for no setting)
* @return true iff a value was set
*/
private static boolean addDefault(Properties registry, String key, String value) {
// Set value at key if nothing else has set it
if (value == null || registry.containsKey(key)) {
return false;
} else {
registry.setProperty(key, value);
return true;
}
}
/**
* Get the value of an environment variable, if we are allowed to and it exists; otherwise
* return <code>null</code>. We are allowed to access the environment variable if the -E flag
* was not given and the application has permission to read environment variables. The -E flag
* is reflected in {@link Options#ignore_environment}, and will be set automatically if it turns
* out we do not have permission.
*
* @param varname name to access in the environment
* @return the value or <code>null</code>.
*/
private static String getenv(String varname) {
if (!Options.ignore_environment) {
try {
return System.getenv(varname);
} catch (SecurityException e) {
// We're not allowed to access them after all
Options.ignore_environment = true;
}
}
return null;
}
}
class CommandLineOptions {
public String filename;
public boolean jar, interactive, notice;
public boolean runCommand, runModule;
public boolean fixInteractive;
public boolean help, version;
public String[] argv;
public Properties properties;
public String command;
public List<String> warnoptions = Generic.list();
public String encoding;
public String division;
public String moduleName;
public CommandLineOptions() {
filename = null;
jar = fixInteractive = false;
interactive = notice = true;
runModule = false;
properties = new Properties();
help = version = false;
}
public void setProperty(String key, String value) {
properties.put(key, value);
try {
System.setProperty(key, value);
} catch (SecurityException e) {
// continue
}
}
private boolean argumentExpected(String arg) {
System.err.println("Argument expected for the " + arg + " option");
return false;
}
public boolean parse(String[] args) {
int index = 0;
while (index < args.length && args[index].startsWith("-")) {
String arg = args[index];
if (arg.equals("-h") || arg.equals("-?") || arg.equals("--help")) {
help = true;
return false;
} else if (arg.equals("-V") || arg.equals("--version")) {
version = true;
return false;
} else if (arg.equals("-")) {
if (!fixInteractive) {
interactive = false;
}
filename = "-";
} else if (arg.equals("-i")) {
fixInteractive = true;
interactive = true;
} else if (arg.equals("-jar")) {
jar = true;
if (!fixInteractive) {
interactive = false;
}
} else if (arg.equals("-u")) {
Options.unbuffered = true;
} else if (arg.equals("-v")) {
Options.verbose++;
} else if (arg.equals("-vv")) {
Options.verbose += 2;
} else if (arg.equals("-vvv")) {
Options.verbose += 3;
} else if (arg.equals("-s")) {
Options.no_user_site = true;
} else if (arg.equals("-S")) {
Options.importSite = false;
} else if (arg.equals("-B")) {
Options.dont_write_bytecode = true;
} else if (arg.startsWith("-c")) {
runCommand = true;
if (arg.length() > 2) {
command = arg.substring(2);
} else if ((index + 1) < args.length) {
command = args[++index];
} else {
return argumentExpected(arg);
}
if (!fixInteractive) {
interactive = false;
}
index++;
break;
} else if (arg.startsWith("-W")) {
if (arg.length() > 2) {
warnoptions.add(arg.substring(2));
} else if ((index + 1) < args.length) {
warnoptions.add(args[++index]);
} else {
return argumentExpected(arg);
}
} else if (arg.equals("-E")) {
// -E (ignore environment variables)
Options.ignore_environment = true;
} else if (arg.startsWith("-D")) {
String key = null;
String value = null;
int equals = arg.indexOf("=");
if (equals == -1) {
String arg2 = args[++index];
key = arg.substring(2, arg.length());
value = arg2;
} else {
key = arg.substring(2, equals);
value = arg.substring(equals + 1, arg.length());
}
setProperty(key, value);
} else if (arg.startsWith("-Q")) {
if (arg.length() > 2) {
division = arg.substring(2);
} else {
division = args[++index];
}
} else if (arg.startsWith("-m")) {
runModule = true;
if (arg.length() > 2) {
moduleName = arg.substring(2);
} else if ((index + 1) < args.length) {
moduleName = args[++index];
} else {
return argumentExpected(arg);
}
if (!fixInteractive) {
interactive = false;
}
index++;
int n = args.length - index + 1;
argv = new String[n];
argv[0] = moduleName;
for (int i = 1; index < args.length; i++, index++) {
argv[i] = args[index];
}
return true;
} else if (arg.startsWith("-3")) {
Options.py3k_warning = true;
} else {
String opt = args[index];
if (opt.startsWith("--")) {
opt = opt.substring(2);
} else if (opt.startsWith("-")) {
opt = opt.substring(1);
}
System.err.println("Unknown option: " + opt);
return false;
}
index += 1;
}
notice = interactive;
if (filename == null && index < args.length && command == null) {
filename = args[index++];
if (!fixInteractive) {
interactive = false;
}
notice = false;
}
if (command != null) {
notice = false;
}
int n = args.length - index + 1;
argv = new String[n];
if (filename != null) {
argv[0] = filename;
} else if (command != null) {
argv[0] = "-c";
} else {
argv[0] = "";
}
for (int i = 1; i < n; i++, index++) {
argv[i] = args[index];
}
return true;
}
}