Skip to content
Myr

runtime: avoid assumption that all objects provide __call__

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Objects which are backed by native extensions do not provide a __call__ attribute, but are none the less callable if the native extension provides a 'tp_call' implementation. The jinja2.runtime.Context.call method unconditionally access the '__call__' attribute causing an exception to be raised if the object was a native extension method. A demo of the problem can be seen using PyGObject: $ cat demo.py #!/usr/bin/python from gi.repository import Gio from jinja2 import Environment f = Gio.File.new_for_path("/some/file.txt") print f.get_uri() t = Environment().from_string("""{{f.get_uri()}}""") print t.render(f=f) Which when run results in $ ./demo.py file:///some/file.txt Traceback (most recent call last): File "./demo.py", line 10, in <module> print t.render(f=f) File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render return self.environment.handle_exception(exc_info, True) File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception reraise(exc_type, exc_value, tb) File "<template>", line 1, in top-level template code AttributeError: 'gi.FunctionInfo' object has no attribute '__call__' After this patch it produces $ ./demo.py file:///some/file.txt file:///some/file.txt

authored by Daniel P. Berrange · committed by Daniel P. Berrange · parent 9b4b20a · Browse files · 1 file+8 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Daniel P. Berrange <berrange@redhat.com>

Diff stats

1 file+8 added−7 removed

  • other:+8 added−7 removed

1 file+8 added−7 removed

modified:jinja2/runtime.py+8 added-7 removed
175 unmodified lines
176 if __debug__:177 __traceback_hide__ = True # noqa178179 # Allow callable classes to take a context180 if hasattr(__obj, '__call__'):180 fn = __obj.__call__181 for fn_type in ('contextfunction',182 'evalcontextfunction',183 'environmentfunction'):184 if hasattr(fn, fn_type):185 __obj = fn186 break181 fn = __obj.__call__182 for fn_type in ('contextfunction',183 'evalcontextfunction',184 'environmentfunction'):185 if hasattr(fn, fn_type):186 __obj = fn187 break188189 if isinstance(__obj, _context_function_types):190 if getattr(__obj, 'contextfunction', 0):191 args = (__self,) + args
477 unmodified lines