Fix doctests

Fix #427
This commit is contained in:
Markus Unterwaditzer 2015-03-22 14:22:40 +01:00
parent 1c5f1e8d85
commit 599c1360bc
6 changed files with 31 additions and 32 deletions

View File

@ -879,13 +879,12 @@ class Template(object):
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
@ -1032,10 +1031,10 @@ class Template(object):
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

View File

@ -39,8 +39,8 @@ def find_undeclared_variables(ast):
>>> 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

View File

@ -444,7 +444,7 @@ class Macro(object):
@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)
@ -454,7 +454,7 @@ class Undefined(object):
>>> 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')
@ -468,7 +468,7 @@ class Undefined(object):
@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:
@ -620,7 +620,7 @@ class DebugUndefined(Undefined):
>>> foo + 42
Traceback (most recent call last):
...
UndefinedError: 'foo' is undefined
jinja2.exceptions.UndefinedError: 'foo' is undefined
"""
__slots__ = ()
@ -645,15 +645,15 @@ class StrictUndefined(Undefined):
>>> 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__ = \

View File

@ -23,8 +23,14 @@ from jinja2._compat import string_types, PY2
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'])
@ -32,11 +38,6 @@ 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
@ -124,9 +125,7 @@ def is_internal_attribute(obj, attr):
: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

View File

@ -21,7 +21,7 @@ env = jinja2.Environment(extensions=['jinja2.ext.i18n', 'jinja2.ext.do',
def shell_init_func():
def _compile(x):
print env.compile(x, raw=True)
print(env.compile(x, raw=True))
result = {
'e': env,
'c': _compile,
@ -34,7 +34,7 @@ def shell_init_func():
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)

View File

@ -5,4 +5,5 @@ universal = 1
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