Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pickle support to psutil Exceptions #2380

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -824,4 +824,8 @@ I: 2361

N: Shade Gladden
W: https://github.com/shadeyg56
I: 2376
I: 2376

N: Anthony Ryan
W: https://github.com/anthonyryan1
I: 2272
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- 2360_, [macOS]: can't compile on macOS < 10.13. (patch by Ryan Schmidt)
- 2254_, [Linux]: offline cpus raise NotImplementedError in cpu_freq() (patch by Shade Gladden)
- 2272_, Add pickle support to psutil Exceptions

5.9.8
=====
Expand Down
12 changes: 12 additions & 0 deletions psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ def __init__(self, pid, name=None, msg=None):
self.name = name
self.msg = msg or "process no longer exists"

def __reduce__(self):
return (self.__class__, (self.pid, self.name, self.msg))


class ZombieProcess(NoSuchProcess):
"""Exception raised when querying a zombie process. This is
Expand All @@ -347,6 +350,9 @@ def __init__(self, pid, name=None, ppid=None, msg=None):
self.ppid = ppid
self.msg = msg or "PID still exists but it's a zombie"

def __reduce__(self):
return (self.__class__, (self.pid, self.name, self.ppid, self.msg))


class AccessDenied(Error):
"""Exception raised when permission to perform an action is denied."""
Expand All @@ -359,6 +365,9 @@ def __init__(self, pid=None, name=None, msg=None):
self.name = name
self.msg = msg or ""

def __reduce__(self):
return (self.__class__, (self.pid, self.name, self.msg))


class TimeoutExpired(Error):
"""Raised on Process.wait(timeout) if timeout expires and process
Expand All @@ -374,6 +383,9 @@ def __init__(self, seconds, pid=None, name=None):
self.name = name
self.msg = "timeout after %s seconds" % seconds

def __reduce__(self):
return (self.__class__, (self.seconds, self.pid, self.name))


# ===================================================================
# --- utils
Expand Down
39 changes: 39 additions & 0 deletions psutil/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,45 @@ def check(ret):
check(psutil.disk_usage(os.getcwd()))
check(psutil.users())

b = pickle.loads(
pickle.dumps(
psutil.NoSuchProcess(
pid=4567, name='name test', msg='msg test'
)
)
)
self.assertEqual(b.pid, 4567)
self.assertEqual(b.name, 'name test')
self.assertEqual(b.msg, 'msg test')

b = pickle.loads(
pickle.dumps(
psutil.ZombieProcess(
pid=4567, name='name test', ppid=42, msg='msg test'
)
)
)
self.assertEqual(b.pid, 4567)
self.assertEqual(b.ppid, 42)
self.assertEqual(b.name, 'name test')
self.assertEqual(b.msg, 'msg test')

b = pickle.loads(
pickle.dumps(psutil.AccessDenied(pid=123, name='name', msg='msg'))
)
self.assertEqual(b.pid, 123)
self.assertEqual(b.name, 'name')
self.assertEqual(b.msg, 'msg')

b = pickle.loads(
pickle.dumps(
psutil.TimeoutExpired(seconds=33, pid=4567, name='name')
)
)
self.assertEqual(b.seconds, 33)
self.assertEqual(b.pid, 4567)
self.assertEqual(b.name, 'name')

# # XXX: https://github.com/pypa/setuptools/pull/2896
# @unittest.skipIf(APPVEYOR, "temporarily disabled due to setuptools bug")
# def test_setup_script(self):
Expand Down
Loading