當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。