Giter Club home page Giter Club logo

Comments (5)

anki-code avatar anki-code commented on July 21, 2024

Indeed, $RAISE_SUBPROC_ERROR is to raise error in any case when any command fails but set -e treat the line as whole:

Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a list (see Lists of Commands), or a compound command (see Compound Commands) returns a non-zero status.

So we need additional setting e.g. $XONSH_RAISE_SUBPROC_PIPELINE_ERROR to do the same as bash. PR is welcome!

from xonsh.

anki-code avatar anki-code commented on July 21, 2024

JFYI. For this:

set -e
ls nonono || true     # false || true
echo "success"

the xonsh equvalent for now is this:

$RAISE_SUBPROC_ERROR = True
try:
    ls nonono
except:               # OR except subprocess.CalledProcessError:
    pass
echo "success"

from xonsh.

anki-code avatar anki-code commented on July 21, 2024

You can create syntax sugar for this using macro call:

import subprocess
def ignore_error(cmd):
    """Ignore failing of the command."""
    try:
        execx(cmd)
    except subprocess.CalledProcessError:
        pass
        
        
$RAISE_SUBPROC_ERROR = False      
ignore_error!(echo 1 and (ls nonono or echo 2))
echo success
# success
        
$RAISE_SUBPROC_ERROR = True        
ignore_error!(echo 1 and (ls nonono or echo 2))
echo success
# success

from xonsh.

ading2210 avatar ading2210 commented on July 21, 2024

I'm aware that I can do a try-except block to ignore errors, though that still doesn't help with the underlying problem of boolean logic being broken with subprocess return codes.

from xonsh.

anki-code avatar anki-code commented on July 21, 2024

I figured this out the case.

What bash doing. In fact it implicitly overrides the execution of a logical expression and the execution of process. If we run echo 1 and ls nonono the result of this logical will be treated as "return code" and if it's not 0 and set -e the error will be raised.

In xonsh we have separation between process running and logical expression running. If we run echo 1 and ls nonono we have 2 separate processes and logical expression and RAISE_SUBPROC_ERROR will work on process level.

In fact in this issue was requested an additional mode like RAISE_COMMAND_ERROR that will work for the whole logical expression.

Current behavior:

$RAISE_SUBPROC_ERROR=False
echo 1 and (ls no or echo 2)
# run logical: `echo 1`, `ls no`, `echo 2`.
__xonsh__.history[-1]
# rtn=0
echo 1 and (echo 2 or ls no)
# run logical: `echo 1`, `echo 2`.
__xonsh__.history[-1]
# rtn=0

$RAISE_SUBPROC_ERROR=True
echo 1 and (ls no or echo 2)
# run logical: `echo 1`, `ls no` (raise and stop).
__xonsh__.history[-1]
# rtn=2
echo 1 and (echo 2 or ls no)
# run logical: `echo 1`, `echo 2`.
__xonsh__.history[-1]
# rtn=0

from xonsh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.