Beispiel #1
0
    def file_input(self, nodelist):
        # Add a "from IO_MODULE import IO_CLASS" statement to the
        # beginning of the module.
        doc = None  # self.get_docstring(nodelist, symbol.file_input)

        if sys.hexversion >= 0x02050000:
            io_imp = ast.From(IO_MODULE, [(IO_CLASS, None)], 0)
            markup_imp = ast.From(MARKUP_MODULE, [(MARKUP_CLASS, None)], 0)
        else:
            io_imp = ast.From(IO_MODULE, [(IO_CLASS, None)])
            markup_imp = ast.From(MARKUP_MODULE, [(MARKUP_CLASS, None)])

        markup_assign = ast.Assign(
            [ast.AssName(MARKUP_MANGLED_CLASS, OP_ASSIGN)],
            ast.Name(MARKUP_CLASS))

        # Add an IO_INSTANCE binding for module level expressions (like
        # doc strings).  This instance will not be returned.
        io_instance = ast.CallFunc(ast.Name(IO_CLASS), [])
        io_assign_name = ast.AssName(IO_INSTANCE, OP_ASSIGN)
        io_assign = ast.Assign([io_assign_name], io_instance)

        stmts = [io_imp, io_assign, markup_imp, markup_assign]

        for node in nodelist:
            if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
                self.com_append_stmt(stmts, node)

        return ast.Module(doc, ast.Stmt(stmts))
Beispiel #2
0
    def file_input(self, nodelist):
        doc = None # self.get_docstring(nodelist, symbol.file_input)
        if sys.hexversion >= 0x02050000:
            html_imp = ast.From(
                'quixote.html',
                [('TemplateIO', '_q_TemplateIO'), ('htmltext', '_q_htmltext')],
                0)
            vars_imp = ast.From("__builtin__", [("vars", "_q_vars")], 0)
        else:
            html_imp = ast.From(
                'quixote.html',
                [('TemplateIO', '_q_TemplateIO'), ('htmltext', '_q_htmltext')])
            vars_imp = ast.From("__builtin__", [("vars", "_q_vars")])

        ptl_imports = [ vars_imp, html_imp ]
        stmts = []
        for node in nodelist:
            if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
                self.com_append_stmt(stmts, node)
        # count __future__ statements
        i = 0
        for stmt in stmts:
            if isinstance(stmt, ast.From) and stmt.modname == '__future__':
                i += 1
            else:
                break
        stmts[i:i] = ptl_imports
        return ast.Module(doc, ast.Stmt(stmts))
Beispiel #3
0
    def __init__(self, source, filename=None, lineno=-1, lookup='lenient'):
        """Create the code object, either from a string, or from an AST node.
        
        :param source: either a string containing the source code, or an AST
                       node
        :param filename: the (preferably absolute) name of the file containing
                         the code
        :param lineno: the number of the line on which the code was found
        :param lookup: the lookup class that defines how variables are looked
                       up in the context. Can be either `LenientLookup` (the
                       default), `StrictLookup`, or a custom lookup class
        """
        if isinstance(source, basestring):
            self.source = source
            node = _parse(source, mode=self.mode)
        else:
            assert isinstance(source, ast.Node)
            self.source = '?'
            if self.mode == 'eval':
                node = ast.Expression(source)
            else:
                node = ast.Module(None, source)

        self.code = _compile(node,
                             self.source,
                             mode=self.mode,
                             filename=filename,
                             lineno=lineno)
        if lookup is None:
            lookup = LenientLookup
        elif isinstance(lookup, basestring):
            lookup = {'lenient': LenientLookup, 'strict': StrictLookup}[lookup]
        self._globals = lookup.globals()
Beispiel #4
0
def transform(node, filename):
    root = ast.Module(None, node, lineno=1)
    nodes = [root]
    while nodes:
        node = nodes.pop()
        node.filename = filename
        if node.__class__ in (ast.Printnl, ast.Print):
            node.dest = ast.Name('__context')
        nodes.extend(node.getChildNodes())
    return root
 def run(self, frame):
     # fall-back for unknown statement nodes
     try:
         expr = ast.Module(None, ast.Stmt([self.__obj__]))
         expr.filename = '<run>'
         co = pycodegen.ModuleCodeGenerator(expr).getCode()
         frame.exec_(co)
     except passthroughex:
         raise
     except:
         raise Failure(self)
Beispiel #6
0
    def file_input(self, nodelist):
        doc = None # self.get_docstring(nodelist, symbol.file_input)

        html_imp = ast.From('quixote.html', [('TemplateIO', '_q_TemplateIO'),
                                             ('htmltext', '_q_htmltext')])
        vars_imp = ast.From("__builtin__", [("vars", "_q_vars")])
        stmts = [ vars_imp, html_imp ]

        for node in nodelist:
            if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
                self.com_append_stmt(stmts, node)

        return ast.Module(doc, ast.Stmt(stmts))
Beispiel #7
0
def compile(src, filename, mode='exec', showTree=False, importlogix=True,
            module=None):
    global lineno
    lineno = 0
    global modulename
    modulename = module

    implogix = ast.Import([(logixModuleName, 'logix')])
    prepend = importlogix and [implogix] or None

    if len(src) == 0:
        src = [None]

    try:
        if mode == "exec":
            statements = block(src, False, prepend)
            tree = ast.Module(None, statements)
            gen = pycodegen.ModuleCodeGenerator

        else:
            assert len(src) == 1
            stmt = src[0]

            if mode == "single":
                statements = block([stmt], False, prepend=prepend)
                tree = ast.Module(None, statements)
                gen = pycodegen.InteractiveCodeGenerator

            elif mode == "eval":
                statements = block([stmt], True, prepend)
                tree = ast.Expression(statements)
                gen = pycodegen.ExpressionCodeGenerator
            else:
                raise ValueError("compile() 3rd arg must be 'exec' or "
                                 "'eval' or 'single'")
    except CompileError, exc:
        offset = None
        raise SyntaxError(str(exc), (filename, lineno, offset, None))
Beispiel #8
0
def compileCodeObjects(filename, codeobjs):
    if len(codeobjs) == 0:
        stmts = []
    else:
        stmts = [ast.For(ast.AssName('[--codeobj--]', 'OP_ASSIGN'),
                         ast.Const(codeobjs),
                         ast.Stmt([ast.Exec(ast.Name('[--codeobj--]'),
                                            None, None)]),
                         None),
                 ast.AssName('[--codeobj--]', 'OP_DELETE')]

    module = ast.Module(None, ast.Stmt(stmts))
    compiler.misc.set_filename(filename, module)
    return pycodegen.ModuleCodeGenerator(module).getCode()
Beispiel #9
0
def transform(node, filename):
    root = ast.Module(None, node, lineno=1)
    nodes = [root]
    while nodes:
        node = nodes.pop()
        node.filename = filename
        if node.__class__ in (ast.Printnl, ast.Print):
            node.dest = ast.Name('__context')
        elif node.__class__ is ast.Const and isinstance(node.value, str):
            try:
                node.value.decode('ascii')
            except UnicodeError:
                node.value = node.value.decode('utf-8')
        nodes.extend(node.getChildNodes())
    return root
 def run(self, frame):
     expr = Interpretable(self.expr)
     expr.eval(frame)
     self.result = expr.result
     self.explanation = '... = ' + expr.explanation
     # fall-back-run the rest of the assignment
     ass = ast.Assign(self.nodes, ast.Name('__exprinfo_expr'))
     mod = ast.Module(None, ast.Stmt([ass]))
     mod.filename = '<run>'
     co = pycodegen.ModuleCodeGenerator(mod).getCode()
     try:
         frame.exec_(co, __exprinfo_expr=expr.result)
     except passthroughex:
         raise
     except:
         raise Failure(self)
Beispiel #11
0
 def parse(self, code):
     self.lexer.input(code)
     result = self.parser.parse(lexer=self.lexer)
     return ast.Module(None, result)
Beispiel #12
0
 def parsefile(self, node):
     doc = node.Documentation
     body = self.transform(node)
     if doc:
         del body.nodes[0]
     return ast.Module(doc, body)