]> kaliko git repositories - python-musicpd.git/commitdiff
mpd.py: raise ConnectionError when trying to use an unconnected socket
authorJ. Alexander Treuman <jat@spatialrift.net>
Sun, 23 Mar 2008 19:50:48 +0000 (15:50 -0400)
committerJ. Alexander Treuman <jat@spatialrift.net>
Sun, 23 Mar 2008 19:50:48 +0000 (15:50 -0400)
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

diff --git a/mpd.py b/mpd.py
index e16c8b8aa584ca7a5b5db0fa86faf002cc0afd5c..02d27880d35759de8ea4cc960055720804703d23 100644 (file)
--- 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):