Skip to content

Pr ipython directive context #6075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from Jan 24, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions doc/sphinxext/ipython_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,15 @@ def block_parser(part, rgxin, rgxout, fmtin, fmtout):
class EmbeddedSphinxShell(object):
"""An embedded IPython instance to run inside Sphinx"""

def __init__(self, exec_lines=None):
def __init__(self, exec_lines=None,state=None):

self.cout = StringIO()

if exec_lines is None:
exec_lines = []

self.state = state

# Create config object for IPython
config = Config()
config.InteractiveShell.autocall = False
Expand Down Expand Up @@ -438,8 +440,20 @@ def process_input(self, data, input_prompt, lineno):
elif is_semicolon: # get spacing right
ret.append('')

# output any exceptions raised during execution to stdout
# unless :okexcept: has been specified.
if not is_okexcept and "Traceback" in output:
filename = self.state.document.current_source
lineno = self.state.document.current_line
try:
lineno = int(lineno) -1
except:
pass
s = "\nException in %s at line %s:\n" % (filename, lineno)
sys.stdout.write('\n\n>>>'+'-'*73)
sys.stdout.write(s)
sys.stdout.write(output)
sys.stdout.write('<<<' + '-'*73+'\n\n')

self.cout.truncate(0)
return (ret, input_lines, output, is_doctest, decorator, image_file,
Expand Down Expand Up @@ -735,7 +749,7 @@ def setup(self):

# Must be called after (potentially) importing matplotlib and
# setting its backend since exec_lines might import pylab.
self.shell = EmbeddedSphinxShell(exec_lines)
self.shell = EmbeddedSphinxShell(exec_lines, self.state)

# Store IPython directive to enable better error messages
self.shell.directive = self
Expand Down