220
220
# for tokenizing blocks
221
221
COMMENT , INPUT , OUTPUT = range (3 )
222
222
223
- PSEUDO_DECORATORS = ["suppress" , "verbatim" , "savefig" , "doctest" ]
223
+ PSEUDO_DECORATORS = ["suppress" , "verbatim" , "savefig" , "doctest" , "okexcept" ]
224
224
225
225
#-----------------------------------------------------------------------------
226
226
# Functions and class declarations
@@ -443,6 +443,16 @@ def process_image(self, decorator):
443
443
image_directive = '\n ' .join (imagerows )
444
444
return image_file , image_directive
445
445
446
+ @staticmethod
447
+ def no_traceback_handler (shell , etype , value , tb , tb_offset = None ):
448
+ """
449
+ Exception handler that shows only the exception without the traceback.
450
+ """
451
+ shell .InteractiveTB .color_toggle ()
452
+ stb = shell .InteractiveTB .get_exception_only (etype , value )
453
+ print (shell .InteractiveTB .stb2text (stb ))
454
+ return stb
455
+
446
456
# Callbacks for each type of token
447
457
def process_input (self , data , input_prompt , lineno ):
448
458
"""
@@ -453,14 +463,20 @@ def process_input(self, data, input_prompt, lineno):
453
463
image_file = None
454
464
image_directive = None
455
465
456
- is_verbatim = decorator == '@verbatim' or self .is_verbatim
457
- is_doctest = (decorator is not None and \
458
- decorator .startswith ('@doctest' )) or self .is_doctest
459
- is_suppress = decorator == '@suppress' or self .is_suppress
460
- is_okexcept = decorator == '@okexcept' or self .is_okexcept
461
- is_okwarning = decorator == '@okwarning' or self .is_okwarning
462
- is_savefig = decorator is not None and \
463
- decorator .startswith ('@savefig' )
466
+ is_verbatim = decorator == "@verbatim" or self .is_verbatim
467
+ is_doctest = (
468
+ decorator is not None and decorator .startswith ("@doctest" )
469
+ ) or self .is_doctest
470
+ is_suppress = decorator == "@suppress" or self .is_suppress
471
+ is_okexcept = (
472
+ decorator is not None and decorator .startswith ("@okexcept" )
473
+ ) or self .is_okexcept
474
+ no_traceback = (
475
+ decorator is not None
476
+ and decorator .partition (" " )[2 ].startswith ("no_traceback" )
477
+ ) or self .no_traceback
478
+ is_okwarning = decorator == "@okwarning" or self .is_okwarning
479
+ is_savefig = decorator is not None and decorator .startswith ("@savefig" )
464
480
465
481
input_lines = input .split ('\n ' )
466
482
if len (input_lines ) > 1 :
@@ -494,7 +510,11 @@ def process_input(self, data, input_prompt, lineno):
494
510
self .IP .execution_count += 1 # increment it anyway
495
511
else :
496
512
# only submit the line in non-verbatim mode
513
+ if no_traceback :
514
+ self .IP .set_custom_exc ((BaseException ,), self .no_traceback_handler )
497
515
self .process_input_lines (input_lines , store_history = store_history )
516
+ if no_traceback :
517
+ self .IP .set_custom_exc ((), None )
498
518
499
519
if not is_suppress :
500
520
for i , line in enumerate (input_lines ):
@@ -901,13 +921,14 @@ class IPythonDirective(Directive):
901
921
required_arguments = 0
902
922
optional_arguments = 4 # python, suppress, verbatim, doctest
903
923
final_argumuent_whitespace = True
904
- option_spec = { 'python' : directives .unchanged ,
905
- 'suppress' : directives .flag ,
906
- 'verbatim' : directives .flag ,
907
- 'doctest' : directives .flag ,
908
- 'okexcept' : directives .flag ,
909
- 'okwarning' : directives .flag
910
- }
924
+ option_spec = {
925
+ "python" : directives .unchanged ,
926
+ "suppress" : directives .flag ,
927
+ "verbatim" : directives .flag ,
928
+ "doctest" : directives .flag ,
929
+ "okexcept" : directives .unchanged ,
930
+ "okwarning" : directives .flag ,
931
+ }
911
932
912
933
shell = None
913
934
@@ -1001,11 +1022,12 @@ def run(self):
1001
1022
rgxin , rgxout , promptin , promptout = self .setup ()
1002
1023
1003
1024
options = self .options
1004
- self .shell .is_suppress = 'suppress' in options
1005
- self .shell .is_doctest = 'doctest' in options
1006
- self .shell .is_verbatim = 'verbatim' in options
1007
- self .shell .is_okexcept = 'okexcept' in options
1008
- self .shell .is_okwarning = 'okwarning' in options
1025
+ self .shell .is_suppress = "suppress" in options
1026
+ self .shell .is_doctest = "doctest" in options
1027
+ self .shell .is_verbatim = "verbatim" in options
1028
+ self .shell .is_okexcept = "okexcept" in options
1029
+ self .shell .is_okwarning = "okwarning" in options
1030
+ self .shell .no_traceback = options .get ("okexcept" ) == "no_traceback"
1009
1031
1010
1032
# handle pure python code
1011
1033
if 'python' in self .arguments :
0 commit comments