本文整理汇总了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)
示例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
)
示例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 "'привет'</span>" in l.get_template(
"foo.html"
).render().decode("utf-8")
else:
assert (
"'прив"
"ет'</span>"
) in l.get_template("foo.html").render().decode("utf-8")
示例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 'y' referenced before assignment"
in html_error
)
示例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"]))
示例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">${'привет' + foobar}</div>'\
in result_lines(l.get_template("foo.html").render().decode('utf-8'))
else:
assert '${u'приве'\
'т' + foobar}' in \
result_lines(l.get_template("foo.html").render().decode('utf-8'))
示例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 '<span style="color:red"'\
'>Foobar</span>'\
.encode('ascii') in markup
else:
assert '<span style="color:red">Foobar</span></h3>' \
not in markup
assert '<span style="color:red"'\
'>Foobar</span>' in markup
示例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()
)
示例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
示例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__"])
示例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
)
示例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
示例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)
示例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__'])
示例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()