Exemplo n.º 1
0
def test_genshi_nameconstant():
    from genshi.template.astutil import ASTCodeGenerator, parse
    from tg.renderers.genshi import GenshiRenderer

    # This checks genshi gets monkeypatched to fix it on Py34 if option is provided
    GenshiRenderer.create(Bunch({
        'templating.genshi.name_constant_patch': True,
        'use_dotted_templatenames': False,
        'auto_reload_templates': False,
        'paths': Bunch({'templates': '/tmp'})
    }), None)
    assert hasattr(ASTCodeGenerator, 'visit_NameConstant')

    astgen = ASTCodeGenerator(parse('range(10)', mode='eval'))
    for n in (False, True, None):
        astgen._new_line()
        astgen.visit_NameConstant(Bunch(value=n))
        line = str(astgen.line)
        assert line == str(n), line

    astgen._new_line()
    try:
        astgen.visit_NameConstant(Bunch(value='HELLO_WORLD'))
    except Exception as e:
        assert 'Unknown NameConstant' in str(e)
    else:
        assert False
Exemplo n.º 2
0
def test_genshi_nameconstant():
    from genshi.template.astutil import ASTCodeGenerator, parse
    from tg.renderers.genshi import GenshiRenderer

    # This checks genshi gets monkeypatched to fix it on Py34 if option is provided
    GenshiRenderer.create(
        Bunch(
            {
                "templating.genshi.name_constant_patch": True,
                "use_dotted_templatenames": False,
                "auto_reload_templates": False,
                "paths": Bunch({"templates": "/tmp"}),
            }
        ),
        None,
    )
    assert hasattr(ASTCodeGenerator, "visit_NameConstant")

    astgen = ASTCodeGenerator(parse("range(10)", mode="eval"))
    for n in (False, True, None):
        astgen._new_line()
        astgen.visit_NameConstant(Bunch(value=n))
        line = str(astgen.line)
        assert line == str(n), line

    astgen._new_line()
    try:
        astgen.visit_NameConstant(Bunch(value="HELLO_WORLD"))
    except Exception as e:
        assert "Unknown NameConstant" in str(e)
    else:
        assert False
Exemplo n.º 3
0
def _compile(node,
             source=None,
             mode='eval',
             filename=None,
             lineno=-1,
             xform=None):
    if not filename:
        filename = '<string>'
    if IS_PYTHON2:
        # Python 2 requires non-unicode filenames
        if isinstance(filename, str):
            filename = filename.encode('utf-8', 'replace')
    else:
        # Python 3 requires unicode filenames
        if not isinstance(filename, str):
            filename = filename.decode('utf-8', 'replace')
    if lineno <= 0:
        lineno = 1

    if xform is None:
        xform = {
            'eval': ExpressionASTTransformer
        }.get(mode, TemplateASTTransformer)
    tree = xform().visit(node)

    if mode == 'eval':
        name = '<Expression %r>' % (source or '?')
    else:
        lines = source.splitlines()
        if not lines:
            extract = ''
        else:
            extract = lines[0]
        if len(lines) > 1:
            extract += ' ...'
        name = '<Suite %r>' % (extract)
    new_source = ASTCodeGenerator(tree).code
    code = compile(new_source, filename, mode)

    try:
        # We'd like to just set co_firstlineno, but it's readonly. So we need
        # to clone the code object while adjusting the line number
        return build_code_chunk(code, filename, name, lineno)
    except RuntimeError:
        return code
Exemplo n.º 4
0
def _compile(node,
             source=None,
             mode='eval',
             filename=None,
             lineno=-1,
             xform=None):
    if isinstance(filename, unicode):
        # unicode file names not allowed for code objects
        filename = filename.encode('utf-8', 'replace')
    elif not filename:
        filename = '<string>'
    if lineno <= 0:
        lineno = 1

    if xform is None:
        xform = {
            'eval': ExpressionASTTransformer
        }.get(mode, TemplateASTTransformer)
    tree = xform().visit(node)

    if mode == 'eval':
        name = '<Expression %r>' % (source or '?')
    else:
        lines = source.splitlines()
        if not lines:
            extract = ''
        else:
            extract = lines[0]
        if len(lines) > 1:
            extract += ' ...'
        name = '<Suite %r>' % (extract)
    new_source = ASTCodeGenerator(tree).code
    code = compile(new_source, filename, mode)

    try:
        # We'd like to just set co_firstlineno, but it's readonly. So we need
        # to clone the code object while adjusting the line number
        return CodeType(0, code.co_nlocals, code.co_stacksize,
                        code.co_flags | 0x0040, code.co_code, code.co_consts,
                        code.co_names, code.co_varnames, filename, name,
                        lineno, code.co_lnotab, (), ())
    except RuntimeError:
        return code