-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Improved exceptions (raise from ...
)
#2487
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed-off-by: Giampaolo Rodola <[email protected]>
Signed-off-by: Giampaolo Rodola <[email protected]>
When passing a non existent PID to Process(), we now get a very compact traceback: ``` Traceback (most recent call last): File "/home/giampaolo/svn/psutil/foo.py", line 5, in <module> p = psutil.Process(123124) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 317, in __init__ self._init(pid) File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 363, in _init raise NoSuchProcess(pid, msg=msg) from None psutil.NoSuchProcess: process PID not found (pid=123124) ``` Before we got: ``` <traceback object at 0x74d0dd6ddb40> Traceback (most recent call last): File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 327, in _init _psplatform.cext.check_pid_range(pid) OverflowError: Python int too large to convert to C long During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/giampaolo/svn/psutil/foo.py", line 5, in <module> p = psutil.Process(132798127392783333912839812793) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 317, in __init__ self._init(pid) File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 332, in _init raise NoSuchProcess(pid, msg=msg).with_traceback(tb) File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 327, in _init _psplatform.cext.check_pid_range(pid) psutil.NoSuchProcess: process PID out of range (pid=132798127392783333912839812793) ~/svn/psutil {raise-from}$ ^C ~/svn/psutil {raise-from}$ ^C ~/svn/psutil {raise-from}$ python3 foo.py <traceback object at 0x7ef45b787800> Traceback (most recent call last): File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 327, in _init _psplatform.cext.check_pid_range(pid) OverflowError: Python int too large to convert to C long The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/giampaolo/svn/psutil/foo.py", line 5, in <module> p = psutil.Process(132798127392783333912839812793) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 317, in __init__ self._init(pid) File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 332, in _init raise NoSuchProcess(pid, msg=msg) from e psutil.NoSuchProcess: process PID out of range (pid=132798127392783333912839812793) ~/svn/psutil {raise-from}$ ^C ~/svn/psutil {raise-from}$ python3 foo.py Traceback (most recent call last): File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 1647, in wrapper return fun(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_common.py", line 464, in wrapper raise err from None File "/home/giampaolo/svn/psutil/psutil/_common.py", line 462, in wrapper return fun(self) ^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 1713, in _parse_stat_file data = bcat(f"{self._procfs_path}/{self.pid}/stat") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_common.py", line 794, in bcat return cat(fname, fallback=fallback, _open=open_binary) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_common.py", line 782, in cat with _open(fname) as f: ^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_common.py", line 746, in open_binary return open(fname, "rb", buffering=FILE_READ_BUFFER_SIZE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: '/proc/123124/stat' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 350, in _init self._ident = self._get_ident() ^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 391, in _get_ident return (self.pid, self.create_time()) ^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 773, in create_time self._create_time = self._proc.create_time() ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 1647, in wrapper return fun(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 1885, in create_time ctime = float(self._parse_stat_file()['create_time']) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 1659, in wrapper raise NoSuchProcess(self.pid, self._name) psutil.NoSuchProcess: process no longer exists (pid=123124) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/giampaolo/svn/psutil/foo.py", line 5, in <module> p = psutil.Process(123124) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 317, in __init__ self._init(pid) File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 363, in _init raise NoSuchProcess(pid, msg=msg) psutil.NoSuchProcess: process PID not found (pid=123124) ```
Code: ``` p = psutil.Popen(["ls"]) p.foo ``` Trceback now: ``` Traceback (most recent call last): File "/home/giampaolo/svn/psutil/foo.py", line 16, in <module> p.foo() ^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 1442, in __getattribute__ raise AttributeError(msg) from None AttributeError: <class 'psutil.Popen'> class no attribute 'foo' ``` Traback before: ``` Traceback (most recent call last): File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 1436, in __getattribute__ return object.__getattribute__(self, name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Popen' object has no attribute 'foo' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 1439, in __getattribute__ return object.__getattribute__(self.__subproc, name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Popen' object has no attribute 'foo' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/giampaolo/svn/psutil/foo.py", line 16, in <module> p.foo() ^^^^^ File "/home/giampaolo/svn/psutil/psutil/__init__.py", line 1445, in __getattribute__ raise AttributeError(msg) AttributeError: Popen instance has no attribute 'foo' ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As part of the dropping of Python 2.7 support (#2480), we can now take advantage of chained exceptions machinery (
raise x from y
andraise x from None
). In practical terms, this is what changes:Shorter tracebacks
When adding the full traceback info adds no value, we now shorten tracebacks if
raise X from None
.A similar (hackish) attempt was made in 633d8019, when we were still stuck with Python 2. The notable example is passing a PID that does not exist to the
Process
class:Before we got:
Now we get:
Different wording for "translated" exceptions
By "translated" I mean psutil's
NoSuchProcess
,ZombieProcess
andAccessDenied
.Given the following code:
Before we got:
Now we get:
Diff: