File tree Expand file tree Collapse file tree 1 file changed +11
-12
lines changed Expand file tree Collapse file tree 1 file changed +11
-12
lines changed Original file line number Diff line number Diff line change @@ -498,19 +498,18 @@ def process_code_block_for_outputs_and_locals(
498498 code_start , code_end = get_code_bounds (block_lines )
499499 code = dedent ("\n " .join (block_lines [code_start :code_end ]))
500500
501- try :
502- with capture_output () as outfile :
501+ with capture_output () as outfile :
502+ try :
503503 code_locals = exec_code (code , local_vars )
504- except Exception as e :
505- if allow_exception :
506- code_locals = local_vars
507- else :
508- print (
509- f"Exception occurred while executing code block: { type (e ).__name__ } : { e } \n "
510- f"Note: Code block was:\n \n { block } "
511- )
512- print (err_msg )
513- raise
504+ except Exception as e :
505+ if allow_exception :
506+ # We print the error message here so it can be captured in `outfile`
507+ # and displayed in the output in cases where we actually expect exceptions.
508+ print (e )
509+ code_locals = local_vars
510+ else :
511+ print (f"{ err_msg } \n " f"Note: Code block was:\n \n { block } " )
512+ raise
514513
515514 new_locals = {
516515 key : value for key , value in code_locals .items () if key not in local_vars or value is not local_vars [key ]
You can’t perform that action at this time.
0 commit comments