ZEPPELIN-1287. No need to call print to display output in PythonInterpreter

This commit is contained in:
Jeff Zhang 2016-08-04 14:05:22 +08:00
parent b007924959
commit 0eade713f1
3 changed files with 17 additions and 25 deletions

View file

@ -134,9 +134,9 @@ public class PythonInterpreter extends Interpreter {
InterpreterResult result;
if (pythonErrorIn(output)) {
result = new InterpreterResult(Code.ERROR, output.replaceAll(">>>", "").trim());
result = new InterpreterResult(Code.ERROR, output.replaceAll(">>> ", "").trim());
} else {
result = new InterpreterResult(Code.SUCCESS, output.replaceAll(">>>", "")
result = new InterpreterResult(Code.SUCCESS, output.replaceAll(">>> ", "")
.replaceAll("\\.\\.\\.", "").trim());
}
return result;
@ -265,4 +265,5 @@ public class PythonInterpreter extends Interpreter {
public int getMaxResult() {
return maxResult;
}
}

View file

@ -20,13 +20,7 @@ package org.apache.zeppelin.python;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.*;
import java.lang.reflect.Field;
/**
@ -34,11 +28,11 @@ import java.lang.reflect.Field;
* Python process (REPL) used by python interpreter
*/
public class PythonProcess {
Logger logger = LoggerFactory.getLogger(PythonProcess.class);
private static final Logger logger = LoggerFactory.getLogger(PythonProcess.class);
private static final String STATEMENT_END = "*!?flush reader!?*";
InputStream stdout;
OutputStream stdin;
BufferedWriter writer;
PrintWriter writer;
BufferedReader reader;
Process process;
@ -56,7 +50,7 @@ public class PythonProcess {
process = builder.start();
stdout = process.getInputStream();
stdin = process.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(stdin));
writer = new PrintWriter(stdin, true);
reader = new BufferedReader(new InputStreamReader(stdout));
try {
pid = findPid();
@ -85,22 +79,21 @@ public class PythonProcess {
}
public String sendAndGetResult(String cmd) throws IOException {
writer.write(cmd + "\n\n");
writer.write("print (\"*!?flush reader!?*\")\n\n");
writer.flush();
String output = "";
String line;
while (!(line = reader.readLine()).contains("*!?flush reader!?*")) {
writer.println(cmd);
writer.println();
writer.println("\"" + STATEMENT_END + "\"");
StringBuilder output = new StringBuilder();
String line = null;
while (!(line = reader.readLine()).contains(STATEMENT_END)) {
logger.debug("Read line from python shell : " + line);
if (line.equals("...")) {
logger.warn("Syntax error ! ");
output += "Syntax error ! ";
output.append("Syntax error ! ");
break;
}
output += "\r" + line + "\n";
output.append(line + "\n");
}
return output;
return output.toString();
}
private long findPid() throws NoSuchFieldException, IllegalAccessException {

View file

@ -25,8 +25,6 @@ try:
except ImportError:
import io as io
sys.displayhook = lambda x: None
def intHandler(signum, frame): # Set the signal handler
print ("Paragraph interrupted")
raise KeyboardInterrupt()