emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Michael Brand <michael.ch.brand@gmail.com>
To: Eric Schulte <schulte.eric@gmail.com>
Cc: "András Major" <andras.g.major@gmail.com>,
	"Sebastien Vauban" <wxhgmqzgwmuf@spammotel.com>,
	"Achim Gratz" <stromeko@nexgo.de>,
	emacs-orgmode@gnu.org
Subject: Re: Bug: babel: results switch (output vs. value) has no or wrong effect for sh source block [7.7 (release_7.7.107.g7a82)]
Date: Mon, 29 Aug 2011 21:17:46 +0200	[thread overview]
Message-ID: <CALn3zoh5Cs1pC8n_mD92gS5N1sbHN72t_roGFvYsQ-R4J92cPA@mail.gmail.com> (raw)
In-Reply-To: <8762lodoy1.fsf@gmail.com>

Hi Eric

On Tue, Aug 23, 2011 at 19:13, Eric Schulte <schulte.eric@gmail.com> wrote:
> Note that during interactive evaluation if the exit status is non-0 then
> STDERR will be dumped into a babel error buffer which will be poped up,
> so this information will not be silently discarded.

Thanks for emphasizing this. I didn't use code block evaluation much
for ob-sh and haven't yet noticed this rule with the last exit status
that seems very important to know.

Two things I would like to comment referring to the examples 1) to 5)
below:
a) _completeness_ of the process output:
   - When the last exit status is 0, babel silently discards possible
     important information from stderr, see mainly 1), also 2) to 3).
   - When the last exit status is >0, babel pops up the "Org-Babel
     Error Output" and silently discards possible important
     information from stdout, see 4).
b) _decision_ whether the process succeeded or failed:
   - This is made by discriminating the last exit status 0 or not. But
     there are even standard processes with an exit status and stderr
     not simple, see 4) to 5) ("simple": 0 => success and output only
     on stdout, >0 => failure and output only on stderr).

My suggestions for changes (I have understood that this would not be
easy to do, babel grew differently):
a) _completeness_ of the process output:
   - When the last exit status is 0: Include stderr in "#+results:" by
     default. In the minority of use cases where the behavior changes
     I think this is most of the time an improvement.
   - When the last exit status is >0 and babel pops up the "Org-Babel
     Error Output": Include stdout there by default. In the minority
     of use cases where the behavior changes I think this is always an
     improvement.
b) _decision_ whether the process succeeded or failed:
   - My point of view is that babel code block evaluation is not
     always able to make such a decision for two reasons, at least in
     the case of the languages awk, sh and shell: Firstly not
     universally because it would require to consider which process it
     runs, e. g. the exit status 1 means success for diff or grep but
     >0 means failure for most other processes. Secondly not reliably
     because it would require to consider for which purpose this
     process is used during this evaluation. When one wants to
     document the output of a failure then "Org-Babel Error Output"
     does not come in handy.
   - An option to move this decision to the user would help. For this
     I wish to have an additional and somehow more manual operation
     mode that bypasses the interpretation of the last exit status and
     omits "Org-Babel Error Output". Achim already phrased it in this
     thread as:
     - "I think that it would be generally useful (not just for shell
       blocks) to be able to capture stderr, either together with
       stdout or separately into a result target block and have the
       return status available as a variable."
     - I would only add: It would be nice to have both variants of
       "either or" available to choose from. The first for better
       human readability and the second to read stdout without stderr
       into the next chained code block again.

----------------------------------------------------------------

Comments and questions for the examples and workarounds:

Examples 4) to 5) end up with an empty "Org-Babel Error Output" and
"#+results:" is emptied.
- Since they are both empty it is not necessarily clear what happened.
- The Messages buffer says "Babel evaluation exited with code 1" and
  "Code block produced no output.". The second statement is not
  necessarily clear:
  - Is this an _info_ or was output expected and this is an _error_
    that even stopped babel from doing more work?
    - Is the message "Code block evaluation complete." from other
      cases an indication whether babel finished all its work? If this
      is an indication, isn't this message missing in the following
      case?
        #+begin_src sh :results output
          true
        #+end_src

        #+results:

  - Which "output" was meant to be missing?
    - Output missing on stdout (example 5) or was stdout not
      considered due to last exit status >0 (example 4)?
    - Output missing on stderr?

At the end are the workarounds I will use for now for the examples 1)
to 5). Shown is the complete variant when I need to know the last exit
status.
- 2>&1 :: collect stderr
- $?   :: last exit status
- true :: omit any "Org-Babel Error Output"

----------------------------------------------------------------

Examples:

1) command(s) before the last command have error
   - non-existent file myfile, output mixed on stdout and stderr
     - in a terminal (not babel code block evaluation):
       - shell command:
         #+begin_example
           ls myfile
           echo b
         #+end_example

       - output (stderr: ls, stdout: echo, exit status ls: 2, exit
         status echo: 0):
         #+begin_example
           ls: myfile: No such file or directory
           b
         #+end_example

     - with babel:
       #+begin_src sh :results output
         ls myfile
         echo b
       #+end_src

       #+results:
       : b

2) strace (Linux)
   - output on stderr
     - in a terminal (not babel code block evaluation):
       - shell command:
         #+begin_example
           strace true
         #+end_example

       - output (stdout: empty, stderr: syscalls, exit status: 0):
         #+begin_example
           execve("/bin/true", ["true"], [/* 54 vars */]) = 0
           [...]
           exit_group(0)                           = ?
         #+end_example

     - with babel:
       #+begin_src sh :results output
         strace true
       #+end_src

       #+results:

3) truss (Solaris)
   - output on stderr
     - in a terminal (not babel code block evaluation):
       - shell command:
         #+begin_example
           truss true
         #+end_example

       - output (stdout: empty, stderr: syscalls, exit status: 0):
         #+begin_example
           execve("/usr/bin/true", 0xFFBFF0FC, 0xFFBFF104)  argc = 1
           [...]
           _exit(0)
         #+end_example

     - with babel:
       #+begin_src sh :results output
         truss true
       #+end_src

       #+results:

4) diff
   - the exit status as standardized by POSIX/SUS
     http://pubs.opengroup.org/onlinepubs/009695399/utilities/diff.html
     - 0  :: No differences were found.
     - 1  :: Differences were found.
     - >1 :: An error occurred.

   - example with two differing files
     #+begin_src sh :results silent
       echo a > /tmp/a
       echo b > /tmp/b
     #+end_src

     - in a terminal (not babel code block evaluation):
       - shell command:
         #+begin_example
           diff /tmp/a /tmp/b
         #+end_example

       - output (stdout: differences, stderr: empty, exit status: 1):
         #+begin_example
           1c1
           < a
           ---
           > b
         #+end_example

     - with babel:
       #+begin_src sh :results output
         diff /tmp/a /tmp/b
       #+end_src

       #+results:

5) grep
   - the exit status as standardized by POSIX/SUS
     http://pubs.opengroup.org/onlinepubs/009695399/utilities/grep.html
     - 0  :: One or more lines were selected.
     - 1  :: No lines were selected.
     - >1 :: An error occurred.

   - example:
     - in a terminal (not babel code block evaluation):
       - shell command:
         #+begin_example
           echo a | grep b
         #+end_example

       - output (stdout: empty, stderr: empty, exit status: 1):
         #+begin_example
         #+end_example

     - with babel:
       #+begin_src sh :results output
         echo a | grep b
       #+end_src

       #+results:

----------------------------------------------------------------

Workarounds:

1) command(s) before the last command have error
   #+begin_src sh :results output
     ls myfile 2>&1
     echo b 2>&1
     echo "last exit status: $?" 2>&1
     true 2>&1
   #+end_src

   #+results:
   : ls: myfile: No such file or directory
   : b
   : last exit status: 0

2) strace (Linux)
   #+begin_src sh :results output
     strace true b 2>&1
     echo "last exit status: $?" 2>&1
     true 2>&1
   #+end_src

   #+results:
   : execve("/bin/true", ["true", "b"], [/* 104 vars */]) = 0
   : [...]
   : exit_group(0)                           = ?
   : last exit status: 0

3) truss (Solaris)
   #+begin_src sh :results output
     truss true b 2>&1
     echo "last exit status: $?" 2>&1
     true 2>&1
   #+end_src

   #+results:
   : execve("/usr/bin/true", 0xFFBFF0FC, 0xFFBFF104)  argc = 1
   : [...]
   : _exit(0)
   : last exit status: 0

4) diff
   #+begin_src sh :results output
     diff /tmp/a /tmp/b 2>&1
     echo "last exit status: $?" 2>&1
     true 2>&1
   #+end_src

   #+results:
   : 1c1
   : < a
   : ---
   : > b
   : last exit status: 1

5) grep
   #+begin_src sh :results output
     echo a | grep b 2>&1
     echo "last exit status: $?" 2>&1
     true 2>&1
   #+end_src

   #+results:
   : last exit status: 1

----------------------------------------------------------------

Michael

      reply	other threads:[~2011-08-29 19:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-19  6:36 Bug: babel: results switch (output vs. value) has no or wrong effect for sh source block [7.7 (release_7.7.107.g7a82)] Andras Major
2011-08-19 13:33 ` Eric Schulte
2011-08-19 18:52   ` András Major
2011-08-19 19:03     ` Achim Gratz
2011-08-19 19:38       ` Sebastien Vauban
2011-08-19 19:20     ` Sebastien Vauban
2011-08-19 22:46     ` Eric Schulte
2011-08-22 21:49       ` Michael Brand
2011-08-23 16:18         ` Achim Gratz
2011-08-23 17:10           ` Eric Schulte
2011-08-23 17:13         ` Eric Schulte
2011-08-29 19:17           ` Michael Brand [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CALn3zoh5Cs1pC8n_mD92gS5N1sbHN72t_roGFvYsQ-R4J92cPA@mail.gmail.com \
    --to=michael.ch.brand@gmail.com \
    --cc=andras.g.major@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=schulte.eric@gmail.com \
    --cc=stromeko@nexgo.de \
    --cc=wxhgmqzgwmuf@spammotel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).