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

Prevent scope lookup from accidentally matching callable builtins #118

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

echuber2
Copy link

Fixes #117

The current lookup by scope has false positives when the variable in the template coincidentally matches some Python builtin on the type. For example, {{title}} matches str.title when it shouldn't. This fix detects when the attribute found in the scope is callable and raises an exception to continue as normal. This fix does not break the intended ability to have callable lambdas in the template.

Before the fix:

>>> import chevron ; chevron.render('{{#title}}{{title}} abc{{/title}}', {'title': 'bob'})
'<built-in method title of str object at 0x7f2dadea95f0> abc'

(Here, it found a 'title' attribute on 'bob' and got messed up.)

After the fix:

>>> import chevron ; chevron.render('{{#title}}{{title}} abc{{/title}}', {'title': 'bob'})
'bob abc'

Callable functionality is still working fine:

>>> import chevron ; chevron.render('{{#bar}}{{foo}} abc{{/bar}}', {'foo': 'bob', 'bar': lambda text,render: render("rendered:"+text)})
'rendered:bob abc'

The lambda example from the chevron readme still works as before too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interpolation when the key mirrors a built-in str method
1 participant