Improved error message for undefineds
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 7d29ec6 · Browse files · 3 files·+34 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+34 added−9 removed
- other:+34 added−9 removed
Provenance
- Commit
98dbf5f- Tree
dbf6acb- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7d29ec6
3 files changed+34 added−9 removed
modified:jinja2/testsuite/api.py+11 added-3 removed
205 unmodified lines206 def test_indexing_gives_undefined(self):207 t = Template("{{ var[42].foo }}")208 assert_raises(UndefinedError, t.render, var=0)209 210- def test_none_gives_propert_error(self):210+ def test_none_gives_proper_error(self):211 try:212- Undefined(None).split()212+ Environment().getattr(None, 'split')213 except UndefinedError, e:214- assert e.message == 'None is not defined'214+ assert e.message == "None has no attribute 'split'"215 else:216 assert False, 'expected exception'217 218+ def test_object_repr(self):219+ try:220+ Undefined(obj=42, name='upper')221+ except UndefinedError, e:222+ assert e.message == "'int' object has no attribute 'upper'"223+ else:224+ assert False, 'expected exception'225 226+227 def suite():228 suite = unittest.TestSuite()229 suite.addTest(unittest.makeSuite(ExtendedAPITestCase))230 suite.addTest(unittest.makeSuite(MetaTestCase))2 unmodified linesmodified:jinja2/runtime.py+7 added-6 removed
10 unmodified lines11 import sys12 from itertools import chain, imap13 from jinja2.nodes import EvalContext14 from jinja2.utils import Markup, partial, soft_unicode, escape, missing, \15- concat, MethodType, FunctionType, internalcode, next15+ concat, MethodType, FunctionType, internalcode, next, \16+ object_type_repr17 from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \18 TemplateNotFound19 20 416 unmodified lines437 if self._undefined_hint is None:438 if self._undefined_obj is missing:439 hint = '%r is undefined' % self._undefined_name440 elif not isinstance(self._undefined_name, basestring):440- hint = '%r object has no element %r' % (441- self._undefined_obj.__class__.__name__,441+ hint = '%s has no element %r' % (442+ object_type_repr(self._undefined_obj),443 self._undefined_name444 )445 else:445- hint = '%r object has no attribute %r' % (446- self._undefined_obj.__class__.__name__,446+ hint = '%r has no attribute %r' % (447+ object_type_repr(self._undefined_obj),448 self._undefined_name449 )450 else:451 hint = self._undefined_hint49 unmodified lines501 if self._undefined_hint is None:502 if self._undefined_obj is missing:503 return u'{{ %s }}' % self._undefined_name504 return '{{ no such element: %s[%r] }}' % (504- self._undefined_obj.__class__.__name__,505+ object_type_repr(self._undefined_obj),506 self._undefined_name507 )508 return u'{{ undefined value printed: %s }}' % self._undefined_hint509 28 unmodified linesmodified:jinja2/utils.py+16 added
225 unmodified lines226 if e.errno not in (errno.ENOENT, errno.EISDIR):227 raise228 229 230+def object_type_repr(obj):231+ """Returns the name of the object's type. For some recognized232+ singletons the name of the object is returned instead. (For233+ example for `None` and `Ellipsis`).234+ """235+ if obj is None:236+ return 'None'237+ elif obj is Ellipsis:238+ return 'Ellipsis'239+ if obj.__class__.__module__ == '__builtin__':240+ name = obj.__name__241+ else:242+ name = obj.__class__.module__ + '.' + obj.__name__243+ return '%s object' % name244+245+246 def pformat(obj, verbose=False):247 """Prettyprint an object. Either use the `pretty` library or the248 builtin `pprint`.249 """569 unmodified lines