<div dir="ltr"><div>cool! this worked wonderfully:<br><br><br>(setq org-babel-python-command &quot;python -m sandbox&quot;)<br>#+BEGIN_SRC python <br>print &#39;hello&#39;<br><br>print 4 + 6<br><br>import sys<br><br>print &gt;&gt;sys.stderr, &#39;message to stderr&#39;<br>
<br><br>raise Exception(&#39;baboom&#39;)<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 &quot;/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py&quot;, line 19, in &lt;module&gt;<br>
    exec(content, ns_globals, ns_locals)<br>  File &quot;&lt;string&gt;&quot;, line 10, in &lt;module&gt;<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__ == &#39;__main__&#39;:<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 = &#39;&#39;&#39;<br>    --------------------------------------------------------------<br>    {0}<br>    &#39;&#39;&#39;.format(out)<br><br>    if err:<br>        s += &#39;&#39;&#39;<br>    --------------------------------------------------------------<br>
    stderr:<br>    {0}<br>    &#39;&#39;&#39;.format(err)<br><br>    if exc:<br>        s += &#39;&#39;&#39;<br>    --------------------------------------------------------------<br>    {0}<br>    &#39;&#39;&#39;.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">&lt;<a href="mailto:schulte.eric@gmail.com" target="_blank">schulte.eric@gmail.com</a>&gt;</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 &quot;python -m sandbox&quot;.<br>
<br>
If that doesn&#39;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 &lt;<a href="mailto:jkitchin@andrew.cmu.edu">jkitchin@andrew.cmu.edu</a>&gt; writes:<br>
<br>
&gt; Hi,<br>
&gt; I am looking at a new strategy to capture stderr and exceptions in python<br>
&gt; code blocks. Right now exceptions are not captured in the output, and<br>
&gt; neither is stderr.<br>
&gt;<br>
&gt; I made a little sandbox module that captures stdout, stderr, and exceptions<br>
&gt; and then prints them all to stdout with some minor formatting. Here is an<br>
&gt; example.<br>
&gt;<br>
&gt; Say test.py has this content<br>
&gt;<br>
&gt; #+BEGIN_SRC python<br>
&gt; print &#39;hello&#39;<br>
&gt;<br>
&gt; print 4 + 6<br>
&gt;<br>
&gt; import sys<br>
&gt;<br>
&gt; print &gt;&gt;sys.stderr, &#39;message to stderr&#39;<br>
&gt;<br>
&gt;<br>
&gt; raise Exception(&#39;baboom&#39;)<br>
&gt; #+END_SRC<br>
&gt;<br>
&gt; When I use the sandbox, I get all the output on stdout like this.<br>
&gt;<br>
&gt; #+BEGIN_SRC sh<br>
&gt; python -m sandbox &lt; test.py<br>
&gt; # or cat test.py | python -m sandbox<br>
&gt; #+END_SRC<br>
&gt;<br>
&gt; #+RESULTS:<br>
&gt; #+begin_example<br>
&gt;<br>
&gt; --------------------------------------------------------------<br>
&gt; hello<br>
&gt; 10<br>
&gt;<br>
&gt;<br>
&gt; --------------------------------------------------------------<br>
&gt; stderr:<br>
&gt; message to stderr<br>
&gt;<br>
&gt;<br>
&gt; --------------------------------------------------------------<br>
&gt; Traceback (most recent call last):<br>
&gt;   File &quot;/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py&quot;, line 16, in<br>
&gt; &lt;module&gt;<br>
&gt;     exec(content, ns_globals, ns_locals)<br>
&gt;   File &quot;&lt;string&gt;&quot;, line 10, in &lt;module&gt;<br>
&gt; Exception: baboom<br>
&gt;<br>
&gt;<br>
&gt; #+end_example<br>
&gt;<br>
&gt;<br>
&gt; So, I was wondering how to get this to happen in org-mode on a regular<br>
&gt; python block. I think it could work if I could define a custom interpreter<br>
&gt; for a particular block, e.g. python-sandbox that takes the codeblock on<br>
&gt; stdin.<br>
&gt;<br>
&gt; Is there some other way that I could do this? Thanks!<br>
&gt;<br>
&gt; John<br>
&gt;<br>
&gt; -----------------------------------<br>
&gt; John Kitchin<br>
&gt; Associate Professor<br>
&gt; Doherty Hall A207F<br>
&gt; Department of Chemical Engineering<br>
&gt; Carnegie Mellon University<br>
&gt; Pittsburgh, PA 15213<br>
&gt; <a href="tel:412-268-7803" value="+14122687803">412-268-7803</a><br>
&gt; <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>