Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
code = """# -*- coding: utf-8 -*-
% if 2 == 2: /an error
${'привет'}
% endif
"""
else:
code = """# -*- coding: utf-8 -*-
% if 2 == 2: /an error
${u'привет'}
% endif
"""
try:
template = Template(code)
template.render_unicode()
except exceptions.CompileException:
html_error = exceptions.html_error_template().render()
if compat.py3k:
assert (
(
"CompileException: Fragment 'if 2 == 2: /an "
"error' is not a partial control statement "
"at line: 2 char: 1"
).encode(sys.getdefaultencoding(), "htmlentityreplace")
in html_error
)
else:
assert (
"CompileException: Fragment 'if 2 == 2: /an "
"error' is not a partial control statement "
"at line: 2 char: 1"
) in html_error
def _render_error(template, context, error):
if template.error_handler:
result = template.error_handler(context, error)
if not result:
compat.reraise(*sys.exc_info())
else:
error_template = exceptions.html_error_template()
if context._outputting_as_unicode:
context._buffer_stack[:] = [
util.FastEncodingBuffer(as_unicode=True)
]
else:
context._buffer_stack[:] = [
util.FastEncodingBuffer(
error_template.output_encoding,
error_template.encoding_errors,
)
]
context._set_with_template(error_template)
error_template.render_context(context, error=error)
def serve_template(templatename, **kwargs):
html_dir = 'public/html/'
_hplookup = TemplateLookup(directories=[html_dir])
try:
template = _hplookup.get_template(templatename)
return template.render(**kwargs)
except:
return exceptions.html_error_template().render()
def get(self):
from appname import person
# we check and create a Person if you login to this app
person.initilize_person(self.request.headers)
try:
import settings
template = Template(filename='templates/base.mako')
out = template.render(CLOUD_API_KEY=settings.CLOUD_API_KEY)
except:
out = exceptions.html_error_template().render()
logging.exception('Oh NO! Rendering error.')
self.response.out.write(out)
def mako_html_data( exc_value ):
if isinstance( exc_value, ( mako.exceptions.CompileException, mako.exceptions.SyntaxException ) ):
return mako.exceptions.html_error_template().render( full=False, css=False )
if isinstance( exc_value, AttributeError ) and exc_value.args[0].startswith( "'Undefined' object has no attribute" ):
return mako.exceptions.html_error_template().render( full=False, css=False )
formatters.append( mako_html_data )
def get(self, name, style='html'):
key = "%s.%s" % (name.lower(), style)
template = self.templates.get(key)
if not template:
try:
template = config['pylons.g'].mako_lookup.get_template(key)
if template:
self.templates[key] = template
except TemplateLookupException:
print exceptions.html_error_template().render()
if not template:
raise AttributeError('template for %s not found' % name)
return template
def _render_error(template, context, error):
if template.error_handler:
result = template.error_handler(context, error)
if not result:
compat.reraise(*sys.exc_info())
else:
error_template = exceptions.html_error_template()
if context._outputting_as_unicode:
context._buffer_stack[:] = [
util.FastEncodingBuffer(as_unicode=True)]
else:
context._buffer_stack[:] = [util.FastEncodingBuffer(
error_template.output_encoding,
error_template.encoding_errors)]
context._set_with_template(error_template)
error_template.render_context(context, error=error)
def serve_template(templatename, **kwargs):
interface_dir = os.path.join(str(headphones.PROG_DIR), 'data/interfaces/')
template_dir = os.path.join(str(interface_dir), headphones.CONFIG.INTERFACE)
_hplookup = TemplateLookup(directories=[template_dir])
try:
template = _hplookup.get_template(templatename)
return template.render(**kwargs)
except:
return exceptions.html_error_template().render()
def _render_error(template, context, error):
if template.error_handler:
result = template.error_handler(context, error)
if not result:
compat.reraise(*sys.exc_info())
else:
error_template = exceptions.html_error_template()
if context._outputting_as_unicode:
context._buffer_stack[:] = [
util.FastEncodingBuffer(as_unicode=True)]
else:
context._buffer_stack[:] = [util.FastEncodingBuffer(
error_template.output_encoding,
error_template.encoding_errors)]
context._set_with_template(error_template)
error_template.render_context(context, error=error)