66"""
77import logging
88import os
9+ import shutil
10+ import subprocess
911import typing as t
1012
1113from libtmux .common import tmux_cmd
@@ -115,6 +117,31 @@ def __init__(
115117 if colors :
116118 self .colors = colors
117119
120+ def is_alive (self ) -> bool :
121+ try :
122+ res = self .cmd ("list-sessions" )
123+ print (f"returncode: { res .returncode } " )
124+ print (f"stdout: { res .stdout } " )
125+ print (f"stderr: { res .stderr } " )
126+ return res .returncode == 0
127+ except Exception :
128+ return False
129+
130+ def raise_if_dead (self ) -> None :
131+ tmux_bin = shutil .which ("tmux" )
132+ if tmux_bin is None :
133+ raise exc .TmuxCommandNotFound ()
134+
135+ cmd_args : t .List [str ] = ["list-sessions" ]
136+ if self .socket_name :
137+ cmd_args .insert (0 , f"-L{ self .socket_name } " )
138+ if self .socket_path :
139+ cmd_args .insert (0 , f"-S{ self .socket_path } " )
140+ if self .config_file :
141+ cmd_args .insert (0 , f"-f{ self .config_file } " )
142+
143+ subprocess .check_call ([tmux_bin ] + cmd_args )
144+
118145 def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> tmux_cmd :
119146 """
120147 Execute tmux command and return output.
@@ -207,7 +234,10 @@ def list_sessions(self) -> t.List[Session]:
207234 @property
208235 def sessions (self ) -> t .List [Session ]:
209236 """Property / alias to return :meth:`~.list_sessions`."""
210- return self .list_sessions ()
237+ try :
238+ return self .list_sessions ()
239+ except Exception :
240+ return []
211241
212242 #: Alias :attr:`sessions` for :class:`~libtmux.common.TmuxRelationalObject`
213243 children = sessions # type: ignore
@@ -348,7 +378,7 @@ def _update_panes(self) -> "Server":
348378 return self
349379
350380 @property
351- def attached_sessions (self ) -> t .Optional [ t . List [Session ] ]:
381+ def attached_sessions (self ) -> t .List [Session ]:
352382 """
353383 Return active :class:`Session` objects.
354384
@@ -357,19 +387,22 @@ def attached_sessions(self) -> t.Optional[t.List[Session]]:
357387 list of :class:`Session`
358388 """
359389
360- sessions = self ._sessions
361- attached_sessions = list ()
390+ try :
391+ sessions = self ._sessions
392+ attached_sessions = list ()
362393
363- for session in sessions :
364- attached = session .get ("session_attached" )
365- # for now session_active is a unicode
366- if attached != "0" :
367- logger .debug ("session %s attached" , session .get ("name" ))
368- attached_sessions .append (session )
369- else :
370- continue
394+ for session in sessions :
395+ attached = session .get ("session_attached" )
396+ # for now session_active is a unicode
397+ if attached != "0" :
398+ logger .debug ("session %s attached" , session .get ("name" ))
399+ attached_sessions .append (session )
400+ else :
401+ continue
371402
372- return [Session (server = self , ** s ) for s in attached_sessions ] or None
403+ return [Session (server = self , ** s ) for s in attached_sessions ] or []
404+ except Exception :
405+ return []
373406
374407 def has_session (self , target_session : str , exact : bool = True ) -> bool :
375408 """
0 commit comments