Skip to content

Commit

Permalink
exceptions: ensure ProcessExecutionError can be pickled (#586)
Browse files Browse the repository at this point in the history
* exceptions: ensure ProcessExecutionError can be pickled

* fix: EnvironmentError is OSError in Python 3

* Update plumbum/commands/processes.py

Co-authored-by: Henry Schreiner <[email protected]>
  • Loading branch information
koreno and henryiii authored Feb 3, 2022
1 parent 7bba255 commit ba4b1a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions plumbum/commands/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,19 @@ def run(self):
# ===================================================================================================
# Exceptions
# ===================================================================================================
class ProcessExecutionError(EnvironmentError):
class ProcessExecutionError(OSError):
"""Represents the failure of a process. When the exit code of a terminated process does not
match the expected result, this exception is raised by :func:`run_proc
<plumbum.commands.run_proc>`. It contains the process' return code, stdout, and stderr, as
well as the command line used to create the process (``argv``)
"""

def __init__(self, argv, retcode, stdout, stderr, message=None):
super().__init__(self, argv, retcode, stdout, stderr)

# we can't use 'super' here since OSError only keeps the first 2 args,
# which leads to failuring in loading this object from a pickle.dumps.
Exception.__init__(self, argv, retcode, stdout, stderr)

self.message = message
self.argv = argv
self.retcode = retcode
Expand Down
7 changes: 7 additions & 0 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,13 @@ def test_quoting(self):
ls("-a", "") # check that empty strings are rendered correctly
assert execinfo.value.argv[-2:] == ["-a", ""]

def test_exception_pickling(self):
import pickle

with pytest.raises(ProcessExecutionError) as exc_info:
local.cmd.ls("no-file")
assert pickle.loads(pickle.dumps(exc_info.value)).argv == exc_info.value.argv

def test_tempdir(self):
with local.tempdir() as dir:
assert dir.is_dir()
Expand Down

0 comments on commit ba4b1a0

Please sign in to comment.