From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: odd/unexpected behavior with Python src blocks Date: Mon, 29 Sep 2014 14:03:16 -0400 Message-ID: References: <87oatzkygu.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:35428) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XYfIG-0003NT-8b for emacs-orgmode@gnu.org; Mon, 29 Sep 2014 14:03:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XYfIB-0000wt-0o for emacs-orgmode@gnu.org; Mon, 29 Sep 2014 14:03:28 -0400 Received: from smtp.andrew.cmu.edu ([128.2.157.39]:50279) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XYfIA-0000vc-Sv for emacs-orgmode@gnu.org; Mon, 29 Sep 2014 14:03:22 -0400 Received: from johns-air.wv.cc.cmu.edu (KITCHIN-TIMEMACHINE.CHEME.CMU.EDU [128.2.54.215]) (user=jkitchin mech=PLAIN (0 bits)) by smtp.andrew.cmu.edu (8.14.8/8.14.8) with ESMTP id s8TI3Gaq028616 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT) for ; Mon, 29 Sep 2014 14:03:16 -0400 In-Reply-To: (Instructor account's message of "Mon, 29 Sep 2014 11:56:06 -0400") 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: emacs-orgmode@gnu.org In case anyone else is interested, here is a workaround of sorts to capture stderr in a python block: https://github.com/jkitchin/pycse/blob/master/pycse/sandbox.py You use this module to redefine stdout and stderr. You basically run python like this: #+BEGIN_SRC emacs-lisp (setq org-babel-python-command "python -m pycse.sandbox") #+END_SRC Then you get this output. #+BEGIN_SRC python :results output from scipy.integrate import odeint #k =3D -1 def f(y, x): return -k * y print(odeint(f, 1, [0, 1])) #+END_SRC #+RESULTS: #+begin_example sandbox: ---stdout----------------------------------------------------------- [[ 1.] [ 1.]] ---stderr----------------------------------------------------------- Traceback (most recent call last): File "", line 6, in f NameError: global name 'k' is not defined Traceback (most recent call last): File "", line 6, in f NameError: global name 'k' is not defined Traceback (most recent call last): File "", line 6, in f NameError: global name 'k' is not defined Traceback (most recent call last): File "", line 6, in f NameError: global name 'k' is not defined #+end_example At least until babel captures stderr, this is a way to capture it. It does = break the nice behavior of making tables output, since it prints a string. The string could be orgified somewhat, eg #+STDOUT: #+STDERR: I am not sure I will use this routinely, so I am not sure how much I will put into furhter development. Instructor account writes: > Indeed, this passes by org-babel because stderr is not captured, and > scipy does what you suggest. I guess this happens inside compiled > c/Fortran code since there is nothing catching it in the odeint > python function doing that. > > Thanks, I will try to bring it up on the scipy list.=20 > > Aaron Ecay writes: > >> Hi John, >> >> 2014ko irailak 28an, John Kitchin-ek idatzi zuen: >> >> [...] >> >>> I am not sure why this happens, but it seems like incorrect behavior to >>> me. >> >> In the first case, python exits with a non-zero exit code, whereas in >> the second it exits with a zero code (i.e. successful exit), and prints >> the matrix-thing [[1.], [1.]] to stdout. It looks like scipy traps the >> NameErrors and prints them to stderr, but continues its computation >> regardless. >> >> I=E2=80=99d say babel is performing as desired here, but scipy sadly isn= =E2=80=99t >> reporting its errors in the standard way (by a nonzero exit, which I >> think would happen automatically if the NameErrors made it to the top >> level of the stack without being caught). --=20 ----------------------------------- John Kitchin Professor Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 http://kitchingroup.cheme.cmu.edu