-
-
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
Switch OSError to psutil.AccessDenied for net_if_stats failure #860
Comments
What is the reason for this exception in the first place? Does this amount to needing to run |
As a slightly simpler example of this problem (as has been suggested to me), please see the following. This one is done with Python 2.7.12 in iPython 5.0.0. Though a In [1]: import psutil
In [2]: psutil.net_if_stats()
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-2-06c5562e3b64> in <module>()
----> 1 psutil.net_if_stats()
/groups/dudman/home/kirkhamj/miniconda/envs/nanshenv2/lib/python2.7/site-packages/psutil/__init__.pyc in net_if_stats()
1959 - mtu: the maximum transmission unit expressed in bytes.
1960 """
-> 1961 return _psplatform.net_if_stats()
1962
1963
/groups/dudman/home/kirkhamj/miniconda/envs/nanshenv2/lib/python2.7/site-packages/psutil/_pslinux.pyc in net_if_stats()
789 ret = {}
790 for name in names:
--> 791 isup, duplex, speed, mtu = cext.net_if_stats(name)
792 duplex = duplex_map[duplex]
793 ret[name] = _common.snicstats(isup, duplex, speed, mtu)
OSError: [Errno 1] Operation not permitted |
The reason is to have a single custom exception which can be catched across all different supported platforms and Python versions. On UNIX we may get
...and that's it. The exact same reason applies to |
Yes, there's nothing else you can do. The error comes from the kernel, which is preventing you to perform that specific operation 'cause you're a limited user, and using sudo / su is the only possible workaround. |
Looking back at this, I think it is better to let OSError propagate. So far AccessDenied is only raised for methods of the Process class. Extending the same behavior to system-related APIs would be a first in psutil, and since I consider this a pretty rare corner case I don't want to introduce this "difference". |
In some cases, a user may not have the proper permissions to run
net_if_stats
. If that is the case, anOSError
is currently raised. It may be more appropriate to raisepsutil.AccessDenied
or at least it is a possibility worth considering ( #797 (comment) ). See an example of what this error looks like now below.The text was updated successfully, but these errors were encountered: