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

Clearer tracebacks in case of chained exceptions #2196

Merged
merged 6 commits into from
Jan 24, 2023
Merged

Conversation

giampaolo
Copy link
Owner

@giampaolo giampaolo commented Jan 24, 2023

See #2195. This PR removes the KeyError part from the traceback below:

Traceback (most recent call last):
  File "/home/giampaolo/svn/psutil/psutil/_common.py", line 422, in wrapper
    return cache[key]
           ~~~~~^^^^^
KeyError: (('/proc',), frozenset())

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 285, in <module>
    set_scputimes_ntuple("/proc")
  File "/home/giampaolo/svn/psutil/psutil/_common.py", line 425, in wrapper
    ret = cache[key] = fun(*args, **kwargs)
                       ^^^^^^^^^^^^^^^^^^^^
  File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 268, in set_scputimes_ntuple
    with open_binary("/etc/.pwd.lock") as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/giampaolo/svn/psutil/psutil/_common.py", line 761, in open_binary
    return open(fname, "rb", buffering=FILE_READ_BUFFER_SIZE)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/stat'

The KeyError bit (or AttributeError bit) shows up frequently in the chain of exceptions, before psutil raises AccessDenied or NoSuchProcess. KeyError has to do with the internal caching mechanism (an internal detail) and user doesn't need to know about that (it adds no value really), so let's just hide it. New traceback looks like this:

Traceback (most recent call last):
  File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 285, in <module>
    set_scputimes_ntuple("/proc")
  File "/home/giampaolo/svn/psutil/psutil/_common.py", line 427, in wrapper
    raise raise_from(err, None)
          ^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 3, in raise_from
  File "/home/giampaolo/svn/psutil/psutil/_common.py", line 425, in wrapper
    ret = cache[key] = fun(*args, **kwargs)
                       ^^^^^^^^^^^^^^^^^^^^
  File "/home/giampaolo/svn/psutil/psutil/_pslinux.py", line 268, in set_scputimes_ntuple
    with open_binary("/etc/.pwd.lock") as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/giampaolo/svn/psutil/psutil/_common.py", line 760, in open_binary
    return open(fname, "rb", buffering=FILE_READ_BUFFER_SIZE)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/stat'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant