Fixing catch a bit to now force a param.

This commit is contained in:
Paul R. Tagliamonte 2013-03-14 09:21:03 -04:00
parent 1fdbca30a7
commit 9b32506854
4 changed files with 34 additions and 11 deletions

7
bin/py2ast Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env python
import sys
import ast
print(ast.dump(ast.parse(open(sys.argv[1], 'r').read())))

View File

@ -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)

View File

@ -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."))))

View File

@ -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"