Пример #1
0
def build_single_input(builder, nb):
    atoms = get_atoms(builder, nb)
    l = len(atoms)
    if l == 1 or l==2:
        atom0 = atoms[0]
        if isinstance(atom0, TokenObject) and atom0.name == builder.parser.tokens['NEWLINE']:
            # atom0 = ast.Pass(atom0.lineno) # break test_astcompiler
            atom0 = ast.Stmt([], atom0.lineno) # break test_astbuilder
        elif not isinstance(atom0, ast.Stmt):
            atom0 = ast.Stmt([atom0], atom0.lineno)
        builder.push(ast.Module(builder.space.w_None, atom0, atom0.lineno))
    else:
        assert False, "Forbidden path"
Пример #2
0
def build_file_input(builder, nb):
    stmts = []
    atoms = get_atoms(builder, nb)
    if atoms:
        lineno = atoms[0].lineno
    else:
        lineno = -1
    for node in atoms:
        if isinstance(node, ast.Stmt):
            stmts.extend(node.nodes)
        elif isinstance(node, TokenObject) and node.name == builder.parser.tokens['ENDMARKER']:
            # XXX Can't we just remove the last element of the list ?
            break
        elif isinstance(node, TokenObject) and node.name == builder.parser.tokens['NEWLINE']:
            continue
        else:
            stmts.append(node)
    main_stmt = ast.Stmt(stmts, lineno)
    doc = get_docstring(builder,main_stmt)
    return builder.push(ast.Module(doc, main_stmt, lineno))
Пример #3
0
 def stmt(self, stmt, msg=None):
     mod = ast.Module([stmt])
     self.mod(mod, msg)
Пример #4
0
 def expr(self, node, msg=None, exc=validate.ValidationError):
     mod = ast.Module([ast.Expr(node, 0, 0)])
     self.mod(mod, msg, exc=exc)