Skip to content

Commit

Permalink
fix #547: have username return the stringified UID in case UID cannot…
Browse files Browse the repository at this point in the history
… be resolved by the system (as opposed to letting KeyError propagate)
  • Loading branch information
giampaolo committed Nov 26, 2014
1 parent d14a6c3 commit 4dfb4ad
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -11,6 +11,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
**Bug fixes**

- #496: [Solaris]: can't import psutil.
- #547: [UNIX]: Process.username() may raise KeyError if UID can't be resolved.


2.1.3 - 2014-09-26
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -59,8 +59,8 @@ Example applications
See also:

* https://github.com/nicolargo/glances
* https://github.com/google/grr
* https://github.com/Jahaja/psdash
* https://code.google.com/p/grr/

==============
Example usages
Expand Down Expand Up @@ -210,7 +210,7 @@ Process management
>>>
>>> p.memory_info()
pmem(rss=7471104, vms=68513792)
>>> p.ext_memory_info()
>>> p.memory_info_ex()
extmem(rss=9662464, vms=49192960, shared=3612672, text=2564096, lib=0, data=5754880, dirty=0)
>>> p.memory_maps()
[pmmap_grouped(path='/lib/x86_64-linux-gnu/libutil-2.15.so', rss=16384, anonymous=8192, swap=0),
Expand Down
7 changes: 6 additions & 1 deletion psutil/__init__.py
Expand Up @@ -563,7 +563,12 @@ def username(self):
# might happen if python was installed from sources
raise ImportError(
"requires pwd module shipped with standard python")
return pwd.getpwuid(self.uids().real).pw_name
real_uid = self.uids().real
try:
return pwd.getpwuid(real_uid).pw_name
except KeyError:
# the uid can't be resolved by the system
return str(real_uid)
else:
return self._proc.username()

Expand Down

0 comments on commit 4dfb4ad

Please sign in to comment.