transforms/inline: support reraise and tuple exception matching

This commit is contained in:
Sebastien Bourdeauducq 2014-09-23 22:10:15 +08:00
parent f7075b24f6
commit 4d3d15ad99
1 changed files with 25 additions and 18 deletions

View File

@ -218,6 +218,7 @@ class _ReferenceReplacer(ast.NodeVisitor):
def visit_Raise(self, node): def visit_Raise(self, node):
if node.cause is not None: if node.cause is not None:
raise NotImplementedError("Exception causes are not supported") raise NotImplementedError("Exception causes are not supported")
if node.exc is not None:
exception_class = self.rm.get(self.obj, self.func_name, node.exc) exception_class = self.rm.get(self.obj, self.func_name, node.exc)
if not inspect.isclass(exception_class): if not inspect.isclass(exception_class):
raise NotImplementedError("Exception must be a class") raise NotImplementedError("Exception must be a class")
@ -229,19 +230,25 @@ class _ReferenceReplacer(ast.NodeVisitor):
node.exc) node.exc)
return node return node
def _encode_exception(self, e):
exception_class = self.rm.get(self.obj, self.func_name, e)
if not inspect.isclass(exception_class):
raise NotImplementedError("Exception type must be a class")
exception_id = self.rm.exception_map[exception_class]
return ast.copy_location(
ast.Call(func=ast.Name("EncodedException", ast.Load()),
args=[value_to_ast(exception_id)],
keywords=[], starargs=None, kwargs=None),
e)
def visit_ExceptHandler(self, node): def visit_ExceptHandler(self, node):
if node.name is not None: if node.name is not None:
raise NotImplementedError("'as target' is not supported") raise NotImplementedError("'as target' is not supported")
if node.type is not None: if node.type is not None:
exception_class = self.rm.get(self.obj, self.func_name, node.type) if isinstance(node.type, ast.Tuple):
if not inspect.isclass(exception_class): node.type.elts = [self._encode_exception(e) for e in node.type.elts]
raise NotImplementedError("Exception type must be a class") else:
exception_id = self.rm.exception_map[exception_class] node.type = self._encode_exception(node.type)
node.type = ast.copy_location(
ast.Call(func=ast.Name("EncodedException", ast.Load()),
args=[value_to_ast(exception_id)],
keywords=[], starargs=None, kwargs=None),
node.type)
self.generic_visit(node) self.generic_visit(node)
return node return node