From 398350e643c4b873c366d9b7a31c7c80736054d2 Mon Sep 17 00:00:00 2001 From: John de Largentaye Date: Wed, 16 Aug 2017 16:51:46 -0700 Subject: [PATCH] Make Server.has_session() use returncode has_session() would erroneously return true if tmux returned an unexpected string, such as "no current session". Instead of adding yet another string to the list to check against, use the return code of the 'tmux has-session' command, which is documented to return 0 (Shell true) if targeted session exists, and return 1 (Shell false) in any other case. --- libtmux/common.py | 3 +++ libtmux/server.py | 19 +++---------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index cb6cfa405..103b1c61b 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -189,6 +189,7 @@ def __init__(self, *args, **kwargs): self.process.stdout.close() stderr = self.process.stderr.read() self.process.stderr.close() + returncode = self.process.returncode except Exception as e: logger.error( 'Exception for %s: \n%s' % ( @@ -197,6 +198,8 @@ def __init__(self, *args, **kwargs): ) ) + self.returncode = returncode + self.stdout = console_to_str(stdout) self.stdout = self.stdout.split('\n') self.stdout = list(filter(None, self.stdout)) # filter empty values diff --git a/libtmux/server.py b/libtmux/server.py index 7a1655933..74f06f25b 100644 --- a/libtmux/server.py +++ b/libtmux/server.py @@ -334,24 +334,11 @@ def has_session(self, target_session, exact=True): proc = self.cmd('has-session', '-t%s' % target_session) - if not proc.stdout: - return True - if any( - x in proc.stdout for x in - ['failed to connect to server', 'error connecting to'] - ): - return False - elif 'no server running' in proc.stdout: # tmux 2.0 - return False - elif 'can\'t find session' in proc.stdout: # tmux 2.1 - return False - elif 'bad session name' in proc.stdout: # tmux >= 1.9 - return False - elif 'session not found' in proc.stdout: - return False - else: + if not proc.returncode: return True + return False + def kill_server(self): """``$ tmux kill-server``.""" self.cmd('kill-server')