From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: What is output for org-babel? Date: Wed, 11 Nov 2009 19:25:16 -0500 Message-ID: <87tyx0r3mb.fsf@stats.ox.ac.uk> References: <4af2e095.0706c00a.3de8.57c8@mx.google.com> <87skcoydie.fsf@stats.ox.ac.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N8NVA-0000wY-IQ for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:25:28 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N8NV5-0000ul-Tz for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:25:28 -0500 Received: from [199.232.76.173] (port=60812 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8NV5-0000ug-OL for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:25:23 -0500 Received: from markov.stats.ox.ac.uk ([163.1.210.1]:45914) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1N8NV4-0007vo-MP for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:25:23 -0500 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Darlan Cavalcante Moreira Cc: emacs-orgmode@gnu.org, andrea Crotti Dan Davison writes: > Darlan Cavalcante Moreira writes: > <...> >>> So I investigate and for example this >>> >>> #+BEGIN_SRC python >>> import os; os.listdir(os.getcwd()) >>> #+END_SRC >> >> This does not return anything for me either, but >> #+BEGIN_SRC python >> import os >> os.listdir(os.getcwd()) >> #+END_SRC >> works as expected. This is strange, since "import os; os.listdir(os.getcwd())" >> is valid and will work if typed in the python interpreter directly. > > This is a limitation of the current implementation of :results value > evaluation. I've just pushed a change which fixes this and related problems. It introduces a change for python users: when using the default evaluation mode (':results value' non-session), if you want the source code block to return a value, you must now include the 'return' statement that would be required if you were writing a python function definition. On Worg I've added some more detailed documentation of exactly how the result is obtained in the four different cases (value/output, session/non-session), and I'm also pasting that below. Dan *** Evaluation results: output/value & session/non-session The following applies particularly to perl, python, R and ruby. Nature of Results: | | non-session | session | |--------+--------------------------+-------------------------------------| | value | value of last expression | value of last expression | | output | contents of stdout | concatenation of interpreter output | Note that in ':results value' (session and non-session), the result is imported into org-mode as a table (a one- or two-dimensional vector of strings or numbers) when appropriate. **** :results value (non-session) This is the default. Internally, the value is obtained by wrapping the code in a function definition in the external language, and evaluating that function. Therefore, code should be written as if it were the body of such a function. In particular, note that python does not automatically return a value from a function unless a =return= statement is present, and so a 'return' statement will usually be required in python :results value (non-session). This is the only one of the four evaluation contexts in which the code is automatically wrapped in a function definition. **** :results output (non-session) The code is passed to the interpreter as an external process, and the contents of the standard output stream is returned as text. (In certain languages this also contains the error output stream; this is an area for future work.) **** :results value (session) The code is passed to the interpreter running as an interactive emacs inferior process. The result returned is the result of the last evaluation performed by the interpreter. (This is obtained in a language-specific manner: the value of the variable =_= in python and ruby, and the value of =.Last.value= in R). **** :results output (session) The code is passed to the interpreter running as an interactive emacs inferior process. The result returned is the concatenation of the sequence of (text) output from the interactive interpreter. Notice that this is not necessarily the same as what would be sent to stdout if the same code were passed to a non-interactive interpreter running as an external process. For example, compare the following two blocks: #+begin_src python :results output print "hello" 2 print "bye" #+end_src #+resname: : hello : bye In non-session mode, the '2' is not printed and does not appear. #+begin_src python :results output :session print "hello" 2 print "bye" #+end_src #+resname: : hello : 2 : bye But in session mode, the interactive interpreter receives input '2' and prints out its value, '2'. (Indeed, the other print statements are unnecessary here).