From mboxrd@z Thu Jan  1 00:00:00 1970
From: John Kitchin <jkitchin@andrew.cmu.edu>
Subject: Re: setting options to python interpreter for a code block
Date: Wed, 11 Sep 2013 14:25:32 -0400
Message-ID: <CAJ51ETr-REDaU9NuNKcqxsrrkaK7Msh=u9qx0kLU8SBH++Jn7A@mail.gmail.com>
References: <CAJ51ETpJZdU5M4-fNmsOu5T2mqQkcSgYzV1xDNSk3=e3QmXy-A@mail.gmail.com>
	<87bo3zw7dl.fsf@gmail.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=bcaec521623b2d4d1304e61fbf3d
Return-path: <emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org>
Received: from eggs.gnu.org ([2001:4830:134:3::10]:34528)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <johnrkitchin@gmail.com>) id 1VJp6e-0004P5-8s
	for emacs-orgmode@gnu.org; Wed, 11 Sep 2013 14:25:38 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <johnrkitchin@gmail.com>) id 1VJp6c-0001Wt-8T
	for emacs-orgmode@gnu.org; Wed, 11 Sep 2013 14:25:36 -0400
Received: from mail-pa0-x22d.google.com ([2607:f8b0:400e:c03::22d]:36764)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <johnrkitchin@gmail.com>) id 1VJp6b-0001Wf-KW
	for emacs-orgmode@gnu.org; Wed, 11 Sep 2013 14:25:34 -0400
Received: by mail-pa0-f45.google.com with SMTP id bg4so9661264pad.32
	for <emacs-orgmode@gnu.org>; Wed, 11 Sep 2013 11:25:32 -0700 (PDT)
In-Reply-To: <87bo3zw7dl.fsf@gmail.com>
List-Id: "General discussions about Org-mode." <emacs-orgmode.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/emacs-orgmode>,
	<mailto:emacs-orgmode-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/emacs-orgmode>
List-Post: <mailto:emacs-orgmode@gnu.org>
List-Help: <mailto:emacs-orgmode-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/emacs-orgmode>,
	<mailto:emacs-orgmode-request@gnu.org?subject=subscribe>
Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org
Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org
To: Eric Schulte <schulte.eric@gmail.com>
Cc: emacs-orgmode@gnu.org

--bcaec521623b2d4d1304e61fbf3d
Content-Type: text/plain; charset=ISO-8859-1

cool! this worked wonderfully:


(setq org-babel-python-command "python -m sandbox")
#+BEGIN_SRC python
print 'hello'

print 4 + 6

import sys

print >>sys.stderr, 'message to stderr'


raise Exception('baboom')
#+END_SRC

#+RESULTS:
#+begin_example

    --------------------------------------------------------------
    hello
10


    --------------------------------------------------------------
    stderr:
    message to stderr


    --------------------------------------------------------------
    Traceback (most recent call last):
  File "/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py", line 19, in
<module>
    exec(content, ns_globals, ns_locals)
  File "<string>", line 10, in <module>
Exception: baboom


#+end_example

If anyone is interested, here is the sandbox module:
#!/usr/bin/env python
from cStringIO import StringIO
import os, sys

old_stdout = sys.stdout
old_stderr = sys.stderr
redirected_output = sys.stdout = StringIO()
redirected_error = sys.stderr = StringIO()

ns_globals = {}
ns_locals = {}


if __name__ == '__main__':
    content = sys.stdin.read()
    out, err, exc = None, None, None

    try:
        exec(content, ns_globals, ns_locals)
    except:
        import traceback
        exc = traceback.format_exc()

    out = redirected_output.getvalue()
    err = redirected_error.getvalue()

    sys.stdout = old_stdout
    sys.stderr = old_stderr

    s = '''
    --------------------------------------------------------------
    {0}
    '''.format(out)

    if err:
        s += '''
    --------------------------------------------------------------
    stderr:
    {0}
    '''.format(err)

    if exc:
        s += '''
    --------------------------------------------------------------
    {0}
    '''.format(exc)

    print s


John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu



On Wed, Sep 11, 2013 at 2:17 PM, Eric Schulte <schulte.eric@gmail.com>wrote:

> You could try setting org-babel-python-command to "python -m sandbox".
>
> If that doesn't work we could add a cmdline header argument to python
> code blocks pretty easily.
>
> Cheers,
>
> John Kitchin <jkitchin@andrew.cmu.edu> writes:
>
> > Hi,
> > 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.
> >
> > 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.
> >
> > Say test.py has this content
> >
> > #+BEGIN_SRC python
> > print 'hello'
> >
> > print 4 + 6
> >
> > import sys
> >
> > print >>sys.stderr, 'message to stderr'
> >
> >
> > raise Exception('baboom')
> > #+END_SRC
> >
> > When I use the sandbox, I get all the output on stdout like this.
> >
> > #+BEGIN_SRC sh
> > python -m sandbox < test.py
> > # or cat test.py | python -m sandbox
> > #+END_SRC
> >
> > #+RESULTS:
> > #+begin_example
> >
> > --------------------------------------------------------------
> > hello
> > 10
> >
> >
> > --------------------------------------------------------------
> > stderr:
> > message to stderr
> >
> >
> > --------------------------------------------------------------
> > Traceback (most recent call last):
> >   File "/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py", line 16,
> in
> > <module>
> >     exec(content, ns_globals, ns_locals)
> >   File "<string>", line 10, in <module>
> > Exception: baboom
> >
> >
> > #+end_example
> >
> >
> > 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.
> >
> > Is there some other way that I could do this? Thanks!
> >
> > John
> >
> > -----------------------------------
> > John Kitchin
> > Associate Professor
> > Doherty Hall A207F
> > Department of Chemical Engineering
> > Carnegie Mellon University
> > Pittsburgh, PA 15213
> > 412-268-7803
> > http://kitchingroup.cheme.cmu.edu
>
> --
> Eric Schulte
> https://cs.unm.edu/~eschulte
> PGP: 0x614CA05D
>

--bcaec521623b2d4d1304e61fbf3d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>cool! this worked wonderfully:<br><br><br>(setq org-b=
abel-python-command &quot;python -m sandbox&quot;)<br>#+BEGIN_SRC python <b=
r>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>=A0=A0=A0 -----------------------------------------=
---------------------<br>=A0=A0=A0 hello<br>10<br><br>=A0=A0=A0 <br>=A0=A0=
=A0 --------------------------------------------------------------<br>
=A0=A0=A0 stderr:<br>=A0=A0=A0 message to stderr<br><br>=A0=A0=A0 <br>=A0=
=A0=A0 --------------------------------------------------------------<br>=
=A0=A0=A0 Traceback (most recent call last):<br>=A0 File &quot;/home/jkitch=
in/Dropbox/pycse/pycse/sandbox/sandbox.py&quot;, line 19, in &lt;module&gt;=
<br>
=A0=A0=A0 exec(content, ns_globals, ns_locals)<br>=A0 File &quot;&lt;string=
&gt;&quot;, line 10, in &lt;module&gt;<br>Exception: baboom<br><br>=A0=A0=
=A0 <br>#+end_example<br><br></div>If anyone is interested, here is the san=
dbox module:<br>
#!/usr/bin/env python<br>from cStringIO import StringIO<br>import os, sys<b=
r><br>old_stdout =3D sys.stdout<br>old_stderr =3D sys.stderr<br>redirected_=
output =3D sys.stdout =3D StringIO()<br>redirected_error =3D sys.stderr =3D=
 StringIO()<br>
<br>ns_globals =3D {}<br>ns_locals =3D {}<br><br><br>if __name__ =3D=3D &#3=
9;__main__&#39;:<br>=A0=A0=A0 content =3D sys.stdin.read()<br>=A0=A0=A0 out=
, err, exc =3D None, None, None<br><br>=A0=A0=A0 try:<br>=A0=A0=A0=A0=A0=A0=
=A0 exec(content, ns_globals, ns_locals)<br>
=A0=A0=A0 except:<br>=A0=A0=A0=A0=A0=A0=A0 import traceback<br>=A0=A0=A0=A0=
=A0=A0=A0 exc =3D traceback.format_exc()<br><br>=A0=A0=A0 out =3D redirecte=
d_output.getvalue()<br>=A0=A0=A0 err =3D redirected_error.getvalue()<br><br=
>=A0=A0=A0 sys.stdout =3D old_stdout<br>=A0=A0=A0 sys.stderr =3D old_stderr=
<br>
<br>=A0=A0=A0 s =3D &#39;&#39;&#39;<br>=A0=A0=A0 --------------------------=
------------------------------------<br>=A0=A0=A0 {0}<br>=A0=A0=A0 &#39;&#3=
9;&#39;.format(out)<br><br>=A0=A0=A0 if err:<br>=A0=A0=A0=A0=A0=A0=A0 s +=
=3D &#39;&#39;&#39;<br>=A0=A0=A0 ------------------------------------------=
--------------------<br>
=A0=A0=A0 stderr:<br>=A0=A0=A0 {0}<br>=A0=A0=A0 &#39;&#39;&#39;.format(err)=
<br><br>=A0=A0=A0 if exc:<br>=A0=A0=A0=A0=A0=A0=A0 s +=3D &#39;&#39;&#39;<b=
r>=A0=A0=A0 --------------------------------------------------------------<=
br>=A0=A0=A0 {0}<br>=A0=A0=A0 &#39;&#39;&#39;.format(exc)<br>
<br>=A0=A0=A0 print s<br><br></div><div class=3D"gmail_extra"><br clear=3D"=
all"><div>John<br><br>-----------------------------------<br>John Kitchin<b=
r>Associate Professor<br>Doherty Hall A207F<br>Department of Chemical Engin=
eering<br>
Carnegie Mellon University<br>Pittsburgh, PA 15213<br>412-268-7803<br><a hr=
ef=3D"http://kitchingroup.cheme.cmu.edu" target=3D"_blank">http://kitchingr=
oup.cheme.cmu.edu</a><br><br></div>
<br><br><div class=3D"gmail_quote">On Wed, Sep 11, 2013 at 2:17 PM, Eric Sc=
hulte <span dir=3D"ltr">&lt;<a href=3D"mailto:schulte.eric@gmail.com" targe=
t=3D"_blank">schulte.eric@gmail.com</a>&gt;</span> wrote:<br><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex">
You could try setting org-babel-python-command to &quot;python -m sandbox&q=
uot;.<br>
<br>
If that doesn&#39;t work we could add a cmdline header argument to python<b=
r>
code blocks pretty easily.<br>
<br>
Cheers,<br>
<div class=3D"HOEnZb"><div class=3D"h5"><br>
John Kitchin &lt;<a href=3D"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 pyt=
hon<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 excep=
tions<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; =A0 File &quot;/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py&q=
uot;, line 16, in<br>
&gt; &lt;module&gt;<br>
&gt; =A0 =A0 exec(content, ns_globals, ns_locals)<br>
&gt; =A0 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 interpr=
eter<br>
&gt; for a particular block, e.g. python-sandbox that takes the codeblock o=
n<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=3D"tel:412-268-7803" value=3D"+14122687803">412-268-7803</a><b=
r>
&gt; <a href=3D"http://kitchingroup.cheme.cmu.edu" target=3D"_blank">http:/=
/kitchingroup.cheme.cmu.edu</a><br>
<br>
</div></div><span class=3D"HOEnZb"><font color=3D"#888888">--<br>
Eric Schulte<br>
<a href=3D"https://cs.unm.edu/%7Eeschulte" target=3D"_blank">https://cs.unm=
.edu/~eschulte</a><br>
PGP: 0x614CA05D<br>
</font></span></blockquote></div><br></div>

--bcaec521623b2d4d1304e61fbf3d--