<div dir="ltr"><div>cool! this worked wonderfully:<br><br><br>(setq org-babel-python-command "python -m sandbox")<br>#+BEGIN_SRC python <br>print 'hello'<br><br>print 4 + 6<br><br>import sys<br><br>print >>sys.stderr, 'message to stderr'<br> <br><br>raise Exception('baboom')<br>#+END_SRC<br><br>#+RESULTS:<br>#+begin_example<br><br> --------------------------------------------------------------<br> hello<br>10<br><br> <br> --------------------------------------------------------------<br> stderr:<br> message to stderr<br><br> <br> --------------------------------------------------------------<br> Traceback (most recent call last):<br> File "/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py", line 19, in <module><br> exec(content, ns_globals, ns_locals)<br> File "<string>", line 10, in <module><br>Exception: baboom<br><br> <br>#+end_example<br><br></div>If anyone is interested, here is the sandbox module:<br> #!/usr/bin/env python<br>from cStringIO import StringIO<br>import os, sys<br><br>old_stdout = sys.stdout<br>old_stderr = sys.stderr<br>redirected_output = sys.stdout = StringIO()<br>redirected_error = sys.stderr = StringIO()<br> <br>ns_globals = {}<br>ns_locals = {}<br><br><br>if __name__ == '__main__':<br> content = sys.stdin.read()<br> out, err, exc = None, None, None<br><br> try:<br> exec(content, ns_globals, ns_locals)<br> except:<br> import traceback<br> exc = traceback.format_exc()<br><br> out = redirected_output.getvalue()<br> err = redirected_error.getvalue()<br><br> sys.stdout = old_stdout<br> sys.stderr = old_stderr<br> <br> s = '''<br> --------------------------------------------------------------<br> {0}<br> '''.format(out)<br><br> if err:<br> s += '''<br> --------------------------------------------------------------<br> stderr:<br> {0}<br> '''.format(err)<br><br> if exc:<br> s += '''<br> --------------------------------------------------------------<br> {0}<br> '''.format(exc)<br> <br> print s<br><br></div><div class="gmail_extra"><br clear="all"><div>John<br><br>-----------------------------------<br>John Kitchin<br>Associate Professor<br>Doherty Hall A207F<br>Department of Chemical Engineering<br> Carnegie Mellon University<br>Pittsburgh, PA 15213<br>412-268-7803<br><a href="http://kitchingroup.cheme.cmu.edu" target="_blank">http://kitchingroup.cheme.cmu.edu</a><br><br></div> <br><br><div class="gmail_quote">On Wed, Sep 11, 2013 at 2:17 PM, Eric Schulte <span dir="ltr"><<a href="mailto:schulte.eric@gmail.com" target="_blank">schulte.eric@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> You could try setting org-babel-python-command to "python -m sandbox".<br> <br> If that doesn't work we could add a cmdline header argument to python<br> code blocks pretty easily.<br> <br> Cheers,<br> <div class="HOEnZb"><div class="h5"><br> John Kitchin <<a href="mailto:jkitchin@andrew.cmu.edu">jkitchin@andrew.cmu.edu</a>> writes:<br> <br> > Hi,<br> > I am looking at a new strategy to capture stderr and exceptions in python<br> > code blocks. Right now exceptions are not captured in the output, and<br> > neither is stderr.<br> ><br> > I made a little sandbox module that captures stdout, stderr, and exceptions<br> > and then prints them all to stdout with some minor formatting. Here is an<br> > example.<br> ><br> > Say test.py has this content<br> ><br> > #+BEGIN_SRC python<br> > print 'hello'<br> ><br> > print 4 + 6<br> ><br> > import sys<br> ><br> > print >>sys.stderr, 'message to stderr'<br> ><br> ><br> > raise Exception('baboom')<br> > #+END_SRC<br> ><br> > When I use the sandbox, I get all the output on stdout like this.<br> ><br> > #+BEGIN_SRC sh<br> > python -m sandbox < test.py<br> > # or cat test.py | python -m sandbox<br> > #+END_SRC<br> ><br> > #+RESULTS:<br> > #+begin_example<br> ><br> > --------------------------------------------------------------<br> > hello<br> > 10<br> ><br> ><br> > --------------------------------------------------------------<br> > stderr:<br> > message to stderr<br> ><br> ><br> > --------------------------------------------------------------<br> > Traceback (most recent call last):<br> > File "/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py", line 16, in<br> > <module><br> > exec(content, ns_globals, ns_locals)<br> > File "<string>", line 10, in <module><br> > Exception: baboom<br> ><br> ><br> > #+end_example<br> ><br> ><br> > So, I was wondering how to get this to happen in org-mode on a regular<br> > python block. I think it could work if I could define a custom interpreter<br> > for a particular block, e.g. python-sandbox that takes the codeblock on<br> > stdin.<br> ><br> > Is there some other way that I could do this? Thanks!<br> ><br> > John<br> ><br> > -----------------------------------<br> > John Kitchin<br> > Associate Professor<br> > Doherty Hall A207F<br> > Department of Chemical Engineering<br> > Carnegie Mellon University<br> > Pittsburgh, PA 15213<br> > <a href="tel:412-268-7803" value="+14122687803">412-268-7803</a><br> > <a href="http://kitchingroup.cheme.cmu.edu" target="_blank">http://kitchingroup.cheme.cmu.edu</a><br> <br> </div></div><span class="HOEnZb"><font color="#888888">--<br> Eric Schulte<br> <a href="https://cs.unm.edu/%7Eeschulte" target="_blank">https://cs.unm.edu/~eschulte</a><br> PGP: 0x614CA05D<br> </font></span></blockquote></div><br></div>