hacking with returns again

This commit is contained in:
Paul Tagliamonte 2012-12-23 14:01:45 -05:00
parent 002f103aa0
commit 71dc59a441
2 changed files with 18 additions and 9 deletions

View File

@ -63,13 +63,15 @@ def _ast_import(tree):
def _ast_if(node, children, obj): def _ast_if(node, children, obj):
cond = children.pop(0) cond = children.pop(0)
true = children.pop(0) true = children.pop(0)
flse = children.pop(0) flse = []
if children != []:
flse = children.pop(0)
true = true if isinstance(true, list) else [true] true = true if isinstance(true, list) else [true]
flse = flse if isinstance(flse, list) else [flse] flse = flse if isinstance(flse, list) else [flse]
true = _adjust_body(true) true = _adjust_body(true, do_ret=obj.in_fn)
flse = _adjust_body(flse) flse = _adjust_body(flse, do_ret=obj.in_fn)
ret = ast.If(test=cond, body=true, orelse=flse) ret = ast.If(test=cond, body=true, orelse=flse)
return ret return ret
@ -97,7 +99,6 @@ special_cases = {
"is": _ast_cmp, "is-not": _ast_cmp, "is": _ast_cmp, "is-not": _ast_cmp,
"if": _ast_if, "if": _ast_if,
"return": _ast_return,
"do": _ast_do, "do": _ast_do,
"raise": _ast_raise, "raise": _ast_raise,
} }
@ -128,6 +129,7 @@ class AST27Converter(object):
"for": self._ast_for, "for": self._ast_for,
"kwapply": self._ast_kwapply, "kwapply": self._ast_kwapply,
} }
self.in_fn = False
def _def(self, node): def _def(self, node):
""" For the `def` operator """ """ For the `def` operator """
@ -166,8 +168,8 @@ class AST27Converter(object):
body = body if isinstance(body, list) else [body] body = body if isinstance(body, list) else [body]
orel = [] orel = []
body = _adjust_body(body) body = _adjust_body(body, do_ret=self.in_fn)
orel = _adjust_body(orel) orel = _adjust_body(orel, do_ret=self.in_fn)
return ast.While( return ast.While(
test=test, test=test,
@ -187,8 +189,8 @@ class AST27Converter(object):
body = body if isinstance(body, list) else [body] body = body if isinstance(body, list) else [body]
orel = [] orel = []
body = _adjust_body(body) body = _adjust_body(body, do_ret=self.in_fn)
orel = _adjust_body(orel) orel = _adjust_body(orel, do_ret=self.in_fn)
return ast.For( return ast.For(
target=ast.Name(id=str(aname), ctx=ast.Store()), target=ast.Name(id=str(aname), ctx=ast.Store()),
@ -210,9 +212,14 @@ class AST27Converter(object):
# verify child count... # verify child count...
c = [] c = []
_pop_fn = self.in_fn
self.in_fn = True
for child in args: for child in args:
c.append(self.render(child)) c.append(self.render(child))
self.in_fn = _pop_fn
cont = c[-1] # XXX: Wrong... cont = c[-1] # XXX: Wrong...
body = cont if isinstance(cont, list) else [cont] body = cont if isinstance(cont, list) else [cont]
@ -220,7 +227,7 @@ class AST27Converter(object):
# Shim in docstrings # Shim in docstrings
body.insert(0, ast.Expr(value=ast.Str(s=str(doc)))) body.insert(0, ast.Expr(value=ast.Str(s=str(doc))))
body = _adjust_body(body) body = _adjust_body(body, do_ret=True)
ret = ast.FunctionDef( ret = ast.FunctionDef(
name=str(name), name=str(name),

View File

@ -20,6 +20,7 @@ class HyFinder(object):
def is_package(self, fullname): def is_package(self, fullname):
dirpath = "/".join(fullname.split(".")) dirpath = "/".join(fullname.split("."))
for pth in sys.path: for pth in sys.path:
pth = os.path.abspath(pth)
composed_path = "%s/%s/__init__.hy" % (pth, dirpath) composed_path = "%s/%s/__init__.hy" % (pth, dirpath)
if os.path.exists(composed_path): if os.path.exists(composed_path):
return True return True
@ -30,6 +31,7 @@ class HyFinder(object):
dirpath = "/".join(fullname.split(".")) dirpath = "/".join(fullname.split("."))
for pth in sys.path: for pth in sys.path:
pth = os.path.abspath(pth)
for fp in fls: for fp in fls:
composed_path = fp % ("%s/%s" % (pth, dirpath)) composed_path = fp % ("%s/%s" % (pth, dirpath))
if os.path.exists(composed_path): if os.path.exists(composed_path):