blob: 17a599695178ba624e3cefa1e1116153e5c4116b [file] [log] [blame]
Armin Ronachere791c2a2008-04-07 18:39:54 +02001# -*- coding: utf-8 -*-
2"""
3 jinja2.runtime
4 ~~~~~~~~~~~~~~
5
6 Runtime helpers.
7
8 :copyright: Copyright 2008 by Armin Ronacher.
9 :license: GNU GPL.
10"""
Armin Ronacher1ae4fdf2008-04-28 20:49:51 +020011import sys
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020012from types import FunctionType
Armin Ronacherd1342312008-04-28 12:20:12 +020013from itertools import chain, imap
Armin Ronacher7ceced52008-05-03 10:15:31 +020014from jinja2.utils import Markup, partial, soft_unicode, escape, missing, concat
Armin Ronacherd1342312008-04-28 12:20:12 +020015from jinja2.exceptions import UndefinedError, TemplateRuntimeError
Armin Ronachere791c2a2008-04-07 18:39:54 +020016
17
Armin Ronacher2feed1d2008-04-26 16:26:52 +020018# these variables are exported to the template runtime
Armin Ronacher19cf9c22008-05-01 12:49:53 +020019__all__ = ['LoopContext', 'Context', 'TemplateReference', 'Macro', 'Markup',
20 'TemplateRuntimeError', 'missing', 'concat', 'escape',
Armin Ronacherd1342312008-04-28 12:20:12 +020021 'markup_join', 'unicode_join']
Armin Ronacher0611e492008-04-25 23:44:14 +020022
23
Armin Ronacherdc02b642008-05-15 22:47:27 +020024def markup_join(seq):
Armin Ronacherd1342312008-04-28 12:20:12 +020025 """Concatenation that escapes if necessary and converts to unicode."""
26 buf = []
Armin Ronacherdc02b642008-05-15 22:47:27 +020027 iterator = imap(soft_unicode, seq)
Armin Ronacherd1342312008-04-28 12:20:12 +020028 for arg in iterator:
29 buf.append(arg)
30 if hasattr(arg, '__html__'):
31 return Markup(u'').join(chain(buf, iterator))
32 return concat(buf)
33
34
Armin Ronacherdc02b642008-05-15 22:47:27 +020035def unicode_join(seq):
Armin Ronacherd1342312008-04-28 12:20:12 +020036 """Simple args to unicode conversion and concatenation."""
Armin Ronacherdc02b642008-05-15 22:47:27 +020037 return concat(imap(unicode, seq))
Armin Ronacherd1342312008-04-28 12:20:12 +020038
39
Armin Ronacher19cf9c22008-05-01 12:49:53 +020040class Context(object):
Armin Ronacher7259c762008-04-30 13:03:59 +020041 """The template context holds the variables of a template. It stores the
42 values passed to the template and also the names the template exports.
43 Creating instances is neither supported nor useful as it's created
44 automatically at various stages of the template evaluation and should not
45 be created by hand.
Armin Ronacherc9705c22008-04-27 21:28:03 +020046
Armin Ronacher7259c762008-04-30 13:03:59 +020047 The context is immutable. Modifications on :attr:`parent` **must not**
48 happen and modifications on :attr:`vars` are allowed from generated
49 template code only. Template filters and global functions marked as
50 :func:`contextfunction`\s get the active context passed as first argument
51 and are allowed to access the context read-only.
52
53 The template context supports read only dict operations (`get`,
Armin Ronacherf35e2812008-05-06 16:04:10 +020054 `keys`, `values`, `items`, `iterkeys`, `itervalues`, `iteritems`,
55 `__getitem__`, `__contains__`). Additionally there is a :meth:`resolve`
56 method that doesn't fail with a `KeyError` but returns an
57 :class:`Undefined` object for missing variables.
Armin Ronacher9706fab2008-04-08 18:49:56 +020058 """
Armin Ronachere791c2a2008-04-07 18:39:54 +020059
Armin Ronacher203bfcb2008-04-24 21:54:44 +020060 def __init__(self, environment, parent, name, blocks):
61 self.parent = parent
Armin Ronacher2feed1d2008-04-26 16:26:52 +020062 self.vars = vars = {}
Armin Ronacherc63243e2008-04-14 22:53:58 +020063 self.environment = environment
Armin Ronacher203bfcb2008-04-24 21:54:44 +020064 self.exported_vars = set()
Armin Ronacher68f77672008-04-17 11:50:39 +020065 self.name = name
Armin Ronacher203bfcb2008-04-24 21:54:44 +020066
67 # bind functions to the context of environment if required
Armin Ronacher2feed1d2008-04-26 16:26:52 +020068 for name, obj in parent.iteritems():
Armin Ronacher203bfcb2008-04-24 21:54:44 +020069 if type(obj) is FunctionType:
70 if getattr(obj, 'contextfunction', 0):
Armin Ronacher2feed1d2008-04-26 16:26:52 +020071 vars[name] = partial(obj, self)
Armin Ronacher203bfcb2008-04-24 21:54:44 +020072 elif getattr(obj, 'environmentfunction', 0):
Armin Ronacher2feed1d2008-04-26 16:26:52 +020073 vars[name] = partial(obj, environment)
Armin Ronacher203bfcb2008-04-24 21:54:44 +020074
75 # create the initial mapping of blocks. Whenever template inheritance
76 # takes place the runtime will update this mapping with the new blocks
77 # from the template.
Armin Ronacher75cfb862008-04-11 13:47:22 +020078 self.blocks = dict((k, [v]) for k, v in blocks.iteritems())
Armin Ronacherf059ec12008-04-11 22:21:00 +020079
Armin Ronacher203bfcb2008-04-24 21:54:44 +020080 def super(self, name, current):
Armin Ronacher62f8a292008-04-13 23:18:05 +020081 """Render a parent block."""
Armin Ronacherc9705c22008-04-27 21:28:03 +020082 try:
83 blocks = self.blocks[name]
Armin Ronacher83fbc0f2008-05-15 12:22:28 +020084 block = blocks[blocks.index(current) + 1]
Armin Ronacherc9705c22008-04-27 21:28:03 +020085 except LookupError:
Armin Ronacher9a822052008-04-17 18:44:07 +020086 return self.environment.undefined('there is no parent block '
Armin Ronacher19cf9c22008-05-01 12:49:53 +020087 'called %r.' % name,
88 name='super')
Armin Ronacherd1342312008-04-28 12:20:12 +020089 wrap = self.environment.autoescape and Markup or (lambda x: x)
Armin Ronacher83fbc0f2008-05-15 12:22:28 +020090 render = lambda: wrap(concat(block(self)))
Armin Ronacherc9705c22008-04-27 21:28:03 +020091 render.__name__ = render.name = name
92 return render
Armin Ronachere791c2a2008-04-07 18:39:54 +020093
Armin Ronacher53042292008-04-26 18:30:19 +020094 def get(self, key, default=None):
Armin Ronacher7259c762008-04-30 13:03:59 +020095 """Returns an item from the template context, if it doesn't exist
96 `default` is returned.
97 """
Armin Ronacherf35e2812008-05-06 16:04:10 +020098 try:
99 return self[key]
100 except KeyError:
101 return default
102
103 def resolve(self, key):
104 """Looks up a variable like `__getitem__` or `get` but returns an
105 :class:`Undefined` object with the name of the name looked up.
106 """
Armin Ronacher53042292008-04-26 18:30:19 +0200107 if key in self.vars:
108 return self.vars[key]
109 if key in self.parent:
110 return self.parent[key]
Armin Ronacherf35e2812008-05-06 16:04:10 +0200111 return self.environment.undefined(name=key)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200112
Armin Ronacher9706fab2008-04-08 18:49:56 +0200113 def get_exported(self):
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200114 """Get a new dict with the exported variables."""
Armin Ronacherc9705c22008-04-27 21:28:03 +0200115 return dict((k, self.vars[k]) for k in self.exported_vars)
Armin Ronacher9706fab2008-04-08 18:49:56 +0200116
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200117 def get_all(self):
Armin Ronacher7259c762008-04-30 13:03:59 +0200118 """Return a copy of the complete context as dict including the
119 global variables.
120 """
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200121 return dict(self.parent, **self.vars)
122
Armin Ronacherf35e2812008-05-06 16:04:10 +0200123 def _all(meth):
Armin Ronachere9411b42008-05-15 16:22:07 +0200124 proxy = lambda self: getattr(self.get_all(), meth)()
Armin Ronacherf35e2812008-05-06 16:04:10 +0200125 proxy.__doc__ = getattr(dict, meth).__doc__
126 proxy.__name__ = meth
127 return proxy
128
129 keys = _all('keys')
130 values = _all('values')
131 items = _all('items')
132 iterkeys = _all('iterkeys')
133 itervalues = _all('itervalues')
134 iteritems = _all('iteritems')
135 del _all
136
Armin Ronacherb5124e62008-04-25 00:36:14 +0200137 def __contains__(self, name):
138 return name in self.vars or name in self.parent
139
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200140 def __getitem__(self, key):
Armin Ronacherf35e2812008-05-06 16:04:10 +0200141 """Lookup a variable or raise `KeyError`."""
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200142 if key in self.vars:
143 return self.vars[key]
Armin Ronacherf35e2812008-05-06 16:04:10 +0200144 return self.parent[key]
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200145
Armin Ronacherf059ec12008-04-11 22:21:00 +0200146 def __repr__(self):
147 return '<%s %s of %r>' % (
148 self.__class__.__name__,
Armin Ronacher963f97d2008-04-25 11:44:59 +0200149 repr(self.get_all()),
Armin Ronacher68f77672008-04-17 11:50:39 +0200150 self.name
Armin Ronacherf059ec12008-04-11 22:21:00 +0200151 )
152
153
Armin Ronacherc9705c22008-04-27 21:28:03 +0200154class TemplateReference(object):
155 """The `self` in templates."""
Armin Ronacher62f8a292008-04-13 23:18:05 +0200156
Armin Ronacherc9705c22008-04-27 21:28:03 +0200157 def __init__(self, context):
158 self.__context = context
Armin Ronacher62f8a292008-04-13 23:18:05 +0200159
Armin Ronacherc9705c22008-04-27 21:28:03 +0200160 def __getitem__(self, name):
161 func = self.__context.blocks[name][-1]
Armin Ronacherd1342312008-04-28 12:20:12 +0200162 wrap = self.__context.environment.autoescape and \
163 Markup or (lambda x: x)
164 render = lambda: wrap(concat(func(self.__context)))
Armin Ronacherc9705c22008-04-27 21:28:03 +0200165 render.__name__ = render.name = name
166 return render
Armin Ronacher62f8a292008-04-13 23:18:05 +0200167
168 def __repr__(self):
169 return '<%s %r>' % (
170 self.__class__.__name__,
Armin Ronacherc9705c22008-04-27 21:28:03 +0200171 self._context.name
Armin Ronacher62f8a292008-04-13 23:18:05 +0200172 )
173
174
Armin Ronacher32a910f2008-04-26 23:21:03 +0200175class LoopContext(object):
176 """A loop context for dynamic iteration."""
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200177
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200178 def __init__(self, iterable, enforce_length=False, recurse=None):
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200179 self._iterable = iterable
Armin Ronacher32a910f2008-04-26 23:21:03 +0200180 self._next = iter(iterable).next
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200181 self._length = None
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200182 self._recurse = recurse
Armin Ronacher32a910f2008-04-26 23:21:03 +0200183 self.index0 = -1
184 if enforce_length:
185 len(self)
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200186
Armin Ronacher2e9396b2008-04-16 14:21:57 +0200187 def cycle(self, *args):
188 """A replacement for the old ``{% cycle %}`` tag."""
189 if not args:
190 raise TypeError('no items for cycling given')
191 return args[self.index0 % len(args)]
192
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200193 first = property(lambda x: x.index0 == 0)
194 last = property(lambda x: x.revindex0 == 0)
195 index = property(lambda x: x.index0 + 1)
Armin Ronacher10f3ba22008-04-18 11:30:37 +0200196 revindex = property(lambda x: x.length - x.index0)
197 revindex0 = property(lambda x: x.length - x.index)
Armin Ronacher2e9396b2008-04-16 14:21:57 +0200198
199 def __len__(self):
200 return self.length
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200201
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200202 def __iter__(self):
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200203 return self
204
Armin Ronacher66a93442008-05-11 23:42:19 +0200205 def loop(self, iterable):
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200206 if self._recurse is None:
207 raise TypeError('Tried to call non recursive loop. Maybe you '
Armin Ronacher66a93442008-05-11 23:42:19 +0200208 "forgot the 'recursive' modifier.")
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200209 return self._recurse(iterable, self._recurse)
210
Armin Ronacher66a93442008-05-11 23:42:19 +0200211 # a nifty trick to enhance the error message if someone tried to call
212 # the the loop without or with too many arguments.
213 __call__ = loop; del loop
214
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200215 def next(self):
216 self.index0 += 1
217 return self._next(), self
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200218
Armin Ronacher2e9396b2008-04-16 14:21:57 +0200219 @property
220 def length(self):
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200221 if self._length is None:
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200222 try:
223 length = len(self._iterable)
224 except TypeError:
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200225 self._iterable = tuple(self._iterable)
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200226 self._next = iter(self._iterable).next
227 length = len(tuple(self._iterable)) + self.index0 + 1
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200228 self._length = length
229 return self._length
230
Armin Ronacher2e9396b2008-04-16 14:21:57 +0200231 def __repr__(self):
Armin Ronacherc9705c22008-04-27 21:28:03 +0200232 return '<%s %r/%r>' % (
233 self.__class__.__name__,
234 self.index,
235 self.length
236 )
Armin Ronacher2e9396b2008-04-16 14:21:57 +0200237
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200238
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200239class Macro(object):
Armin Ronacherd55ab532008-04-09 16:13:39 +0200240 """Wraps a macro."""
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200241
Armin Ronacher963f97d2008-04-25 11:44:59 +0200242 def __init__(self, environment, func, name, arguments, defaults,
243 catch_kwargs, catch_varargs, caller):
Armin Ronacherc63243e2008-04-14 22:53:58 +0200244 self._environment = environment
Armin Ronacher71082072008-04-12 14:19:36 +0200245 self._func = func
Armin Ronacherd84ec462008-04-29 13:43:16 +0200246 self._argument_count = len(arguments)
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200247 self.name = name
248 self.arguments = arguments
249 self.defaults = defaults
Armin Ronacher963f97d2008-04-25 11:44:59 +0200250 self.catch_kwargs = catch_kwargs
251 self.catch_varargs = catch_varargs
Armin Ronacher71082072008-04-12 14:19:36 +0200252 self.caller = caller
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200253
254 def __call__(self, *args, **kwargs):
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200255 arguments = []
Armin Ronacher9706fab2008-04-08 18:49:56 +0200256 for idx, name in enumerate(self.arguments):
257 try:
258 value = args[idx]
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200259 except:
Armin Ronacher9706fab2008-04-08 18:49:56 +0200260 try:
261 value = kwargs.pop(name)
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200262 except:
Armin Ronacher9706fab2008-04-08 18:49:56 +0200263 try:
Armin Ronacherd84ec462008-04-29 13:43:16 +0200264 value = self.defaults[idx - self._argument_count]
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200265 except:
Armin Ronacher9a822052008-04-17 18:44:07 +0200266 value = self._environment.undefined(
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200267 'parameter %r was not provided' % name, name=name)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200268 arguments.append(value)
269
270 # it's important that the order of these arguments does not change
271 # if not also changed in the compiler's `function_scoping` method.
272 # the order is caller, keyword arguments, positional arguments!
Armin Ronacher71082072008-04-12 14:19:36 +0200273 if self.caller:
274 caller = kwargs.pop('caller', None)
275 if caller is None:
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200276 caller = self._environment.undefined('No caller defined',
277 name='caller')
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200278 arguments.append(caller)
Armin Ronacher963f97d2008-04-25 11:44:59 +0200279 if self.catch_kwargs:
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200280 arguments.append(kwargs)
Armin Ronacher963f97d2008-04-25 11:44:59 +0200281 elif kwargs:
282 raise TypeError('macro %r takes no keyword argument %r' %
283 (self.name, iter(kwargs).next()))
284 if self.catch_varargs:
Armin Ronacherd84ec462008-04-29 13:43:16 +0200285 arguments.append(args[self._argument_count:])
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200286 elif len(args) > self._argument_count:
287 raise TypeError('macro %r takes not more than %d argument(s)' %
288 (self.name, len(self.arguments)))
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200289 return self._func(*arguments)
Armin Ronacher71082072008-04-12 14:19:36 +0200290
291 def __repr__(self):
292 return '<%s %s>' % (
293 self.__class__.__name__,
294 self.name is None and 'anonymous' or repr(self.name)
295 )
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200296
297
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200298def fail_with_undefined_error(self, *args, **kwargs):
299 """Regular callback function for undefined objects that raises an
300 `UndefinedError` on call.
301 """
302 if self._undefined_hint is None:
303 if self._undefined_obj is None:
304 hint = '%r is undefined' % self._undefined_name
305 elif not isinstance(self._undefined_name, basestring):
306 hint = '%r object has no element %r' % (
307 self._undefined_obj.__class__.__name__,
308 self._undefined_name
309 )
310 else:
311 hint = '%r object has no attribute %r' % (
312 self._undefined_obj.__class__.__name__,
313 self._undefined_name
314 )
315 else:
316 hint = self._undefined_hint
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200317 raise self._undefined_exception(hint)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200318
319
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200320class Undefined(object):
Armin Ronacherd1342312008-04-28 12:20:12 +0200321 """The default undefined type. This undefined type can be printed and
322 iterated over, but every other access will raise an :exc:`UndefinedError`:
323
324 >>> foo = Undefined(name='foo')
325 >>> str(foo)
326 ''
327 >>> not foo
328 True
329 >>> foo + 42
330 Traceback (most recent call last):
331 ...
332 jinja2.exceptions.UndefinedError: 'foo' is undefined
Armin Ronacherc63243e2008-04-14 22:53:58 +0200333 """
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200334 __slots__ = ('_undefined_hint', '_undefined_obj', '_undefined_name',
335 '_undefined_exception')
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200336
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200337 def __init__(self, hint=None, obj=None, name=None, exc=UndefinedError):
Armin Ronacher9a822052008-04-17 18:44:07 +0200338 self._undefined_hint = hint
339 self._undefined_obj = obj
340 self._undefined_name = name
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200341 self._undefined_exception = exc
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200342
Armin Ronacherc63243e2008-04-14 22:53:58 +0200343 __add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \
344 __realdiv__ = __rrealdiv__ = __floordiv__ = __rfloordiv__ = \
345 __mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \
Armin Ronacher53042292008-04-26 18:30:19 +0200346 __getattr__ = __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = \
347 fail_with_undefined_error
Armin Ronacherc63243e2008-04-14 22:53:58 +0200348
349 def __str__(self):
350 return self.__unicode__().encode('utf-8')
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200351
352 def __repr__(self):
Priit Laes4149a0e2008-04-17 19:04:44 +0200353 return 'Undefined'
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200354
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200355 def __unicode__(self):
356 return u''
357
Armin Ronacherd1d2f3d2008-04-09 14:02:55 +0200358 def __len__(self):
359 return 0
360
361 def __iter__(self):
362 if 0:
363 yield None
Armin Ronacherc63243e2008-04-14 22:53:58 +0200364
365 def __nonzero__(self):
366 return False
367
368
369class DebugUndefined(Undefined):
Armin Ronacherd1342312008-04-28 12:20:12 +0200370 """An undefined that returns the debug info when printed.
371
372 >>> foo = DebugUndefined(name='foo')
373 >>> str(foo)
374 '{{ foo }}'
375 >>> not foo
376 True
377 >>> foo + 42
378 Traceback (most recent call last):
379 ...
380 jinja2.exceptions.UndefinedError: 'foo' is undefined
381 """
Armin Ronacher53042292008-04-26 18:30:19 +0200382 __slots__ = ()
Armin Ronacherc63243e2008-04-14 22:53:58 +0200383
384 def __unicode__(self):
Armin Ronacher9a822052008-04-17 18:44:07 +0200385 if self._undefined_hint is None:
386 if self._undefined_obj is None:
387 return u'{{ %s }}' % self._undefined_name
388 return '{{ no such element: %s[%r] }}' % (
389 self._undefined_obj.__class__.__name__,
390 self._undefined_name
391 )
392 return u'{{ undefined value printed: %s }}' % self._undefined_hint
Armin Ronacherc63243e2008-04-14 22:53:58 +0200393
394
395class StrictUndefined(Undefined):
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200396 """An undefined that barks on print and iteration as well as boolean
Armin Ronacher53042292008-04-26 18:30:19 +0200397 tests and all kinds of comparisons. In other words: you can do nothing
398 with it except checking if it's defined using the `defined` test.
Armin Ronacherd1342312008-04-28 12:20:12 +0200399
400 >>> foo = StrictUndefined(name='foo')
401 >>> str(foo)
402 Traceback (most recent call last):
403 ...
404 jinja2.exceptions.UndefinedError: 'foo' is undefined
405 >>> not foo
406 Traceback (most recent call last):
407 ...
408 jinja2.exceptions.UndefinedError: 'foo' is undefined
409 >>> foo + 42
410 Traceback (most recent call last):
411 ...
412 jinja2.exceptions.UndefinedError: 'foo' is undefined
Priit Laes4149a0e2008-04-17 19:04:44 +0200413 """
Armin Ronacher53042292008-04-26 18:30:19 +0200414 __slots__ = ()
415 __iter__ = __unicode__ = __len__ = __nonzero__ = __eq__ = __ne__ = \
416 fail_with_undefined_error
Armin Ronacherc63243e2008-04-14 22:53:58 +0200417
Armin Ronacher53042292008-04-26 18:30:19 +0200418
419# remove remaining slots attributes, after the metaclass did the magic they
420# are unneeded and irritating as they contain wrong data for the subclasses.
421del Undefined.__slots__, DebugUndefined.__slots__, StrictUndefined.__slots__