Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: avoid assumption that all objects provide __call__ #502

Merged
merged 1 commit into from
Jul 7, 2017

Conversation

berrange
Copy link
Contributor

@berrange berrange commented Oct 13, 2015

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

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

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

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
@ThiefMaster
Copy link
Member

just a small heads-up: you can use fenced code blocks (triple backticks before and after) to format something as code on github. right now your demo code in the PR's description is not very readable

@berrange
Copy link
Contributor Author

@ThiefMaster all the text in the PR was just automatically included by github from the commit message berrange@91255f8

@davidism
Copy link
Member

davidism commented Jul 7, 2017

@mitsuhiko checking if the callable object is a FunctionType or MethodType seems unnecessary. Removing it doesn't break any tests and allows callable C objects to mark themselves as context functions too by setting a class attribute instead of a function attribute.

Is there a reason not to remove the isinstance(obj, _context_function_types) check?

@davidism davidism merged commit 07c0b87 into pallets:master Jul 7, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants