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

Check that contextfunction, etc. is True #1145

Closed
davidism opened this issue Feb 3, 2020 · 2 comments · Fixed by #1146
Closed

Check that contextfunction, etc. is True #1145

davidism opened this issue Feb 3, 2020 · 2 comments · Fixed by #1146

Comments

@davidism
Copy link
Member

davidism commented Feb 3, 2020

In order to determine if the context should be passed to a function or filter, Jinja has the contextfunction and contextfilter decorators which set corresponding attributes on the functions. However, they're checked with if getattr(f, "contextfunction", 0), which causes issues for objects that are callable and have permissive __getattr__ functions, such as Mock.

Changing everything to default to False and check is True, would be good for consistency.

The full list is contextfunction, evalcontextfunction, environmentfunction, contextfilter, evalcontextfilter, and environmentcontextfilter.

Extracted from #1139

@davidism davidism modified the milestones: 3.0.0, 2.11.2 Feb 3, 2020
nicki-krizek added a commit to nicki-krizek/jinja that referenced this issue Feb 3, 2020
Explicit checks for "is True" prevents unexpected behaviour with objects
that are callable and have permissive gettatr(), such as Mock.

Fixes pallets#1145
@mitsuhiko
Copy link
Contributor

I'm honestly not sure if it's a good idea to pass mock objects to a template in the first place. FWIW the reason 0 is used is because this code was faster on 2.x where the constant optimization was not applied yet.

@davidism
Copy link
Member Author

davidism commented Feb 3, 2020

I agree it's not, but there was another example of it coming up outside testing and it's a straightforward fix. 0 was only used in one place, False was used everywhere else already.

davidism pushed a commit to nicki-krizek/jinja that referenced this issue Feb 4, 2020
Explicit checks for "is True" prevents unexpected behaviour with objects
that are callable and have permissive gettatr(), such as Mock.

Fixes pallets#1145
@davidism davidism closed this as completed Feb 4, 2020
@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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants