111
111
import tempfile
112
112
import ast
113
113
from pandas .compat import zip , range , map , lmap , u , cStringIO as StringIO
114
+ import warnings
114
115
115
116
# To keep compatibility with various python versions
116
117
try :
@@ -375,6 +376,7 @@ def process_input(self, data, input_prompt, lineno):
375
376
decorator .startswith ('@doctest' )) or self .is_doctest
376
377
is_suppress = decorator == '@suppress' or self .is_suppress
377
378
is_okexcept = decorator == '@okexcept' or self .is_okexcept
379
+ is_okwarning = decorator == '@okwarning' or self .is_okwarning
378
380
is_savefig = decorator is not None and \
379
381
decorator .startswith ('@savefig' )
380
382
@@ -404,28 +406,30 @@ def process_input(self, data, input_prompt, lineno):
404
406
else :
405
407
store_history = True
406
408
407
- for i , line in enumerate (input_lines ):
408
- if line .endswith (';' ):
409
- is_semicolon = True
410
-
411
- if i == 0 :
412
- # process the first input line
413
- if is_verbatim :
414
- self .process_input_line ('' )
415
- self .IP .execution_count += 1 # increment it anyway
409
+ # Note: catch_warnings is not thread safe
410
+ with warnings .catch_warnings (record = True ) as ws :
411
+ for i , line in enumerate (input_lines ):
412
+ if line .endswith (';' ):
413
+ is_semicolon = True
414
+
415
+ if i == 0 :
416
+ # process the first input line
417
+ if is_verbatim :
418
+ self .process_input_line ('' )
419
+ self .IP .execution_count += 1 # increment it anyway
420
+ else :
421
+ # only submit the line in non-verbatim mode
422
+ self .process_input_line (line , store_history = store_history )
423
+ formatted_line = '%s %s' % (input_prompt , line )
416
424
else :
417
- # only submit the line in non-verbatim mode
418
- self .process_input_line (line , store_history = store_history )
419
- formatted_line = '%s %s' % (input_prompt , line )
420
- else :
421
- # process a continuation line
422
- if not is_verbatim :
423
- self .process_input_line (line , store_history = store_history )
425
+ # process a continuation line
426
+ if not is_verbatim :
427
+ self .process_input_line (line , store_history = store_history )
424
428
425
- formatted_line = '%s %s' % (continuation , line )
429
+ formatted_line = '%s %s' % (continuation , line )
426
430
427
- if not is_suppress :
428
- ret .append (formatted_line )
431
+ if not is_suppress :
432
+ ret .append (formatted_line )
429
433
430
434
if not is_suppress and len (rest .strip ()) and is_verbatim :
431
435
# the "rest" is the standard output of the
@@ -440,21 +444,36 @@ def process_input(self, data, input_prompt, lineno):
440
444
elif is_semicolon : # get spacing right
441
445
ret .append ('' )
442
446
447
+ # context information
448
+ filename = self .state .document .current_source
449
+ lineno = self .state .document .current_line
450
+ try :
451
+ lineno -= 1
452
+ except :
453
+ pass
454
+
443
455
# output any exceptions raised during execution to stdout
444
456
# unless :okexcept: has been specified.
445
457
if not is_okexcept and "Traceback" in output :
446
- filename = self .state .document .current_source
447
- lineno = self .state .document .current_line
448
- try :
449
- lineno = int (lineno ) - 1
450
- except :
451
- pass
452
458
s = "\n Exception in %s at line %s:\n " % (filename , lineno )
453
459
sys .stdout .write ('\n \n >>>' + '-' * 73 )
454
460
sys .stdout .write (s )
455
461
sys .stdout .write (output )
456
462
sys .stdout .write ('<<<' + '-' * 73 + '\n \n ' )
457
463
464
+ # output any warning raised during execution to stdout
465
+ # unless :okwarning: has been specified.
466
+ if not is_okwarning :
467
+ for w in ws :
468
+ s = "\n Warning raised in %s at line %s:\n " % (filename , lineno )
469
+ sys .stdout .write ('\n \n >>>' + '-' * 73 )
470
+ sys .stdout .write (s )
471
+ sys .stdout .write ('-' * 76 + '\n ' )
472
+ s = warnings .formatwarning (w .message , w .category ,
473
+ w .filename , w .lineno , w .line )
474
+ sys .stdout .write (s )
475
+ sys .stdout .write ('\n <<<' + '-' * 73 + '\n \n ' )
476
+
458
477
self .cout .truncate (0 )
459
478
return (ret , input_lines , output , is_doctest , decorator , image_file ,
460
479
image_directive )
@@ -698,7 +717,8 @@ class IPythonDirective(Directive):
698
717
'suppress' : directives .flag ,
699
718
'verbatim' : directives .flag ,
700
719
'doctest' : directives .flag ,
701
- 'okexcept' : directives .flag
720
+ 'okexcept' : directives .flag ,
721
+ 'okwarning' : directives .flag
702
722
}
703
723
704
724
shell = None
@@ -797,6 +817,7 @@ def run(self):
797
817
self .shell .is_doctest = 'doctest' in options
798
818
self .shell .is_verbatim = 'verbatim' in options
799
819
self .shell .is_okexcept = 'okexcept' in options
820
+ self .shell .is_okwarning = 'okwarning' in options
800
821
801
822
# handle pure python code
802
823
if 'python' in self .arguments :
0 commit comments