示例#1
0
    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)
示例#2
0
 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)
示例#3
0
    def assign_attribute(self, obj, attr_name, value):
        obj_node   = self.quote(obj)
        dot_loc    = self._add(".")
        name_loc   = self._add(attr_name)
        _          = self._add(" ")
        equals_loc = self._add("=")
        _          = self._add(" ")
        value_node = self.quote(value)

        attr_node  = asttyped.AttributeT(value=obj_node, attr=attr_name, ctx=None,
                                         type=value_node.type,
                                         dot_loc=dot_loc, attr_loc=name_loc,
                                         loc=obj_node.loc.join(name_loc))

        return ast.Assign(targets=[attr_node], value=value_node,
                          op_locs=[equals_loc], loc=name_loc.join(value_node.loc))
示例#4
0
 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)