Commit 71743837 authored by Ayush Tiwari's avatar Ayush Tiwari Committed by Douglas

erp5_data_notebook bt5: Updated extension JupyterCompile to use globals...

erp5_data_notebook bt5: Updated extension JupyterCompile to use globals variable while running exec command
parent 76dd37ce
......@@ -16,10 +16,12 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
# Other way would be to use all the globals variables instead of just an empty
# dictionary, but that might hamper the speed of exec or eval.
# Something like -- g = globals(); g['context'] = self;
g = {}
g = globals()
g['context'] = self
result_string = None
ename, evalue, tb_list = None, None, None
# Update globals dict and use it while running exec command
g.update(old_local_variable_dict)
# IPython expects 2 status message - 'ok', 'error'
# Error would be updated only after the exec throws the error here, in all
......@@ -36,8 +38,8 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
# a=42;print ab None Syntax Error None Name Error
# From above, it infers that the invalid syntax is being handled by exec only
try:
jupyter_compiled = compile(jupyter_code, '<input>', 'eval')
eval_result = eval(jupyter_compiled, g, old_local_variable_dict)
jupyter_compiled = compile(jupyter_code, '<string>', 'eval')
eval_result = eval(jupyter_compiled, g, g)
result_string = str(eval_result)
# Trying to catch everything which results in error from eval
# It can be just an invalid syntax, invalid expression or some error
......@@ -49,8 +51,8 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
result = StringIO()
sys.stdout = result
try:
jupyter_compiled = compile(jupyter_code, '<input>', 'exec')
exec(jupyter_compiled, g, old_local_variable_dict)
jupyter_compiled = compile(jupyter_code, '<string>', 'exec')
exec(jupyter_compiled, g, g)
sys.stdout = old_stdout
result_string = result.getvalue()
......@@ -66,10 +68,10 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
result = {
'result_string': result_string,
'local_variable_dict': old_local_variable_dict,
'local_variable_dict': g,
'status': status,
'evalue': evalue,
'ename': ename,
'traceback': tb_list
'traceback': tb_list,
}
return result
......@@ -46,8 +46,8 @@
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple>
<string>W: 40, 18: Use of eval (eval-used)</string>
<string>W: 53, 6: Use of exec (exec-used)</string>
<string>W: 42, 18: Use of eval (eval-used)</string>
<string>W: 55, 6: Use of exec (exec-used)</string>
</tuple>
</value>
</item>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment