From 4855908121db1f033740c830747d050f8f8a7c51 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 12 Apr 2010 13:51:33 +0200 Subject: [PATCH] Undefineds give better hints for `None` objects now. --HG-- branch : trunk --- jinja2/runtime.py | 6 +++--- jinja2/testsuite/api.py | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/jinja2/runtime.py b/jinja2/runtime.py index fef7b25..16389e0 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -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 @@ class Undefined(object): `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__, diff --git a/jinja2/testsuite/api.py b/jinja2/testsuite/api.py index 4b49819..4134d26 100644 --- a/jinja2/testsuite/api.py +++ b/jinja2/testsuite/api.py @@ -207,6 +207,13 @@ class UndefinedTestCase(JinjaTestCase): 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(): -- 2.26.2