From: Kaliko Jack Date: Mon, 15 Apr 2013 18:13:57 +0000 (+0200) Subject: Handles better lost connections X-Git-Tag: v0.3.1b~3 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=92a072f152417f607bd4cc5f549972ba76a15cad;p=python-musicpd.git Handles better lost connections --- diff --git a/mpd.py b/mpd.py index 4ad4804..373bd16 100644 --- a/mpd.py +++ b/mpd.py @@ -236,6 +236,7 @@ class MPDClient(object): def _read_line(self): line = self._rfile.readline() if not line.endswith("\n"): + self.disconnect() raise ConnectionError("Connection lost while reading line") line = line.rstrip("\n") if line.startswith(ERROR_PREFIX): @@ -414,7 +415,7 @@ class MPDClient(object): if sock is not None: sock.close() if err is not None: - raise err + raise ConnectionError(str(err)) else: raise ConnectionError("getaddrinfo returns an empty list") @@ -434,9 +435,15 @@ class MPDClient(object): raise def disconnect(self): - self._rfile.close() - self._wfile.close() - self._sock.close() + if isinstance(self._rfile, socket._fileobject): + print('closing r socket') + self._rfile.close() + if isinstance(self._wfile, socket._fileobject): + print('closing w socket') + self._wfile.close() + if isinstance(self._sock, socket.socket): + print('closing socket') + self._sock.close() self._reset() def fileno(self):