ENH: Equivalent to subprocess.check_output
Is there a (easy) way to raise an Exception if the returncode is not (by default?) 0?
Comments (14)
-
reporter -
reporter ... looking for a way to avoid having to add
if p.returncode != 0: raise subprocess.CalledProcessError()
when porting from subprocess.
-
reporter -
repo owner This seems a reasonable request, I will look at implementing it soon.
-
reporter So, here's this, which adds an
expect_returncode
kwarg: https://bitbucket.org/westurner/sarge/commits/6cc7780f00ccba2a231c2b7c51cb2a29d287e660It may be a bit overzealous; there's probably a better way to accomplish this.
check_output
andcheck_call
could then just bepartials
ofrun
. -
reporter From discussions here: https://mail.python.org/pipermail/python-ideas/2015-January/031479.html ... https://mail.python.org/pipermail/python-ideas/2015-February/031532.html
It's been suggested that
check
would be sufficient, but TBHexpect_returncode
seems preferable because it should be relatively grep'able. -
reporter Maybe
assert_returncode
would be more palatable? -
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 withexpect_returncode
check_returncode
doesn't seem to have the same implications asexpect_
.Pending further input, I'll stick with
expect_returncode
. -
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). -
reporter https://bitbucket.org/vinay.sajip/sarge/pull-request/1/enh-add-check_call-check_output/diff
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).
Is this needed just for
Pipeline.run
? -
reporter I've added support for
expect_returncode
as a tuple or a list in "ENH: Support Pipeline.run expect_returncode=(int || tuple || list)"PR here: https://bitbucket.org/vinay.sajip/sarge/pull-request/1/enh-add-check_call-check_output
-
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. -
reporter No check is performed when
expect_returncode
isNone
(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] 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)
-
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)
- Log in to comment
e.g.
expect_returncode=0
orexpect_returncode=2