@@ -59,8 +59,6 @@ public static void main(String[] args) {
59
59
60
60
private static final String LANGUAGE_ID = "python" ;
61
61
private static final Source QUIT_EOF = Source .newBuilder (LANGUAGE_ID , "import site\n exit()" , "<exit-on-eof>" ).internal (true ).buildLiteral ();
62
- private static final Source GET_PROMPT = Source .newBuilder (LANGUAGE_ID , "import sys\n sys.ps1" , "<prompt>" ).internal (true ).buildLiteral ();
63
- private static final Source GET_CONTINUE_PROMPT = Source .newBuilder (LANGUAGE_ID , "import sys\n sys.ps2" , "<continue-prompt>" ).internal (true ).buildLiteral ();
64
62
65
63
private ArrayList <String > programArgs = null ;
66
64
private String commandString = null ;
@@ -519,10 +517,12 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
519
517
int lastStatus = 0 ;
520
518
try {
521
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" ));
522
522
523
523
while (true ) { // processing inputs
524
524
boolean doEcho = doEcho (context );
525
- consoleHandler .setPrompt (doEcho ? getPrompt ( context ) : null );
525
+ consoleHandler .setPrompt (doEcho ? sys . getMember ( "ps1" ). asString ( ) : null );
526
526
527
527
try {
528
528
String input = consoleHandler .readLine ();
@@ -542,7 +542,7 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
542
542
context .eval (Source .newBuilder (getLanguageId (), sb .toString (), "<stdin>" ).interactive (true ).buildLiteral ());
543
543
} catch (PolyglotException e ) {
544
544
if (continuePrompt == null ) {
545
- continuePrompt = doEcho ? getContinuePrompt ( context ) : null ;
545
+ continuePrompt = doEcho ? sys . getMember ( "ps2" ). asString ( ) : null ;
546
546
}
547
547
if (e .isIncompleteSource ()) {
548
548
// read another line of input
@@ -597,7 +597,7 @@ private void setupReadline(Context context, ConsoleHandler consoleHandler) {
597
597
context .eval (Source .newBuilder (getLanguageId (), "None" , "setup-interactive" ).interactive (true ).buildLiteral ());
598
598
// Then we can get the readline module and see if any completers were registered and use its
599
599
// history feature
600
- final Value readline = context .eval (Source .newBuilder (getLanguageId (), "import readline; readline" , "setup-interactive" ). buildLiteral ( ));
600
+ final Value readline = context .eval (Source .create (getLanguageId (), "import readline; readline" ));
601
601
final Value completer = readline .getMember ("get_completer" ).execute ();
602
602
final Value shouldRecord = readline .getMember ("get_auto_history" );
603
603
final Value addHistory = readline .getMember ("add_history" );
@@ -640,26 +640,4 @@ private static final class ExitException extends RuntimeException {
640
640
private static boolean doEcho (@ SuppressWarnings ("unused" ) Context context ) {
641
641
return true ;
642
642
}
643
-
644
- private static String getPrompt (Context context ) {
645
- try {
646
- return context .eval (GET_PROMPT ).asString ();
647
- } catch (PolyglotException e ) {
648
- if (e .isExit ()) {
649
- throw new ExitException (e .getExitStatus ());
650
- }
651
- throw new RuntimeException ("error while retrieving prompt" , e );
652
- }
653
- }
654
-
655
- private static String getContinuePrompt (Context context ) {
656
- try {
657
- return context .eval (GET_CONTINUE_PROMPT ).asString ();
658
- } catch (PolyglotException e ) {
659
- if (e .isExit ()) {
660
- throw new ExitException (e .getExitStatus ());
661
- }
662
- throw new RuntimeException ("error while retrieving continue prompt" , e );
663
- }
664
- }
665
643
}
0 commit comments