From: J. Alexander Treuman Date: Mon, 29 Nov 2010 01:36:36 +0000 (-0500) Subject: mpd.py: updating _connect_tcp() with new socket code X-Git-Tag: v0.3.0~16 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=46753bf1c1e917dee5ba9ced19fbf1f6a9c103ca;p=python-musicpd.git mpd.py: updating _connect_tcp() with new socket code _connect_tcp() is largely based on Python's socket.create_connection(). Previously, this code contained two bugs related to raising exceptions. The first bug was introduced by my clumsy attempt to update the code to use the new preferred method of raising exceptions (the same mistake was made in the Python 3 port of the socket module). Instead of raising socket.error with the value of the original exception, socket.error was raised with the original exception as the value of the new exception, thus nesting an exception within an exception. Python 3.1.3 fixed this by simply re-raising the original exception. The second bug is hit when getaddrinfo() returns an empty list. A socket.error is raised with a single string as its argument, instead of a 2-tuple as required by its parent class, IOError. This bug continues to persist in Python 3.1.3 as well as the latest svn tree. This commit updates _connect_tcp() to be a nearly identical copy of the Python 3.1.3 version of socket.create_connection(), except that ConnectionError is raised when getaddrinfo() returns an empty list. --- diff --git a/mpd.py b/mpd.py index 5e50295..b87891b 100644 --- a/mpd.py +++ b/mpd.py @@ -364,23 +364,23 @@ class MPDClient(object): flags = socket.AI_ADDRCONFIG except AttributeError: flags = 0 - msg = "getaddrinfo returns an empty list" + err = None for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, flags): af, socktype, proto, canonname, sa = res + sock = None try: sock = socket.socket(af, socktype, proto) sock.connect(sa) - except socket.error, msg: - if sock: + return sock + except socket.error, err: + if sock is not None: sock.close() - sock = None - continue - break - if not sock: - raise socket.error(msg) - return sock + if err is not None: + raise err + else: + raise ConnectionError("getaddrinfo returns an empty list") def connect(self, host, port): if self._sock: