<div dir="ltr"><div><div><div><div><div>Hi, <br></div><div>I am looking at a new strategy to capture stderr and exceptions in python code blocks. Right now exceptions are not captured in the output, and neither is stderr.<br>
<br>I made a little sandbox module that captures stdout, stderr, and exceptions and then prints them all to stdout with some minor formatting. Here is an example.<br><br></div><div>Say test.py has this content<br><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></div><div>When I use the sandbox, I get all the output on stdout like this.<br>
</div><div><br></div><div>#+BEGIN_SRC sh<br>python -m sandbox &lt; 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 &quot;/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py&quot;, line 16, 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></div><br><br></div>So, I was wondering how to get this to happen in org-mode on a regular python block. I think it could work if I could define a custom interpreter for a particular block, e.g. python-sandbox that takes the codeblock on stdin.<br>
</div></div><br></div>Is there some other way that I could do this? Thanks!<br><div><div><br clear="all"><div><div><div><div><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>
</div></div></div></div></div></div></div>