shakahl / sphinxsearch

Automatically exported from code.google.com/p/sphinxsearch
GNU General Public License v2.0
1 stars 0 forks source link

UnboundLocalError when connection to Sphinx server fails #21

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
When the Sphinx Python API fails to connect to the Sphinx server, a 
UnboundLocalError happens because a variable isn't correctly assigned... This 
shouldn't happen at all.. 

_Connect() method:

        try:
                        ........
            sock = socket.socket ( af, socket.SOCK_STREAM )   <--- this raises socket.error
            sock.settimeout ( self._timeout )
            sock.connect ( addr )
        except socket.error, msg:
            if sock:                  <--- sock doesn't exist!!! UnboundLocalError will be raised
                sock.close()
            self._error = 'connection to %s failed (%s)' % ( desc, msg )
            return

Solution:
Just assign the sock variable first to None to make sure is exists.

Put
    sock = None
before the try statement:

                sock = None
        try:
                        ........
            sock = socket.socket ( af, socket.SOCK_STREAM )
            sock.settimeout ( self._timeout )
            sock.connect ( addr )
        except socket.error, msg:
            if sock:
                sock.close()
            self._error = 'connection to %s failed (%s)' % ( desc, msg )
            return

Original issue reported on code.google.com by pedrof.a...@gmail.com on 25 Oct 2013 at 11:04