diff --git a/bin/py2ast b/bin/py2ast new file mode 100755 index 0000000..038041b --- /dev/null +++ b/bin/py2ast @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +import sys +import ast + + +print(ast.dump(ast.parse(open(sys.argv[1], 'r').read()))) diff --git a/hy/compiler.py b/hy/compiler.py index dfb9985..9b7941f 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -105,6 +105,7 @@ class HyASTCompiler(object): expr.pop(0) # try if sys.version_info[0] >= 3 and sys.version_info[1] >= 3: + # Python 3.3 features a rename of TryExcept to Try. Try = ast.Try else: Try = ast.TryExcept @@ -120,12 +121,27 @@ class HyASTCompiler(object): @builds("catch") def compile_catch_expression(self, expr): expr.pop(0) # catch + _type = self.compile(expr.pop(0)) + name = expr.pop(0) + + if sys.version_info[0] >= 3: + # Python3 features a change where the Exception handler + # moved the name from a Name() to a pure Python String type. + # + # We'll just make sure it's a pure "string", and let it work + # it's magic. + name = str(name) + else: + # Python2 requires an ast.Name, set to ctx Store. + name = self.compile(name) + name.ctx = ast.Store() + return ast.ExceptHandler( lineno=expr.start_line, col_offset=expr.start_column, - type=self.compile(expr.pop(0)), - name=None, - body=self._code_branch(self.compile(expr.pop(0)))) + type=_type, + name=name, + body=self._code_branch([self.compile(x) for x in expr])) def _code_branch(self, branch): if isinstance(branch, list): @@ -465,7 +481,9 @@ class HyASTCompiler(object): ctx=ast.Load() ) - return ast.Name(id=str(symbol), ctx=ast.Load(), + return ast.Name(id=str(symbol), + arg=str(symbol), + ctx=ast.Load(), lineno=symbol.start_line, col_offset=symbol.start_column) diff --git a/site/app.hy b/site/app.hy index 74ae047..5ddbf57 100644 --- a/site/app.hy +++ b/site/app.hy @@ -28,6 +28,6 @@ (post-route "/hy2py" [] (try (hy-to-py (get request.form "code")) - (catch LexException (err "Incomplete Code.")) - (catch HyError (err "Generic error during processing.")) - (catch Exception (err "Erm, you broke something.")))) + (catch LexException e (err "Incomplete Code.")) + (catch HyError e (err "Generic error during processing.")) + (catch Exception e (err "Erm, you broke something.")))) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 76be869..f052bf6 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -123,10 +123,8 @@ "NATIVE: test Exceptions" (try (throw (KeyError)) - (catch IOError (assert (= 2 1))) - (catch KeyError (do - (+ 1 1) - (assert (= 1 1)))))) + (catch IOError e (assert (= 2 1))) + (catch KeyError e (+ 1 1) (assert (= 1 1))))) (defn test-earmuffs [] "NATIVE: Test earmuffs"