当前位置: 首页>>代码示例>>Python>>正文


Python compat.py3k方法代码示例

本文整理汇总了Python中mako.compat.py3k方法的典型用法代码示例。如果您正苦于以下问题:Python compat.py3k方法的具体用法?Python compat.py3k怎么用?Python compat.py3k使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mako.compat的用法示例。


在下文中一共展示了compat.py3k方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _compile_text

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def _compile_text(template, text, filename):
    identifier = template.module_id
    source, lexer = _compile(
        template,
        text,
        filename,
        generate_magic_comment=template.disable_unicode,
    )

    cid = identifier
    if not compat.py3k and isinstance(cid, compat.text_type):
        cid = cid.encode()
    module = types.ModuleType(cid)
    code = compile(source, cid, "exec")

    # this exec() works for 2.4->3.3.
    exec(code, module.__dict__, module.__dict__)
    return (source, module) 
开发者ID:remg427,项目名称:misp42splunk,代码行数:20,代码来源:template.py

示例2: test_py_utf8_html_error_template

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def test_py_utf8_html_error_template(self):
        try:
            foo = u("日本")  # noqa
            raise RuntimeError("test")
        except:
            html_error = exceptions.html_error_template().render()
            if compat.py3k:
                assert "RuntimeError: test" in html_error.decode("utf-8")
                assert "foo = u("日本")" in html_error.decode(
                    "utf-8"
                ) or "foo = u("日本")" in html_error.decode("utf-8")
            else:
                assert "RuntimeError: test" in html_error
                assert (
                    "foo = u("日本")" in html_error
                    or "foo = u("日本")" in html_error
                ) 
开发者ID:sqlalchemy,项目名称:mako,代码行数:19,代码来源:test_exceptions.py

示例3: test_utf8_format_exceptions_pygments

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def test_utf8_format_exceptions_pygments(self):
        """test that htmlentityreplace formatting is applied to
           exceptions reported with format_exceptions=True"""

        l = TemplateLookup(format_exceptions=True)
        if compat.py3k:
            l.put_string(
                "foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}"""
            )
        else:
            l.put_string(
                "foo.html",
                """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""",
            )

        if compat.py3k:
            assert "&#39;привет&#39;</span>" in l.get_template(
                "foo.html"
            ).render().decode("utf-8")
        else:
            assert (
                "&#39;&#x43F;&#x440;&#x438;&#x432;"
                "&#x435;&#x442;&#39;</span>"
            ) in l.get_template("foo.html").render().decode("utf-8") 
开发者ID:sqlalchemy,项目名称:mako,代码行数:26,代码来源:test_exceptions.py

示例4: test_tback_no_trace_from_py_file

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def test_tback_no_trace_from_py_file(self):
        try:
            t = self._file_template("runtimeerr.html")
            t.render()
        except:
            t, v, tback = sys.exc_info()

        if not compat.py3k:
            # blow away tracebaack info
            sys.exc_clear()

        # and don't even send what we have.
        html_error = exceptions.html_error_template().render_unicode(
            error=v, traceback=None
        )
        assert (
            "local variable &#39;y&#39; referenced before assignment"
            in html_error
        ) 
开发者ID:sqlalchemy,项目名称:mako,代码行数:21,代码来源:test_exceptions.py

示例5: test_python_fragment

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def test_python_fragment(self):
        parsed = ast.PythonFragment("for x in foo:", **exception_kwargs)
        eq_(parsed.declared_identifiers, set(["x"]))
        eq_(parsed.undeclared_identifiers, set(["foo"]))

        parsed = ast.PythonFragment("try:", **exception_kwargs)

        if compat.py3k:
            parsed = ast.PythonFragment(
                "except MyException as e:", **exception_kwargs
            )
        else:
            parsed = ast.PythonFragment(
                "except MyException, e:", **exception_kwargs
            )
        eq_(parsed.declared_identifiers, set(["e"]))
        eq_(parsed.undeclared_identifiers, set(["MyException"])) 
开发者ID:sqlalchemy,项目名称:mako,代码行数:19,代码来源:test_ast.py

示例6: test_utf8_format_exceptions_no_pygments

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def test_utf8_format_exceptions_no_pygments(self):
        """test that htmlentityreplace formatting is applied to
           exceptions reported with format_exceptions=True"""

        l = TemplateLookup(format_exceptions=True)
        if compat.py3k:
            l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""")
        else:
            l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""")

        if compat.py3k:
            assert '<div class="sourceline">${&#39;привет&#39; + foobar}</div>'\
                in result_lines(l.get_template("foo.html").render().decode('utf-8'))
        else:
            assert '${u&#39;&#x43F;&#x440;&#x438;&#x432;&#x435;'\
                   '&#x442;&#39; + foobar}' in \
                result_lines(l.get_template("foo.html").render().decode('utf-8')) 
开发者ID:jhpyle,项目名称:docassemble,代码行数:19,代码来源:test_exceptions.py

示例7: test_escapes_html_tags

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def test_escapes_html_tags(self):
        from mako.exceptions import html_error_template

        x = Template("""
        X:
        <% raise Exception('<span style="color:red">Foobar</span>') %>
        """)

        try:
            x.render()
        except:
            # <h3>Exception: <span style="color:red">Foobar</span></h3>
            markup = html_error_template().render(full=False, css=False)
            if compat.py3k:
                assert '<span style="color:red">Foobar</span></h3>'\
                            .encode('ascii') not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;'\
                            .encode('ascii') in markup
            else:
                assert '<span style="color:red">Foobar</span></h3>' \
                            not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;' in markup 
开发者ID:jhpyle,项目名称:docassemble,代码行数:26,代码来源:test_template.py

示例8: test_unicode_literal_in_expr

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def test_unicode_literal_in_expr(self):
        if compat.py3k:
            self._do_memory_test(
                u("""## -*- coding: utf-8 -*-
                ${"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"}
                """).encode('utf-8'),
                u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""),
                filters = lambda s:s.strip()
            )
        else:
            self._do_memory_test(
                u("""## -*- coding: utf-8 -*-
                ${u"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"}
                """).encode('utf-8'),
                u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""),
                filters = lambda s:s.strip()
            ) 
开发者ID:jhpyle,项目名称:docassemble,代码行数:19,代码来源:test_template.py

示例9: __init__

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def __init__(
        self,
        text,
        filename=None,
        disable_unicode=False,
        input_encoding=None,
        preprocessor=None,
    ):
        self.text = text
        self.filename = filename
        self.template = parsetree.TemplateNode(self.filename)
        self.matched_lineno = 1
        self.matched_charpos = 0
        self.lineno = 1
        self.match_position = 0
        self.tag = []
        self.control_line = []
        self.ternary_stack = []
        self.disable_unicode = disable_unicode
        self.encoding = input_encoding

        if compat.py3k and disable_unicode:
            raise exceptions.UnsupportedError(
                "Mako for Python 3 does not " "support disabling Unicode"
            )

        if preprocessor is None:
            self.preprocessor = []
        elif not hasattr(preprocessor, "__iter__"):
            self.preprocessor = [preprocessor]
        else:
            self.preprocessor = preprocessor 
开发者ID:remg427,项目名称:misp42splunk,代码行数:34,代码来源:lexer.py

示例10: _get_module_info_from_callable

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def _get_module_info_from_callable(callable_):
    if compat.py3k:
        return _get_module_info(callable_.__globals__["__name__"])
    else:
        return _get_module_info(callable_.func_globals["__name__"]) 
开发者ID:remg427,项目名称:misp42splunk,代码行数:7,代码来源:template.py

示例11: syntax_highlight

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def syntax_highlight(filename="", language=None):
    mako_lexer = MakoLexer()
    if compat.py3k:
        python_lexer = Python3Lexer()
    else:
        python_lexer = PythonLexer()
    if filename.startswith("memory:") or language == "mako":
        return lambda string: highlight(
            string, mako_lexer, pygments_html_formatter
        )
    return lambda string: highlight(
        string, python_lexer, pygments_html_formatter
    ) 
开发者ID:remg427,项目名称:misp42splunk,代码行数:15,代码来源:pygmentplugin.py

示例12: __init__

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def __init__(self, text, filename=None,
                 disable_unicode=False,
                 input_encoding=None, preprocessor=None):
        self.text = text
        self.filename = filename
        self.template = parsetree.TemplateNode(self.filename)
        self.matched_lineno = 1
        self.matched_charpos = 0
        self.lineno = 1
        self.match_position = 0
        self.tag = []
        self.control_line = []
        self.ternary_stack = []
        self.disable_unicode = disable_unicode
        self.encoding = input_encoding

        if compat.py3k and disable_unicode:
            raise exceptions.UnsupportedError(
                "Mako for Python 3 does not "
                "support disabling Unicode")

        if preprocessor is None:
            self.preprocessor = []
        elif not hasattr(preprocessor, '__iter__'):
            self.preprocessor = [preprocessor]
        else:
            self.preprocessor = preprocessor 
开发者ID:jpush,项目名称:jbox,代码行数:29,代码来源:lexer.py

示例13: _compile_text

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def _compile_text(template, text, filename):
    identifier = template.module_id
    source, lexer = _compile(template, text, filename,
                             generate_magic_comment=template.disable_unicode)

    cid = identifier
    if not compat.py3k and isinstance(cid, compat.text_type):
        cid = cid.encode()
    module = types.ModuleType(cid)
    code = compile(source, cid, 'exec')

    # this exec() works for 2.4->3.3.
    exec(code, module.__dict__, module.__dict__)
    return (source, module) 
开发者ID:jpush,项目名称:jbox,代码行数:16,代码来源:template.py

示例14: _get_module_info_from_callable

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def _get_module_info_from_callable(callable_):
    if compat.py3k:
        return _get_module_info(callable_.__globals__['__name__'])
    else:
        return _get_module_info(callable_.func_globals['__name__']) 
开发者ID:jpush,项目名称:jbox,代码行数:7,代码来源:template.py

示例15: compile

# 需要导入模块: from mako import compat [as 别名]
# 或者: from mako.compat import py3k [as 别名]
def compile(node,
            uri,
            filename=None,
            default_filters=None,
            buffer_filters=None,
            imports=None,
            future_imports=None,
            source_encoding=None,
            generate_magic_comment=True,
            disable_unicode=False,
            strict_undefined=False,
            enable_loop=True,
            reserved_names=frozenset()):
    """Generate module source code given a parsetree node,
      uri, and optional source filename"""

    # if on Py2K, push the "source_encoding" string to be
    # a bytestring itself, as we will be embedding it into
    # the generated source and we don't want to coerce the
    # result into a unicode object, in "disable_unicode" mode
    if not compat.py3k and isinstance(source_encoding, compat.text_type):
        source_encoding = source_encoding.encode(source_encoding)

    buf = util.FastEncodingBuffer()

    printer = PythonPrinter(buf)
    _GenerateRenderMethod(printer,
                          _CompileContext(uri,
                                          filename,
                                          default_filters,
                                          buffer_filters,
                                          imports,
                                          future_imports,
                                          source_encoding,
                                          generate_magic_comment,
                                          disable_unicode,
                                          strict_undefined,
                                          enable_loop,
                                          reserved_names),
                          node)
    return buf.getvalue() 
开发者ID:jpush,项目名称:jbox,代码行数:43,代码来源:codegen.py


注:本文中的mako.compat.py3k方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。