From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: setting options to python interpreter for a code block Date: Wed, 11 Sep 2013 14:25:32 -0400 Message-ID: References: <87bo3zw7dl.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=bcaec521623b2d4d1304e61fbf3d Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:34528) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) 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 ) 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 ) 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 ; Wed, 11 Sep 2013 11:25:32 -0700 (PDT) In-Reply-To: <87bo3zw7dl.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-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 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 exec(content, ns_globals, ns_locals) File "", line 10, in 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 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 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 > > > > exec(content, ns_globals, ns_locals) > > File "", line 10, in > > 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
cool! this worked wonderfully:


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

print 4 + 6

import sys

print &= gt;>sys.stderr, 'message to stderr'


raise Exception('baboom')
#+END_SRC

#+RESULTS:#+begin_example

=A0=A0=A0 -----------------------------------------= ---------------------
=A0=A0=A0 hello
10

=A0=A0=A0
=A0=A0= =A0 --------------------------------------------------------------
=A0=A0=A0 stderr:
=A0=A0=A0 message to stderr

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

=A0=A0= =A0
#+end_example

If anyone is interested, here is the san= dbox module:
#!/usr/bin/env python
from cStringIO import StringIO
import os, sys
old_stdout =3D sys.stdout
old_stderr =3D sys.stderr
redirected_= output =3D sys.stdout =3D StringIO()
redirected_error =3D sys.stderr =3D= StringIO()

ns_globals =3D {}
ns_locals =3D {}


if __name__ =3D=3D = 9;__main__':
=A0=A0=A0 content =3D sys.stdin.read()
=A0=A0=A0 out= , err, exc =3D None, None, None

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

=A0=A0=A0 out =3D redirecte= d_output.getvalue()
=A0=A0=A0 err =3D redirected_error.getvalue()
=A0=A0=A0 sys.stdout =3D old_stdout
=A0=A0=A0 sys.stderr =3D old_stderr=

=A0=A0=A0 s =3D '''
=A0=A0=A0 --------------------------= ------------------------------------
=A0=A0=A0 {0}
=A0=A0=A0 '= 9;'.format(out)

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

=A0=A0=A0 if exc:
=A0=A0=A0=A0=A0=A0=A0 s +=3D '''=A0=A0=A0 --------------------------------------------------------------<= br>=A0=A0=A0 {0}
=A0=A0=A0 '''.format(exc)

=A0=A0=A0 print s


John

-----------------------------------
John KitchinAssociate Professor
Doherty Hall A207F
Department of Chemical Engin= eering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingr= oup.cheme.cmu.edu



On Wed, Sep 11, 2013 at 2:17 PM, Eric Sc= hulte <schulte.eric@gmail.com> wrote:
You could try setting org-babel-python-command to "python -m sandbox&q= uot;.

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 pyt= hon
> code blocks. Right now exceptions are not captured in the output, and<= br> > neither is stderr.
>
> I made a little sandbox module that captures stdout, stderr, and excep= tions
> 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):
> =A0 File "/home/jkitchin/Dropbox/pycse/pycse/sandbox/sandbox.py&q= uot;, line 16, in
> <module>
> =A0 =A0 exec(content, ns_globals, ns_locals)
> =A0 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 interpr= eter
> for a particular block, e.g. python-sandbox that takes the codeblock o= n
> 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--