Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109168 > unrolled thread
| Started by | Pavlos Parissis <pavlos.parissis@gmail.com> |
|---|---|
| First post | 2016-05-27 14:24 +0200 |
| Last post | 2016-05-27 15:42 +0200 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Pavlos Parissis <pavlos.parissis@gmail.com> |
|---|---|
| Date | 2016-05-27 14:24 +0200 |
| Subject | Handling exceptions with Py2 and Py3 |
| Message-ID | <mailman.18.1464351883.2277.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
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? Cheers, Pavlos
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-05-27 22:51 +1000 |
| Message-ID | <574842dc$0$1611$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #109168 |
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?
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.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Pavlos Parissis <pavlos.parissis@gmail.com> |
|---|---|
| Date | 2016-05-27 15:42 +0200 |
| Message-ID | <mailman.20.1464356536.2277.python-list@python.org> |
| In reply to | #109169 |
[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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web