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

Explicitly add global variables to the globals dictionary passed to exec statement when executing compiled templates. #1001

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Joschua-Conrad
Copy link

SimpleTemeplate is implemented as a compiled python code object executed using the builtin function exec(). The global variables visible in the compiled statement are passed as a dictionary. This dictionary now contains explicitly all definitions of the global context, in which exec() is called. Otherwise, specifying a variable in the URL pattern called __builtins__ leads to an empty globals() context, because exec() would not add the globals() definitions implicitly in that case. All calls of builtins in the compiled statement would lead to an exception.
See https://docs.python.org/3/library/functions.html#exec for the implicit definition of necessary global context.

…xec statement when executing compiled templates.

SimpleTemeplate is implemented as a compiled python code object executed using the builtin function exec(). The global variables visible in the compiled statement are passed as a dictionary. This dictionary now contains explicitly all definitions of the global context, in which exec() is called. Otherwise, specifying a variable in the URL pattern called __builtins__ leads to an empty globals() context, because exec() would not add the globals() definitions implicitly in that case. All calls of builtins in the compiled statement would lead to an exception.
See https://docs.python.org/3/library/functions.html#exec for the implicit definition of necessary global context.
@defnull
Copy link
Member

defnull commented Nov 6, 2017

Hmm, wouldn't this also add all variables defined on the module level (all globals) to the template, and also override variables explicitly passed to the template?

@Joschua-Conrad
Copy link
Author

Hmm yes that is true. What about updating the env dictionary from {"__builtins__":__builtins__} instead of globals()?

@defnull
Copy link
Member

defnull commented Nov 6, 2017

Or just add a line to the last update statement.

KoenigRigips added 2 commits November 9, 2017 14:31
This way, the global environment of bottle is preserved and not used as execution environment for compiled templates.
@Joschua-Conrad
Copy link
Author

I updated the sourcefile, now only the reference builtins is added to the execution environment instead of the complete globals() environment.

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.

None yet

2 participants