ENH: Equivalent to subprocess.check_output

Issue #26 new
Wes Turner created an issue

Is there a (easy) way to raise an Exception if the returncode is not (by default?) 0?

Comments (14)

  1. Wes Turner reporter

    ... looking for a way to avoid having to add

      if p.returncode != 0:
        raise subprocess.CalledProcessError()
    

    when porting from subprocess.

  2. Wes Turner reporter

    assert_returncode feels too much like a test, which this isn't; or an assertion that could be compiled out with -O 2, so, sticking with expect_returncode

    check_returncode doesn't seem to have the same implications as expect_.

    Pending further input, I'll stick with expect_returncode.

  3. Vinay Sajip repo owner

    Might there be use cases where several return codes might be OK? In which case, expected_returncodes could be passed as None or a tuple of integers (and perhaps a single integer which would be treated the same as a singleton tuple).

  4. pahaz NA

    expect_returncode - not a unix way. In all normal cases this argument must be set to 0. I think that this argument would be superfluous. He degrades the readability and understandability of code.

  5. Wes Turner reporter

    No check is performed when expect_returncode is None (the default).

    The alternative is to copy and paste this each time: https://bitbucket.org/vinay.sajip/sarge/pull-request/1/enh-add-call-check_call-check_output/diff#Lsarge/init.pyT1062

    [edit] https://bitbucket.org/vinay.sajip/sarge/pull-request/1/enh-add-call-check_call-check_output/diff#Lsarge/init.pyT1062

    [edit] fenced code block of markdown (```markdown\n...\n```) for the above:

    https://bitbucket.org/vinay.sajip/sarge/pull-request/1/enh-add-call-check_call-check_output/diff#Lsarge/__init__.pyT1062
    
    [https://bitbucket.org/vinay.sajip/sarge/pull-request/1/enh-add-call-check_call-check_output/diff#Lsarge/__init__.pyT1062](https://bitbucket.org/vinay.sajip/sarge/pull-request/1/enh-add-call-check_call-check_output/diff#Lsarge/__init__.pyT1062)
    
  6. Wes Turner reporter

    Looks like bitbucket markdown link heuristics are off. This is the linked code block:

                if isinstance(expect_returncode, (tuple, list)):
                    if list(self.returncodes) != list(expect_returncode):
                        raise CalledProcessError(
                            self.returncode,
                            self,
                            expected_returncode=expect_returncode)
                else:
                    if self.returncode != expect_returncode:
                        raise CalledProcessError(
                            self.returncode,
                            self,
                            expected_returncode=expect_returncode)
    
  7. Log in to comment