@@ -58,13 +58,18 @@ public static void main(String[] args) {
58
58
}
59
59
60
60
private static final String LANGUAGE_ID = "python" ;
61
- private static final Source QUIT_EOF = Source .newBuilder (LANGUAGE_ID , "import site\n exit()" , "<exit-on-eof>" ).internal (true ).buildLiteral ();
62
61
63
62
private ArrayList <String > programArgs = null ;
64
63
private String commandString = null ;
65
64
private String inputFile = null ;
65
+ private String module = null ;
66
+ private boolean ignoreEnv = false ;
66
67
private boolean inspectFlag = false ;
67
68
private boolean verboseFlag = false ;
69
+ private boolean quietFlag = false ;
70
+ private boolean noUserSite = false ;
71
+ private boolean noSite = false ;
72
+ private boolean stdinIsInteractive = System .console () != null ;
68
73
private boolean runCC = false ;
69
74
private boolean runLD = false ;
70
75
private VersionAction versionAction = VersionAction .None ;
@@ -76,11 +81,49 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
76
81
for (int i = 0 ; i < arguments .size (); i ++) {
77
82
String arg = arguments .get (i );
78
83
switch (arg ) {
84
+ case "-B" :
85
+ System .out .println ("Warning: " + arg + " does nothing on GraalPython." );
86
+ break ;
87
+ case "-c" :
88
+ i += 1 ;
89
+ if (i < arguments .size ()) {
90
+ commandString = arguments .get (i );
91
+ } else {
92
+ print ("Argument expected for the -c option" );
93
+ printShortHelp ();
94
+ }
95
+ break ;
96
+ case "-E" :
97
+ ignoreEnv = true ;
98
+ break ;
99
+ case "-h" :
100
+ unrecognized .add ("--help" );
101
+ break ;
102
+ case "-i" :
103
+ inspectFlag = true ;
104
+ break ;
105
+ case "-m" :
106
+ i += 1 ;
107
+ if (i < arguments .size ()) {
108
+ module = arguments .get (i );
109
+ } else {
110
+ print ("Argument expected for the -m option" );
111
+ printShortHelp ();
112
+ }
113
+ break ;
79
114
case "-O" :
80
115
case "-OO" :
81
- case "-B" :
82
116
System .out .println ("Warning: " + arg + " does nothing on GraalPython." );
83
117
break ;
118
+ case "-q" :
119
+ quietFlag = true ;
120
+ break ;
121
+ case "-s" :
122
+ noUserSite = true ;
123
+ break ;
124
+ case "-S" :
125
+ noSite = true ;
126
+ break ;
84
127
case "-v" :
85
128
verboseFlag = true ;
86
129
break ;
@@ -99,21 +142,6 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
99
142
runLD = true ;
100
143
programArgs .addAll (arguments .subList (i + 1 , arguments .size ()));
101
144
return unrecognized ;
102
- case "-c" :
103
- i += 1 ;
104
- if (i < arguments .size ()) {
105
- commandString = arguments .get (i );
106
- } else {
107
- print ("Argument expected for the -c option" );
108
- printShortHelp ();
109
- }
110
- break ;
111
- case "-h" :
112
- unrecognized .add ("--help" );
113
- break ;
114
- case "-i" :
115
- inspectFlag = true ;
116
- break ;
117
145
default :
118
146
if (!arg .startsWith ("-" )) {
119
147
inputFile = arg ;
@@ -123,7 +151,8 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
123
151
unrecognized .add (arg );
124
152
}
125
153
}
126
- if (inputFile != null || commandString != null ) {
154
+
155
+ if (inputFile != null || commandString != null || module != null ) {
127
156
i += 1 ;
128
157
if (i < arguments .size ()) {
129
158
programArgs .addAll (arguments .subList (i , arguments .size ()));
@@ -159,28 +188,44 @@ protected void launch(Builder contextBuilder) {
159
188
return ;
160
189
}
161
190
191
+ if (!ignoreEnv ) {
192
+ String pythonpath = System .getenv ("PYTHONPATH" );
193
+ if (pythonpath != null ) {
194
+ contextBuilder .option ("python.PythonPath" , pythonpath );
195
+ }
196
+ inspectFlag = inspectFlag || System .getenv ("PYTHONINSPECT" ) != null ;
197
+ noUserSite = noUserSite || System .getenv ("PYTHONNOUSERSITE" ) != null ;
198
+ verboseFlag = verboseFlag || System .getenv ("PYTHONVERBOSE" ) != null ;
199
+ }
200
+
162
201
// setting this to make sure our TopLevelExceptionHandler calls the excepthook
163
202
// to print Python exceptions
164
203
contextBuilder .option ("python.AlwaysRunExcepthook" , "true" );
165
- if (inspectFlag ) {
166
- contextBuilder .option ("python.InspectFlag" , "true" );
167
- }
204
+ contextBuilder .option ("python.InspectFlag" , Boolean .toString (inspectFlag ));
205
+ contextBuilder .option ("python.VerboseFlag" , Boolean .toString (verboseFlag ));
168
206
if (verboseFlag ) {
169
- contextBuilder .option ("python.VerboseFlag" , "true" );
170
207
contextBuilder .option ("log.python.level" , "FINE" );
171
208
}
172
-
173
- String pythonpath = System .getenv ("PYTHONPATH" );
174
- if (pythonpath != null ) {
175
- contextBuilder .option ("python.PythonPath" , pythonpath );
176
- }
209
+ contextBuilder .option ("python.QuietFlag" , Boolean .toString (quietFlag ));
210
+ contextBuilder .option ("python.NoUserSiteFlag" , Boolean .toString (noUserSite ));
211
+ contextBuilder .option ("python.NoSiteFlag" , Boolean .toString (noSite ));
177
212
178
213
ConsoleHandler consoleHandler = createConsoleHandler (System .in , System .out );
179
214
contextBuilder .arguments (getLanguageId (), programArgs .toArray (new String [0 ])).in (consoleHandler .createInputStream ());
180
215
181
216
int rc = 1 ;
182
217
try (Context context = contextBuilder .build ()) {
183
218
runVersionAction (versionAction , context .getEngine ());
219
+
220
+ if (!quietFlag && (verboseFlag || (commandString == null && inputFile == null && module == null && stdinIsInteractive ))) {
221
+ print ("Python " + evalInternal (context , "import sys; sys.version + ' on ' + sys.platform" ).asString ());
222
+ if (!noSite ) {
223
+ print ("Type \" help\" , \" copyright\" , \" credits\" or \" license\" for more information." );
224
+ }
225
+ }
226
+ if (!noSite ) {
227
+ evalInternal (context , "import site\n " );
228
+ }
184
229
System .err .println ("Please note: This Python implementation is in the very early stages, " +
185
230
"and can run little more than basic benchmarks at this point." );
186
231
consoleHandler .setContext (context );
@@ -429,7 +474,7 @@ protected void printHelp(OptionCategory maxCategory) {
429
474
"-h : print this help message and exit (also --help)\n " +
430
475
"-i : inspect interactively after running script; forces a prompt even\n " +
431
476
" if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n " +
432
- // "-m mod : run library module as a script (terminates option list)\n" +
477
+ "-m mod : run library module as a script (terminates option list)\n " +
433
478
"-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n " +
434
479
"-OO : remove doc-strings in addition to the -O optimizations\n " +
435
480
// "-R : use a pseudo-random salt to make hash() values of various types
@@ -438,16 +483,17 @@ protected void printHelp(OptionCategory maxCategory) {
438
483
// " a defense against denial-of-service attacks\n" +
439
484
// "-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n"
440
485
// +
441
- // "-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n"
442
- // +
443
- // "-S : don't imply 'import site' on initialization\n" +
486
+ "-q : don't print version and copyright messages on interactive startup" +
487
+ "-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE \n " +
488
+ "-S : don't imply 'import site' on initialization\n " +
444
489
// "-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n"
445
490
// +
446
491
// "-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n" +
447
492
// " see man page for details on internal buffering relating to '-u'\n" +
448
- // "-v : verbose (trace import statements); also PYTHONVERBOSE=x\n" +
449
- // " can be supplied multiple times to increase verbosity\n" +
493
+ "-v : verbose (trace import statements); also PYTHONVERBOSE=x\n " +
494
+ " can be supplied multiple times to increase verbosity\n " +
450
495
"-V : print the Python version number and exit (also --version)\n " +
496
+ " when given twice, print more information about the build" +
451
497
// "-W arg : warning control; arg is
452
498
// action:message:category:module:lineno\n" +
453
499
// " also PYTHONWARNINGS=arg\n" +
@@ -482,7 +528,7 @@ protected void printHelp(OptionCategory maxCategory) {
482
528
483
529
@ Override
484
530
protected String [] getDefaultLanguages () {
485
- return new String []{getLanguageId (), "llvm" };
531
+ return new String []{getLanguageId (), "llvm" , "regex" };
486
532
}
487
533
488
534
@ Override
@@ -516,9 +562,8 @@ public ConsoleHandler createConsoleHandler(InputStream inStream, OutputStream ou
516
562
public int readEvalPrint (Context context , ConsoleHandler consoleHandler ) {
517
563
int lastStatus = 0 ;
518
564
try {
519
- setupReadline (context , consoleHandler );
520
- Value sys = context .eval (Source .create (getLanguageId (), "import sys; sys" ));
521
- context .eval (Source .create (getLanguageId (), "del sys\n del site\n del readline" ));
565
+ setupREPL (context , consoleHandler );
566
+ Value sys = evalInternal (context , "import sys; sys" );
522
567
523
568
while (true ) { // processing inputs
524
569
boolean doEcho = doEcho (context );
@@ -570,7 +615,7 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
570
615
}
571
616
} catch (EOFException e ) {
572
617
try {
573
- context . eval ( QUIT_EOF );
618
+ evalInternal ( context , "import site; exit() \n " );
574
619
} catch (PolyglotException e2 ) {
575
620
if (e2 .isExit ()) {
576
621
// don't use the exit code from the PolyglotException
@@ -591,12 +636,15 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
591
636
}
592
637
}
593
638
594
- private void setupReadline (Context context , ConsoleHandler consoleHandler ) {
595
- // First run nothing to trigger the setup of interactive mode (site import and so on)
596
- context .eval (Source .newBuilder (getLanguageId (), "None" , "setup-interactive" ).interactive (true ).buildLiteral ());
639
+ private Value evalInternal (Context context , String code ) {
640
+ return context .eval (Source .newBuilder (getLanguageId (), code , "<internal>" ).internal (true ).buildLiteral ());
641
+ }
642
+
643
+ private void setupREPL (Context context , ConsoleHandler consoleHandler ) {
597
644
// Then we can get the readline module and see if any completers were registered and use its
598
645
// history feature
599
- final Value readline = context .eval (Source .create (getLanguageId (), "import readline; readline" ));
646
+ evalInternal (context , "import sys\n getattr(sys, '__interactivehook__', lambda: None)()\n " );
647
+ final Value readline = evalInternal (context , "import readline; readline" );
600
648
final Value completer = readline .getMember ("get_completer" ).execute ();
601
649
final Value shouldRecord = readline .getMember ("get_auto_history" );
602
650
final Value addHistory = readline .getMember ("add_history" );
0 commit comments