diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 01c18a20..56a51f6c 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -517,8 +517,14 @@ try: if data: yield interface, data.bytes_recv, data.bytes_sent - def _get_user(segment_info): - return psutil.Process(os.getpid()).username + # psutil-2.0.0: psutil.Process.username is unbound method + if callable(psutil.Process.username): + def _get_user(segment_info): + return psutil.Process(os.getpid()).username() + # pre psutil-2.0.0: psutil.Process.username has type property + else: + def _get_user(segment_info): + return psutil.Process(os.getpid()).username class CPULoadPercentSegment(ThreadedSegment): interval = 1 diff --git a/tests/test_segments.py b/tests/test_segments.py index 48fde0c2..bfea4f6a 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -169,7 +169,18 @@ class TestCommon(TestCase): def test_user(self): new_os = new_module('os', getpid=lambda: 1) - new_psutil = new_module('psutil', Process=lambda pid: Args(username='def')) + + class Process(object): + def __init__(self, pid): + pass + + def username(self): + return 'def' + + if hasattr(common, 'psutil') and not callable(common.psutil.Process.username): + username = property(username) + + new_psutil = new_module('psutil', Process=Process) pl = Pl() with replace_env('USER', 'def') as segment_info: common.username = False