Commit 46a8e0d2 authored by Ayush Tiwari's avatar Ayush Tiwari

erp5-data-notebook bt5: Remove exception catching from Base_runJupyter...

erp5-data-notebook bt5: Remove exception catching from Base_runJupyter external function and let the errors be raised loudly
parent 84012adb
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from StringIO import StringIO from StringIO import StringIO
from IPython.utils import py3compat
from IPython.utils.py3compat import unicode_type
from Products.ERP5Type.Globals import PersistentMapping from Products.ERP5Type.Globals import PersistentMapping
import sys import sys
import traceback
import transaction
def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
""" """
...@@ -29,8 +25,10 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -29,8 +25,10 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
g.update(old_local_variable_dict) 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 # XXX: The focus is on 'ok' status only, we're letting errors to be raised on
# other cases, status would be 'ok' # erp5 for now, so as not to hinder the transactions while catching them.
# TODO: This can be refactored by using client side error handling instead of
# catching errors on server/erp5.
status = u'ok' status = u'ok'
# eval used before exec because exec can handle the error raised by both eval # eval used before exec because exec can handle the error raised by both eval
...@@ -55,24 +53,17 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -55,24 +53,17 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
old_stdout = sys.stdout old_stdout = sys.stdout
result = StringIO() result = StringIO()
sys.stdout = result sys.stdout = result
try:
jupyter_compiled = compile(jupyter_code, '<string>', 'exec')
exec(jupyter_compiled, g, g)
sys.stdout = old_stdout
result_string = result.getvalue()
# Catching exception to show it to jupyter frontend # Letting the code fail in case of error while executing the python script/code
except Exception: # XXX: Need to be refactored so to acclimitize transactions failure as well as
# Abort transaction in case of error in script # normal python code failure and show it to user on jupyter frontend.
transaction.abort() # Decided to let this fail silently in backend without letting the frontend
etype, evalue, tb = sys.exc_info() # user know the error so as to let tranasction or its error be handled by ZODB
tb_list = traceback.format_exception(etype, evalue, tb) # in uniform way instead of just using half transactions.
status = u'error' jupyter_compiled = compile(jupyter_code, '<string>', 'exec')
ename = unicode_type(etype.__name__) exec(jupyter_compiled, g, g)
evalue = py3compat.safe_unicode(evalue) sys.stdout = old_stdout
result_string = result.getvalue()
sys.stdout.flush()
sys.stderr.flush()
# Difference between the globals variable before and after exec/eval so that # Difference between the globals variable before and after exec/eval so that
# we don't have to save unnecessary variables in database which might or might # we don't have to save unnecessary variables in database which might or might
...@@ -105,4 +96,4 @@ def UpdatePersistentMapping(self, existing_dict): ...@@ -105,4 +96,4 @@ def UpdatePersistentMapping(self, existing_dict):
new_dict = PersistentMapping() new_dict = PersistentMapping()
for key, value in existing_dict.iteritems(): for key, value in existing_dict.iteritems():
new_dict[key]=value new_dict[key]=value
return new_dict return new_dict
\ No newline at end of file
...@@ -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: 47, 18: Use of eval (eval-used)</string> <string>W: 45, 18: Use of eval (eval-used)</string>
<string>W: 60, 6: Use of exec (exec-used)</string> <string>W: 64, 4: 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