Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109171

Re: Handling exceptions with Py2 and Py3

From Pavlos Parissis <pavlos.parissis@gmail.com>
Newsgroups comp.lang.python
Subject Re: Handling exceptions with Py2 and Py3
Date 2016-05-27 15:42 +0200
Message-ID <mailman.20.1464356536.2277.python-list@python.org> (permalink)
References <57483C86.6030304@gmail.com> <mailman.18.1464351883.2277.python-list@python.org> <574842dc$0$1611$c3e8da3$5496439d@news.astraweb.com> <57484EAF.9060507@gmail.com>

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 27/05/2016 02:51 μμ, Steven D'Aprano wrote:
> On Fri, 27 May 2016 10:24 pm, Pavlos Parissis wrote:
> 
>> Hi,
>>
>> So, we have ConnectionRefusedError in Python3 but not in Python2.
>> Six module doesn't provide a wrapper about this.
>>
>> What is most efficient way to handle this situation in a try-catch block?
> 
> Um, could you give us a hint as to context?
> 

The following is inside a while true loop which implements a
retry logic.

try:

    unix_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    unix_socket.settimeout(0.5)
    unix_socket.connect(self.socket_file)
    unix_socket.send(six.b(command + '\n'))
    file_handle = unix_socket.makefile()
    data = file_handle.read().splitlines()
except ConnectionRefusedError:
    raised = SocketConnectionError(self.socket_file)
except socket.timeout:
    raised = SocketTimeout(socket_file=self.socket_file)
except OSError as error:

    if error.errno == 106:
        raised = SocketTransportError(socket_file=self.socket_file)
    else:
        raised = error
else:
    procecess(data)
finally:
    unix_socket.close()
    if raised:
        time.sleep(self.retry_interval)


> What does Python 2 raise instead of ConnectionRefusedError? I don't know, so
> for the sake of calling it *something* I'll say it's FooError. Then if it
> is a simple name, you can do this:
> 
> 
> if sys.version < "3":
>     ConnectionRefusedError = FooError
> 
> # much later
> try:
>    do_the_thing()
> except ConnectionRefusedError:
>     ...
> 
> 
> 
> Or instead of checking for the version, you can check for the name:
> 
> 
> try:
>     from some_module import ConnectionRefusedError
> except ImportError:
>     # Must be Python 2
>     from another_module import FooError as ConnectionRefusedError
> 
> 
> If neither of these techniques help, please give more context and we can be
> more specific.
> 
> 
> 

I will try the approach mentioned by Ben Finney as I am already checking 'errno'.

Thanks everyone for the replies.

Cheers,
Pavlos

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Handling exceptions with Py2 and Py3 Pavlos Parissis <pavlos.parissis@gmail.com> - 2016-05-27 14:24 +0200
  Re: Handling exceptions with Py2 and Py3 Steven D'Aprano <steve@pearwood.info> - 2016-05-27 22:51 +1000
    Re: Handling exceptions with Py2 and Py3 Pavlos Parissis <pavlos.parissis@gmail.com> - 2016-05-27 15:42 +0200

csiph-web