From 11ad737dbfa521db07486a6cb789d3539a5ec4c0 Mon Sep 17 00:00:00 2001 From: "J. Alexander Treuman" Date: Sun, 23 Mar 2008 15:50:48 -0400 Subject: [PATCH] mpd.py: raise ConnectionError when trying to use an unconnected socket ConnectionError will now be raised when trying to read from/write to a socket before calling connect() or after calling disconnect(). In the past it would try to complete the operation on the unconnected socket, resulting in a socket.error exception. --- mpd.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mpd.py b/mpd.py index e16c8b8..02d2788 100644 --- a/mpd.py +++ b/mpd.py @@ -39,6 +39,13 @@ class CommandListError(MPDError): pass +class _NotConnected(object): + def __getattr__(self, attr): + return self._dummy + + def _dummy(*args): + raise ConnectionError, "Not connected" + class MPDClient(object): def __init__(self): self.iterate = False @@ -275,12 +282,15 @@ class MPDClient(object): def _reset(self): self.mpd_version = None self._commandlist = None - self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self._sockfile = self._sock.makefile("rb+") + self._sock = None + self._sockfile = _NotConnected() def connect(self, host, port): - self.disconnect() + if self._sock: + self.disconnect() + self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._sock.connect((host, port)) + self._sockfile = self._sock.makefile("rb+") self._hello() def disconnect(self): -- 2.39.5