Permalink
Browse files

Undefineds give better hints for `None` objects now.

--HG--
branch : trunk
  • Loading branch information...
1 parent 2e3c9c7 commit 4855908121db1f033740c830747d050f8f8a7c51 @mitsuhiko mitsuhiko committed Apr 12, 2010
Showing with 10 additions and 3 deletions.
  1. +3 −3 jinja2/runtime.py
  2. +7 −0 jinja2/testsuite/api.py
View
@@ -422,7 +422,7 @@ class Undefined(object):
__slots__ = ('_undefined_hint', '_undefined_obj', '_undefined_name',
'_undefined_exception')
- def __init__(self, hint=None, obj=None, name=None, exc=UndefinedError):
+ def __init__(self, hint=None, obj=missing, name=None, exc=UndefinedError):
self._undefined_hint = hint
self._undefined_obj = obj
self._undefined_name = name
@@ -434,7 +434,7 @@ def _fail_with_undefined_error(self, *args, **kwargs):
`UndefinedError` on call.
"""
if self._undefined_hint is None:
- if self._undefined_obj is None:
+ if self._undefined_obj is missing:
hint = '%r is undefined' % self._undefined_name
elif not isinstance(self._undefined_name, basestring):
hint = '%r object has no element %r' % (
@@ -498,7 +498,7 @@ class DebugUndefined(Undefined):
def __unicode__(self):
if self._undefined_hint is None:
- if self._undefined_obj is None:
+ if self._undefined_obj is missing:
return u'{{ %s }}' % self._undefined_name
return '{{ no such element: %s[%r] }}' % (
self._undefined_obj.__class__.__name__,
View
@@ -207,6 +207,13 @@ def test_indexing_gives_undefined(self):
t = Template("{{ var[42].foo }}")
assert_raises(UndefinedError, t.render, var=0)
+ def test_none_gives_propert_error(self):
+ try:
+ Undefined(None).split()
+ except UndefinedError, e:
+ assert e.message == 'None is not defined'
+ else:
+ assert False, 'expected exception'
def suite():

0 comments on commit 4855908

Please sign in to comment.