Browse Source

[livy] Fix executing '%' in pyspark

Erick Tryzelaar 10 years ago
parent
commit
0cd37d7

+ 9 - 21
apps/spark/java/livy-repl/src/main/resources/fake_shell.py

@@ -61,10 +61,6 @@ def execute_reply_internal_error(message, exc_info=None):
     })
 
 
-def ast_parse(code, filename='<stdin>', symbol='exec'):
-    return compile(code, filename, symbol, ast.PyCF_ONLY_AST, 1)
-
-
 class ExecutionError(Exception):
     def __init__(self, exc_info):
         self.exc_info = exc_info
@@ -72,7 +68,7 @@ class ExecutionError(Exception):
 
 class NormalNode(object):
     def __init__(self, code):
-        self.code = ast_parse(code)
+        self.code = compile(code, '<stdin>', 'exec', ast.PyCF_ONLY_AST, 1)
 
     def execute(self):
         to_run_exec, to_run_single = self.code.body[:-1], self.code.body[-1:]
@@ -107,10 +103,13 @@ class MagicNode(object):
 
 
     def execute(self):
+        if not self.magic:
+            raise UnknownMagic('magic command not specified')
+
         try:
             self.handler = magic_router[self.magic]
         except KeyError:
-            raise UnknownMagic(self.magic)
+            raise UnknownMagic("unknown magic command '%s'" % self.magic)
 
         return self.handler(*self.rest)
 
@@ -153,20 +152,6 @@ def parse_code_into_nodes(code):
     return nodes
 
 
-def execute_code(code):
-    try:
-        code = ast.parse(code)
-    except SyntaxError, syntax_error:
-        # It's possible we hit a syntax error because of a magic command. So see if one seems
-        # to be present.
-        try:
-            execute_handling_magic(code)
-        except SyntaxError, syntax_error:
-            pass
-    else:
-        return execute(code)
-
-
 def execute_request(content):
     try:
         code = content['code']
@@ -177,13 +162,16 @@ def execute_request(content):
 
     try:
         nodes = parse_code_into_nodes(code)
-    except (SyntaxError, UnknownMagic):
+    except SyntaxError:
         exc_type, exc_value, tb = sys.exc_info()
         return execute_reply_error(exc_type, exc_value, [])
 
     try:
         for node in nodes:
             result = node.execute()
+    except UnknownMagic:
+        exc_type, exc_value, tb = sys.exc_info()
+        return execute_reply_error(exc_type, exc_value, [])
     except ExecutionError, e:
         return execute_reply_error(*e.exc_info)
 

+ 18 - 0
apps/spark/java/livy-repl/src/test/scala/com/cloudera/hue/livy/repl/PythonInterpreterSpec.scala

@@ -155,6 +155,24 @@ class PythonInterpreterSpec extends BaseInterpreterSpec {
     ))
   }
 
+  it should "report an error if empty magic command" in withInterpreter { interpreter =>
+    val response = interpreter.execute("%")
+    response should equal(Interpreter.ExecuteError(
+      "UnknownMagic",
+      "magic command not specified",
+      List("UnknownMagic: magic command not specified\n")
+    ))
+  }
+
+  it should "report an error if unknown magic command" in withInterpreter { interpreter =>
+    val response = interpreter.execute("%foo")
+    response should equal(Interpreter.ExecuteError(
+      "UnknownMagic",
+      "unknown magic command 'foo'",
+      List("UnknownMagic: unknown magic command 'foo'\n")
+    ))
+  }
+
   it should "not execute part of the block if there is a syntax error" in withInterpreter { interpreter =>
     var response = interpreter.execute(
       """x = 1