Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError when connection to Sphinx server fails #21

Open
GoogleCodeExporter opened this issue Jun 1, 2015 · 0 comments
Open

Comments

@GoogleCodeExporter
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant