Skip to content

Commit fe8fe38

Browse files
author
y-p
committed
Merge pull request #6082 from y-p/PR_ipython_directive
BLD/DOC: report context on warnings and add :okwarning: to ipython_directive
2 parents 9efcb03 + 1c72784 commit fe8fe38

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

doc/sphinxext/ipython_directive.py

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import tempfile
112112
import ast
113113
from pandas.compat import zip, range, map, lmap, u, cStringIO as StringIO
114+
import warnings
114115

115116
# To keep compatibility with various python versions
116117
try:
@@ -375,6 +376,7 @@ def process_input(self, data, input_prompt, lineno):
375376
decorator.startswith('@doctest')) or self.is_doctest
376377
is_suppress = decorator=='@suppress' or self.is_suppress
377378
is_okexcept = decorator=='@okexcept' or self.is_okexcept
379+
is_okwarning = decorator=='@okwarning' or self.is_okwarning
378380
is_savefig = decorator is not None and \
379381
decorator.startswith('@savefig')
380382

@@ -404,28 +406,30 @@ def process_input(self, data, input_prompt, lineno):
404406
else:
405407
store_history = True
406408

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)
416424
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)
424428

425-
formatted_line = '%s %s'%(continuation, line)
429+
formatted_line = '%s %s'%(continuation, line)
426430

427-
if not is_suppress:
428-
ret.append(formatted_line)
431+
if not is_suppress:
432+
ret.append(formatted_line)
429433

430434
if not is_suppress and len(rest.strip()) and is_verbatim:
431435
# the "rest" is the standard output of the
@@ -440,21 +444,36 @@ def process_input(self, data, input_prompt, lineno):
440444
elif is_semicolon: # get spacing right
441445
ret.append('')
442446

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+
443455
# output any exceptions raised during execution to stdout
444456
# unless :okexcept: has been specified.
445457
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
452458
s = "\nException in %s at line %s:\n" % (filename, lineno)
453459
sys.stdout.write('\n\n>>>'+'-'*73)
454460
sys.stdout.write(s)
455461
sys.stdout.write(output)
456462
sys.stdout.write('<<<' + '-'*73+'\n\n')
457463

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 = "\nWarning 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+
458477
self.cout.truncate(0)
459478
return (ret, input_lines, output, is_doctest, decorator, image_file,
460479
image_directive)
@@ -698,7 +717,8 @@ class IPythonDirective(Directive):
698717
'suppress' : directives.flag,
699718
'verbatim' : directives.flag,
700719
'doctest' : directives.flag,
701-
'okexcept': directives.flag
720+
'okexcept': directives.flag,
721+
'okwarning': directives.flag
702722
}
703723

704724
shell = None
@@ -797,6 +817,7 @@ def run(self):
797817
self.shell.is_doctest = 'doctest' in options
798818
self.shell.is_verbatim = 'verbatim' in options
799819
self.shell.is_okexcept = 'okexcept' in options
820+
self.shell.is_okwarning = 'okwarning' in options
800821

801822
# handle pure python code
802823
if 'python' in self.arguments:

0 commit comments

Comments
 (0)