and compatible settings.
>>> template = Template('Hello {{ name }}!')
- >>> template.render(name='John Doe')
- u'Hello John Doe!'
-
+ >>> template.render(name='John Doe') == u'Hello John Doe!'
+ True
>>> stream = template.stream(name='John Doe')
- >>> stream.next()
- u'Hello John Doe!'
- >>> stream.next()
+ >>> next(stream) == u'Hello John Doe!'
+ True
+ >>> next(stream)
Traceback (most recent call last):
...
StopIteration
exported template variables from the Python layer:
>>> t = Template('{% macro foo() %}42{% endmacro %}23')
- >>> unicode(t.module)
- u'23'
- >>> t.module.foo()
- u'42'
+ >>> str(t.module)
+ '23'
+ >>> t.module.foo() == u'42'
+ True
"""
if self._module is not None:
return self._module
>>> from jinja2 import Environment, meta
>>> env = Environment()
>>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')
- >>> meta.find_undeclared_variables(ast)
- set(['bar'])
+ >>> meta.find_undeclared_variables(ast) == set(['bar'])
+ True
.. admonition:: Implementation
@implements_to_string
class Undefined(object):
"""The default undefined type. This undefined type can be printed and
- iterated over, but every other access will raise an :exc:`UndefinedError`:
+ iterated over, but every other access will raise an :exc:`jinja2.exceptions.UndefinedError`:
>>> foo = Undefined(name='foo')
>>> str(foo)
>>> foo + 42
Traceback (most recent call last):
...
- UndefinedError: 'foo' is undefined
+ jinja2.exceptions.UndefinedError: 'foo' is undefined
"""
__slots__ = ('_undefined_hint', '_undefined_obj', '_undefined_name',
'_undefined_exception')
@internalcode
def _fail_with_undefined_error(self, *args, **kwargs):
"""Regular callback function for undefined objects that raises an
- `UndefinedError` on call.
+ `jinja2.exceptions.UndefinedError` on call.
"""
if self._undefined_hint is None:
if self._undefined_obj is missing:
>>> foo + 42
Traceback (most recent call last):
...
- UndefinedError: 'foo' is undefined
+ jinja2.exceptions.UndefinedError: 'foo' is undefined
"""
__slots__ = ()
>>> str(foo)
Traceback (most recent call last):
...
- UndefinedError: 'foo' is undefined
+ jinja2.exceptions.UndefinedError: 'foo' is undefined
>>> not foo
Traceback (most recent call last):
...
- UndefinedError: 'foo' is undefined
+ jinja2.exceptions.UndefinedError: 'foo' is undefined
>>> foo + 42
Traceback (most recent call last):
...
- UndefinedError: 'foo' is undefined
+ jinja2.exceptions.UndefinedError: 'foo' is undefined
"""
__slots__ = ()
__iter__ = __str__ = __len__ = __nonzero__ = __eq__ = \
MAX_RANGE = 100000
#: attributes of function objects that are considered unsafe.
-UNSAFE_FUNCTION_ATTRIBUTES = set(['func_closure', 'func_code', 'func_dict',
- 'func_defaults', 'func_globals'])
+if PY2:
+ UNSAFE_FUNCTION_ATTRIBUTES = set(['func_closure', 'func_code', 'func_dict',
+ 'func_defaults', 'func_globals'])
+else:
+ # On versions > python 2 the special attributes on functions are gone,
+ # but they remain on methods and generators for whatever reason.
+ UNSAFE_FUNCTION_ATTRIBUTES = set()
+
#: unsafe method attributes. function attributes are unsafe for methods too
UNSAFE_METHOD_ATTRIBUTES = set(['im_class', 'im_func', 'im_self'])
#: unsafe generator attirbutes.
UNSAFE_GENERATOR_ATTRIBUTES = set(['gi_frame', 'gi_code'])
-# On versions > python 2 the special attributes on functions are gone,
-# but they remain on methods and generators for whatever reason.
-if not PY2:
- UNSAFE_FUNCTION_ATTRIBUTES = set()
-
import warnings
# make sure we don't warn in python 2.6 about stuff we don't care about
:meth:`~SandboxedEnvironment.is_safe_attribute` is overridden.
>>> from jinja2.sandbox import is_internal_attribute
- >>> is_internal_attribute(lambda: None, "func_code")
- True
- >>> is_internal_attribute((lambda x:x).func_code, 'co_code')
+ >>> is_internal_attribute(str, "mro")
True
>>> is_internal_attribute(str, "upper")
False
def shell_init_func():
def _compile(x):
- print env.compile(x, raw=True)
+ print(env.compile(x, raw=True))
result = {
'e': env,
'c': _compile,
def action_compile():
- print env.compile(sys.stdin.read(), raw=True)
+ print(env.compile(sys.stdin.read(), raw=True))
action_shell = script.make_shell(shell_init_func)
release = egg_info -RDb ''
[pytest]
-norecursedirs = .* *.egg *.egg-info env* artwork docs examples
+norecursedirs = .* *.egg *.egg-info env* artwork docs examples ext scripts
+addopts = --doctest-modules --ignore=setup.py