@@ -59,8 +59,6 @@ public static void main(String[] args) {
5959
6060 private static final String LANGUAGE_ID = "python" ;
6161 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 ();
6462
6563 private ArrayList <String > programArgs = null ;
6664 private String commandString = null ;
@@ -519,10 +517,12 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
519517 int lastStatus = 0 ;
520518 try {
521519 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" ));
522522
523523 while (true ) { // processing inputs
524524 boolean doEcho = doEcho (context );
525- consoleHandler .setPrompt (doEcho ? getPrompt ( context ) : null );
525+ consoleHandler .setPrompt (doEcho ? sys . getMember ( "ps1" ). asString ( ) : null );
526526
527527 try {
528528 String input = consoleHandler .readLine ();
@@ -542,7 +542,7 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
542542 context .eval (Source .newBuilder (getLanguageId (), sb .toString (), "<stdin>" ).interactive (true ).buildLiteral ());
543543 } catch (PolyglotException e ) {
544544 if (continuePrompt == null ) {
545- continuePrompt = doEcho ? getContinuePrompt ( context ) : null ;
545+ continuePrompt = doEcho ? sys . getMember ( "ps2" ). asString ( ) : null ;
546546 }
547547 if (e .isIncompleteSource ()) {
548548 // read another line of input
@@ -597,7 +597,7 @@ private void setupReadline(Context context, ConsoleHandler consoleHandler) {
597597 context .eval (Source .newBuilder (getLanguageId (), "None" , "setup-interactive" ).interactive (true ).buildLiteral ());
598598 // Then we can get the readline module and see if any completers were registered and use its
599599 // 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" ));
601601 final Value completer = readline .getMember ("get_completer" ).execute ();
602602 final Value shouldRecord = readline .getMember ("get_auto_history" );
603603 final Value addHistory = readline .getMember ("add_history" );
@@ -640,26 +640,4 @@ private static final class ExitException extends RuntimeException {
640640 private static boolean doEcho (@ SuppressWarnings ("unused" ) Context context ) {
641641 return true ;
642642 }
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- }
665643}
0 commit comments