Repository: zeppelin Updated Branches: refs/heads/branch-0.6 eebca5928 -> ad4e9e054
[ZEPPELIN-1287][branch-0.6] No need to call print to display output in PythonInterpreter ### What is this PR for? Implement #1278 to merge branch-0.6 ### What type of PR is it? Bug Fix ### What is the Jira issue? [ZEPPELIN-1287](https://issues.apache.org/jira/browse/ZEPPELIN-1287) ### Questions: * Does the licenses files need update? no * Is there breaking changes for older versions? no * Does this needs documentation? no Author: Mina Lee <[email protected]> Closes #1320 from minahlee/branch-0.6_ZEPPELIN-1287 and squashes the following commits: f99f8aa [Mina Lee] return result directly ac83b14 [Mina Lee] No need to call print to display output in PythonInterpreter Project: http://git-wip-us.apache.org/repos/asf/zeppelin/repo Commit: http://git-wip-us.apache.org/repos/asf/zeppelin/commit/ad4e9e05 Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/ad4e9e05 Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/ad4e9e05 Branch: refs/heads/branch-0.6 Commit: ad4e9e05461b2f203cf4fc73524216e0ee4d1c17 Parents: eebca59 Author: Mina Lee <[email protected]> Authored: Fri Aug 12 05:01:45 2016 +0200 Committer: Mina Lee <[email protected]> Committed: Fri Aug 12 09:25:30 2016 +0200 ---------------------------------------------------------------------- .../zeppelin/python/PythonInterpreter.java | 6 ++-- .../apache/zeppelin/python/PythonProcess.java | 33 +++++++++----------- python/src/main/resources/bootstrap.py | 5 ++- .../zeppelin/python/PythonInterpreterTest.java | 8 ++--- 4 files changed, 25 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zeppelin/blob/ad4e9e05/python/src/main/java/org/apache/zeppelin/python/PythonInterpreter.java ---------------------------------------------------------------------- diff --git a/python/src/main/java/org/apache/zeppelin/python/PythonInterpreter.java b/python/src/main/java/org/apache/zeppelin/python/PythonInterpreter.java index 1a0469a..10d12db 100644 --- a/python/src/main/java/org/apache/zeppelin/python/PythonInterpreter.java +++ b/python/src/main/java/org/apache/zeppelin/python/PythonInterpreter.java @@ -138,8 +138,10 @@ public class PythonInterpreter extends Interpreter { return new InterpreterResult(Code.SUCCESS, ""); } String output = sendCommandToPython(cmd); - return new InterpreterResult(Code.SUCCESS, output.replaceAll(">>>", "") - .replaceAll("\\.\\.\\.", "").trim()); + + // TODO(zjffdu), we should not do string replacement operation in the result, as it is + // possible that the output contains the kind of pattern itself, e.g. print("...") + return new InterpreterResult(Code.SUCCESS, output.replaceAll("\\.\\.\\.", "")); } @Override http://git-wip-us.apache.org/repos/asf/zeppelin/blob/ad4e9e05/python/src/main/java/org/apache/zeppelin/python/PythonProcess.java ---------------------------------------------------------------------- diff --git a/python/src/main/java/org/apache/zeppelin/python/PythonProcess.java b/python/src/main/java/org/apache/zeppelin/python/PythonProcess.java index a671224..8eaf5e7 100644 --- a/python/src/main/java/org/apache/zeppelin/python/PythonProcess.java +++ b/python/src/main/java/org/apache/zeppelin/python/PythonProcess.java @@ -21,12 +21,11 @@ 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.PrintWriter; +import java.io.OutputStream; import java.lang.reflect.Field; /** @@ -35,11 +34,11 @@ import java.lang.reflect.Field; */ 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; private String binPath; @@ -56,7 +55,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(); @@ -92,25 +91,23 @@ 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("Readed 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(); } http://git-wip-us.apache.org/repos/asf/zeppelin/blob/ad4e9e05/python/src/main/resources/bootstrap.py ---------------------------------------------------------------------- diff --git a/python/src/main/resources/bootstrap.py b/python/src/main/resources/bootstrap.py index e225f03..f08420a 100644 --- a/python/src/main/resources/bootstrap.py +++ b/python/src/main/resources/bootstrap.py @@ -25,14 +25,13 @@ 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() signal.signal(signal.SIGINT, intHandler) - +# set prompt as empty string so that java side don't need to remove the prompt. +sys.ps1="" def help(): print ('%html') http://git-wip-us.apache.org/repos/asf/zeppelin/blob/ad4e9e05/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterTest.java ---------------------------------------------------------------------- diff --git a/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterTest.java b/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterTest.java index 35f4e2b..b33d238 100644 --- a/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterTest.java +++ b/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterTest.java @@ -132,7 +132,7 @@ public class PythonInterpreterTest { */ try { - when(mockPythonProcess.sendAndGetResult(eq("\n\nimport py4j\n"))).thenReturn(">>>"); + when(mockPythonProcess.sendAndGetResult(eq("\n\nimport py4j\n"))).thenReturn(""); } catch (IOException e) { e.printStackTrace(); } @@ -157,7 +157,7 @@ public class PythonInterpreterTest { public void testClose() { try { - when(mockPythonProcess.sendAndGetResult(eq("\n\nimport py4j\n"))).thenReturn(">>>"); + when(mockPythonProcess.sendAndGetResult(eq("\n\nimport py4j\n"))).thenReturn(""); } catch (IOException e) { e.printStackTrace(); } @@ -222,11 +222,11 @@ public class PythonInterpreterTest { String output = ""; for (int i = 0; i < lines.length; i++) { - output += ">>>" + lines[i]; + output += lines[i]; } return output; } else { - return ">>>"; + return ""; } }
