From: J. Alexander Treuman Date: Sun, 23 Mar 2008 19:50:48 +0000 (-0400) Subject: mpd.py: raise ConnectionError when trying to use an unconnected socket X-Git-Tag: v0.2.0~12 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=11ad737dbfa521db07486a6cb789d3539a5ec4c0;p=python-musicpd.git 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. --- 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):