Skip to content

use getattr in jinja2.filters.make_attrgetter instead of getitem#57

Closed
kilink wants to merge 1 commit intopallets:masterfrom
kilink:master
Closed

use getattr in jinja2.filters.make_attrgetter instead of getitem#57
kilink wants to merge 1 commit intopallets:masterfrom
kilink:master

Conversation

@kilink
Copy link
Copy Markdown

@kilink kilink commented Aug 30, 2011

IMO, jinja2.filters.make_attrgetter should use environment.getattr instead of environment.getitem, which tries Python's getattr first and then falls back to attempting to treat the object as a dict/list.

For context, we ran into an issue when trying to use the sort filter's attribute argument on a list of security-proxied objects; trying to use index notation on them raises an exception that is not caught in environment.getitem.

I've tested this change minor change locally and all of the tests pass.

@snoack
Copy link
Copy Markdown
Contributor

snoack commented Oct 27, 2011

That absolutely makes sense, in my opinion. The dot notation and the fact that filters like groupby and sort call the argument used for the lookup attribute implies that Environment.getattr() is used instead of Environment.getitem(). I am totally surprised that this is not the case, right now.

@snoack
Copy link
Copy Markdown
Contributor

snoack commented Oct 29, 2011

Unfortunately, I just discovered a problem with using getattr() instead of getitem(). If you use a value for the lookup, that isn't a string, a TypeError is raised and no item lookup is done. That's probably the reason why getitem() is used, here.

@mitsuhiko
Copy link
Copy Markdown
Contributor

You can customize how Environment.getitem works by overriding it. I would do that instead of changing the semantics of the template engine for everybody :)

@mitsuhiko mitsuhiko closed this May 19, 2013
@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.

3 participants