Repository: zeppelin
Updated Branches:
  refs/heads/master 873489036 -> 3dec4d700


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

### What is this PR for?
It is not necessary to call print to display output in PythonInterpreter. 2 
main changes:
* the root cause is the displayhook in bootstrap.py
* also did some code refactoring on PythonInterpreter

### What type of PR is it?
[Bug Fix]

### Todos
* [ ] - Task

### What is the Jira issue?
* https://issues.apache.org/jira/browse/ZEPPELIN-1287

### How should this be tested?
Verify it manually

### Screenshots (if appropriate)
![2016-08-04_1404](https://cloud.githubusercontent.com/assets/164491/17392006/090279d2-5a4d-11e6-840b-4cddb595a42e.png)

### Questions:
* Does the licenses files need update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No

Author: Jeff Zhang <[email protected]>

Closes #1278 from zjffdu/ZEPPELIN-1287 and squashes the following commits:

b48b56f [Jeff Zhang] fix unit test fail
3e9f169 [Jeff Zhang] address comments
0eade71 [Jeff Zhang] ZEPPELIN-1287. 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/3dec4d70
Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/3dec4d70
Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/3dec4d70

Branch: refs/heads/master
Commit: 3dec4d7006b8a57136f34ae330ba937d8990f2d2
Parents: 8734890
Author: Jeff Zhang <[email protected]>
Authored: Mon Aug 8 08:40:46 2016 +0800
Committer: Mina Lee <[email protected]>
Committed: Thu Aug 11 10:39:22 2016 +0200

----------------------------------------------------------------------
 .../zeppelin/python/PythonInterpreter.java      |  8 +++--
 .../apache/zeppelin/python/PythonProcess.java   | 32 +++++++++-----------
 python/src/main/resources/bootstrap.py          |  4 +--
 .../zeppelin/python/PythonInterpreterTest.java  |  8 ++---
 4 files changed, 26 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zeppelin/blob/3dec4d70/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 670dffc..877d697 100644
--- a/python/src/main/java/org/apache/zeppelin/python/PythonInterpreter.java
+++ b/python/src/main/java/org/apache/zeppelin/python/PythonInterpreter.java
@@ -134,10 +134,11 @@ public class PythonInterpreter extends Interpreter {
 
     InterpreterResult result;
     if (pythonErrorIn(output)) {
-      result = new InterpreterResult(Code.ERROR, output.replaceAll(">>>", 
"").trim());
+      result = new InterpreterResult(Code.ERROR, output);
     } else {
-      result = 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("...")
+      result = new InterpreterResult(Code.SUCCESS, 
output.replaceAll("\\.\\.\\.", ""));
     }
     return result;
   }
@@ -265,4 +266,5 @@ public class PythonInterpreter extends Interpreter {
   public int getMaxResult() {
     return maxResult;
   }
+  
 }

http://git-wip-us.apache.org/repos/asf/zeppelin/blob/3dec4d70/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 348ced6..0ab1461 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;
 
 /**
@@ -34,11 +33,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 +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();
@@ -85,22 +84,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 {

http://git-wip-us.apache.org/repos/asf/zeppelin/blob/3dec4d70/python/src/main/resources/bootstrap.py
----------------------------------------------------------------------
diff --git a/python/src/main/resources/bootstrap.py 
b/python/src/main/resources/bootstrap.py
index 09e51e3..889b456 100644
--- a/python/src/main/resources/bootstrap.py
+++ b/python/src/main/resources/bootstrap.py
@@ -25,13 +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/3dec4d70/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 a4c80ae..8866e6c 100644
--- a/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterTest.java
+++ b/python/src/test/java/org/apache/zeppelin/python/PythonInterpreterTest.java
@@ -115,7 +115,7 @@ public class PythonInterpreterTest {
    */
   @Test
   public void testPy4jInstalled() throws IOException, InterruptedException {
-    when(mockPythonProcess.sendAndGetResult(eq("\n\nimport 
py4j\n"))).thenReturn(">>>");
+    when(mockPythonProcess.sendAndGetResult(eq("\n\nimport 
py4j\n"))).thenReturn("");
 
     pythonInterpreter.open();
     Integer py4jPort = pythonInterpreter.getPy4jPort();
@@ -137,7 +137,7 @@ public class PythonInterpreterTest {
   @Test
   public void testClose() throws IOException, InterruptedException {
     //given: py4j is installed
-    when(mockPythonProcess.sendAndGetResult(eq("\n\nimport 
py4j\n"))).thenReturn(">>>");
+    when(mockPythonProcess.sendAndGetResult(eq("\n\nimport 
py4j\n"))).thenReturn("");
 
     pythonInterpreter.open();
     Integer py4jPort = pythonInterpreter.getPy4jPort();
@@ -210,11 +210,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 "";
     }
   }
 

Reply via email to