Beispiel #1
0
    def elif_(self, expr):

        if self.childnodes is self.elsenodes:
            raise TextTemplateError('else_() has already been called')

        self.codeobj_list.append(_compile(expr.strip(), '<string>', 'eval'))
        self.childnodes = []
        self.ifnodes_list.append(self.childnodes)
Beispiel #2
0
def compile_until(terminators, source, filename, mode, flags=0, dont_inherit=0):

    """Do built-in `compile()` until one of the tokens in
    `terminators` found in `source`.

    Return `(codeobj, offset)` tuple.

    The argument `terminators` is a list of string tokens specifies
    the end of the source code.

    >>> src = 'a=3}}'
    >>> codeobj, offset = compile_until(['}}'], src, '<string>', 'exec')
    >>> src[offset:]
    '}}'
    >>> exec codeobj
    >>> a
    3
    """

    try:
        codeobj = _compile(source, filename, mode, flags, dont_inherit)
    except SyntaxError as e:
        lines = source.splitlines(True)
        i_line = e.lineno - 1
        offset = e.offset - 1
        lines_body = lines[:i_line]
        lines_rest = lines[i_line:]
        lines_body.append(lines[i_line][:offset])
        lines_rest[0] = lines[i_line][offset:]
        body = ''.join(lines_body)
        rest = ''.join(lines_rest)
        for token in terminators:
            if rest.startswith(token):
                break
        else:
            # The SyntaxError was raised by but terminators.
            raise
        return _compile(body, filename, mode, flags, dont_inherit), len(body)
    else:
        raise ValueError('didn\'t encouter any terminator')
Beispiel #3
0
    def __init__(self, expr):

        Block.__init__(self)
        self.codeobj_list = [_compile(expr.strip(), '<string>', 'eval')]
        self.ifnodes_list = [self.childnodes]
        self.elsenodes = []
Beispiel #4
0
 def __init__(self, expr):
     self.codeobj = _compile(expr, '<string>', 'eval')
Beispiel #5
0
    def __init__(self, expr):

        Block.__init__(self)
        self.codeobj = _compile('(locals() for %s)' % expr, '<string>', 'eval')