Commit 13bc9cc6 authored by Ayush Tiwari's avatar Ayush Tiwari

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 bbae0fe5
...@@ -16,10 +16,12 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -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 # 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. # dictionary, but that might hamper the speed of exec or eval.
# Something like -- g = globals(); g['context'] = self; # Something like -- g = globals(); g['context'] = self;
g = {} g = globals()
g['context'] = self g['context'] = self
result_string = None result_string = None
ename, evalue, tb_list = None, None, 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' # IPython expects 2 status message - 'ok', 'error'
# Error would be updated only after the exec throws the error here, in all # 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): ...@@ -36,8 +38,8 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
# a=42;print ab None Syntax Error None Name Error # a=42;print ab None Syntax Error None Name Error
# From above, it infers that the invalid syntax is being handled by exec only # From above, it infers that the invalid syntax is being handled by exec only
try: try:
jupyter_compiled = compile(jupyter_code, '<input>', 'eval') jupyter_compiled = compile(jupyter_code, '<string>', 'eval')
eval_result = eval(jupyter_compiled, g, old_local_variable_dict) eval_result = eval(jupyter_compiled, g, g)
result_string = str(eval_result) result_string = str(eval_result)
# Trying to catch everything which results in error from eval # Trying to catch everything which results in error from eval
# It can be just an invalid syntax, invalid expression or some error # 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): ...@@ -49,8 +51,8 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
result = StringIO() result = StringIO()
sys.stdout = result sys.stdout = result
try: try:
jupyter_compiled = compile(jupyter_code, '<input>', 'exec') jupyter_compiled = compile(jupyter_code, '<string>', 'exec')
exec(jupyter_compiled, g, old_local_variable_dict) exec(jupyter_compiled, g, g)
sys.stdout = old_stdout sys.stdout = old_stdout
result_string = result.getvalue() result_string = result.getvalue()
...@@ -66,10 +68,10 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -66,10 +68,10 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
result = { result = {
'result_string': result_string, 'result_string': result_string,
'local_variable_dict': old_local_variable_dict, 'local_variable_dict': g,
'status': status, 'status': status,
'evalue': evalue, 'evalue': evalue,
'ename': ename, 'ename': ename,
'traceback': tb_list 'traceback': tb_list,
} }
return result return result
...@@ -46,8 +46,8 @@ ...@@ -46,8 +46,8 @@
<key> <string>text_content_warning_message</string> </key> <key> <string>text_content_warning_message</string> </key>
<value> <value>
<tuple> <tuple>
<string>W: 40, 18: Use of eval (eval-used)</string> <string>W: 42, 18: Use of eval (eval-used)</string>
<string>W: 53, 6: Use of exec (exec-used)</string> <string>W: 55, 6: Use of exec (exec-used)</string>
</tuple> </tuple>
</value> </value>
</item> </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