def visit_FunctionDef(self, node): self._write_py_context(node.lineno + len(node.decorator_list)) func = self.visit_function_inline(node) self.block.bind_var(self.writer, node.name, func.expr) # Docstring should be the 1st statement (expression), if exists first_expr = node.body[0] if isinstance(first_expr, ast.Expr) and isinstance( first_expr.value, ast.Str): docstring = first_expr.value doc_assign = ast.Assign( loc=docstring.loc, targets=[ ast.Attribute(value=ast.Name(id=node.name), attr='__doc__') ], value=docstring, ) self.visit_Assign(doc_assign) while node.decorator_list: decorator = node.decorator_list.pop() wrapped = ast.Name(id=node.name) decorated = ast.Call(func=decorator, args=[wrapped], keywords=[], starargs=None, kwargs=None) target = ast.Assign(targets=[wrapped], value=decorated, loc=node.loc) self.visit_Assign(target)
def testWriteExceptDispatcherMultipleExcept(self): visitor = stmt.StatementVisitor(_MakeModuleBlock()) handlers = [ ast.ExceptHandler(type=ast.Name(id='foo')), ast.ExceptHandler(type=ast.Name(id='bar')) ] self.assertEqual( visitor._write_except_dispatcher( # pylint: disable=protected-access 'exc', 'tb', handlers), [1, 2]) expected = re.compile( r'ResolveGlobal\(.*foo.*\bif .*\bIsInstance\(.*\{.*goto Label1.*' r'ResolveGlobal\(.*bar.*\bif .*\bIsInstance\(.*\{.*goto Label2.*' r'\bRaise\(exc\.ToObject\(\), nil, tb\.ToObject\(\)\)', re.DOTALL) self.assertRegexpMatches(visitor.writer.getvalue(), expected)
def visit_FunctionDef(self, node): self._write_py_context(node.lineno + len(node.decorator_list)) func = self.expr_visitor.visit_function_inline(node) self.block.bind_var(self.writer, node.name, func.expr) while node.decorator_list: decorator = node.decorator_list.pop() wrapped = ast.Name(id=node.name) decorated = ast.Call(func=decorator, args=[wrapped], keywords=[], starargs=None, kwargs=None) target = ast.Assign(targets=[wrapped], value=decorated, loc=node.loc) self.visit_Assign(target)
def testWriteExceptDispatcherBareExceptionNotLast(self): visitor = stmt.StatementVisitor(_MakeModuleBlock()) handlers = [ ast.ExceptHandler(type=None), ast.ExceptHandler(type=ast.Name(id='foo')) ] self.assertRaisesRegexp( util.ParseError, r"default 'except:' must be last", visitor._write_except_dispatcher, # pylint: disable=protected-access 'exc', 'tb', handlers)
def testWriteExceptDispatcherBareExcept(self): visitor = stmt.StatementVisitor(_MakeModuleBlock()) handlers = [ ast.ExceptHandler(type=ast.Name(id='foo')), ast.ExceptHandler(type=None) ] self.assertEqual( visitor._write_except_dispatcher( # pylint: disable=protected-access 'exc', 'tb', handlers), [1, 2]) expected = re.compile( r'ResolveGlobal\(.*foo.*\bIsInstance\(.*' r'goto Label1.*goto Label2', re.DOTALL) self.assertRegexpMatches(visitor.writer.getvalue(), expected)
def _assign_docstring(self, node): self._docstring = node doc_assign = ast.Assign(loc=node.loc, targets=[ast.Name(id='__doc__')], value=node) self.visit_Assign(doc_assign)