Skip to content

Commit

Permalink
fix #692: no longer cache Process.name() on POSIX as it may change
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 16, 2015
1 parent 7e0dbd9 commit 999dc85
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Bug tracker at https://github.com/giampaolo/psutil/issues

3.2.3 - XXXX-XX-XX
==================

**Bug fixes**

- #692: [UNIX] Process.name() is no longer cached as it may change.


3.2.2 - 2015-10-04
==================

Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ Process class

.. method:: name()

The process name. The return value is cached after first call.
The process name.

.. method:: exe()

Expand Down
44 changes: 24 additions & 20 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
]
__all__.extend(_psplatform.__extra__all__)
__author__ = "Giampaolo Rodola'"
__version__ = "3.2.2"
__version__ = "3.2.3"
version_info = tuple([int(num) for num in __version__.split('.')])
AF_LINK = _psplatform.AF_LINK
_TOTAL_PHYMEM = None
Expand Down Expand Up @@ -519,25 +519,29 @@ def ppid(self):

def name(self):
"""The process name. The return value is cached after first call."""
if self._name is None:
name = self._proc.name()
if _POSIX and len(name) >= 15:
# On UNIX the name gets truncated to the first 15 characters.
# If it matches the first part of the cmdline we return that
# one instead because it's usually more explicative.
# Examples are "gnome-keyring-d" vs. "gnome-keyring-daemon".
try:
cmdline = self.cmdline()
except AccessDenied:
pass
else:
if cmdline:
extended_name = os.path.basename(cmdline[0])
if extended_name.startswith(name):
name = extended_name
self._proc._name = name
self._name = name
return self._name
# Process name is only cached on Windows as on POSIX it may
# change, see:
# https://github.com/giampaolo/psutil/issues/692
if _WINDOWS and self._name is not None:
return self._name
name = self._proc.name()
if _POSIX and len(name) >= 15:
# On UNIX the name gets truncated to the first 15 characters.
# If it matches the first part of the cmdline we return that
# one instead because it's usually more explicative.
# Examples are "gnome-keyring-d" vs. "gnome-keyring-daemon".
try:
cmdline = self.cmdline()
except AccessDenied:
pass
else:
if cmdline:
extended_name = os.path.basename(cmdline[0])
if extended_name.startswith(name):
name = extended_name
self._name = name
self._proc._name = name
return name

def exe(self):
"""The process executable as an absolute path.
Expand Down

0 comments on commit 999dc85

Please sign in to comment.