Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109171
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Pavlos Parissis <pavlos.parissis@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Handling exceptions with Py2 and Py3 |
| Date | Fri, 27 May 2016 15:42:07 +0200 |
| Lines | 129 |
| 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> |
| Mime-Version | 1.0 |
| Content-Type | multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="fQ13x7Si4vgcO00xa7P2fm5X7pGOX1UWr" |
| X-Trace | news.uni-berlin.de fGHZJWTHscc+lYJfSeHkbQbF8nvi9pNhCfPZfk5kTpLA== |
| Return-Path | <pavlos.parissis@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'context': 0.05; 'error:': 0.05; 'finally:': 0.05; 'importerror:': 0.05; 'python3': 0.05; 'implements': 0.07; 'wrapper': 0.07; 'oserror': 0.09; 'retry': 0.09; 'sake': 0.09; 'specific.': 0.09; 'python': 0.10; 'skip:f 30': 0.15; '2016': 0.16; 'error.errno': 0.16; 'filename:fname piece:signature': 0.16; 'hint': 0.16; 'received:192.168.0.103': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'later': 0.16; 'skip:u 30': 0.18; 'try:': 0.18; 'cheers,': 0.22; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'error': 0.27; 'checking': 0.27; 'fri,': 0.27; 'this.': 0.28; 'skip:u 20': 0.28; 'raise': 0.29; 'version,': 0.30; 'skip:s 30': 0.31; 'everyone': 0.31; 'help,': 0.32; "d'aprano": 0.33; 'raised': 0.33; 'steven': 0.33; "i'll": 0.33; 'message-id:@gmail.com': 0.34; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'instead': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'thanks': 0.37; 'skip:s 50': 0.37; 'skip:s 40': 0.38; 'hi,': 0.38; 'data': 0.39; 'does': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'skip:u 10': 0.61; 'provide': 0.61; 'more': 0.63; 'six': 0.65; 'techniques': 0.65; 'situation': 0.67; 'replies.': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to; bh=s6NguY+6z9A6eENPu5cNFgTXWt1trTTtyDkmF6hLsbs=; b=EXplrkV6jYbY1NskCAIl+DXaW8sc+MgZNecx+3E4LtX6qM/Z28KwIrAofuSoCMN/46 p5WN8+m5TfwD6x7IdbfqvFg92MKFOIo1O+TwoSA+90GCAj6PPxemDtWhAdXlc4l2DGLR HtCzhVaNhxn1MV+GEr+ASzwzpLVJ4/QRK2ifbT8NIg7qBJBCwe1Lvvsj0RJcYzwjP1Pf ceO5P0Aj29wHgDfk12WdTUAVPyBgOxVAdDyy6ZIC24wOooGl0ODxOThnKxf9Sf2BKG2B pRODZ0lLHdH7Es6NII95QbXiPipLsGGISPCYq8qkNMarkIA0KWTm2CQQgSuzLw69CdEF 2t/A== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to; bh=s6NguY+6z9A6eENPu5cNFgTXWt1trTTtyDkmF6hLsbs=; b=bq3cJy30Z7tqqjM2r10yzz8O0vrY3lxNMxNChHT1wnFL6LePBVP0oyN/odo8PquA1+ RKOc8izOl4aKouuKszTr5Ir972QUcKnEZeFkhBB1IV16rC+M9rWxv6lLGXGNDYfbXMJj 7o2qh17TOXe7OZj5N3xfrkn7zU6vy65We4ArUEPYA3FRTRR2kAPt4iqQm4EAr+UQ3URr x/4oZ6COK1T9u5y+4DMKHzzTw5zYo1FPvu8PQFc7Dm+o1YHtB2MGclCvWPTpJKvuAV95 J35pCgITTL9ljBw75RtYYymPC5f1u8XVODOcKegOS69LLjfeDGgv11rzUr6gut3CGXya rKVA== |
| X-Gm-Message-State | ALyK8tLDPvpAehkvcYHWuZequqtq4wmgDzWANyb0Xb7CQnirAdB93VdCExRR/nxzZwkzUQ== |
| X-Received | by 10.194.58.8 with SMTP id m8mr15439058wjq.98.1464356530432; Fri, 27 May 2016 06:42:10 -0700 (PDT) |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.8.0 |
| In-Reply-To | <574842dc$0$1611$c3e8da3$5496439d@news.astraweb.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <57484EAF.9060507@gmail.com> |
| X-Mailman-Original-References | <57483C86.6030304@gmail.com> <mailman.18.1464351883.2277.python-list@python.org> <574842dc$0$1611$c3e8da3$5496439d@news.astraweb.com> |
| Xref | csiph.com comp.lang.python:109171 |
Show key headers only | 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 | Next — Previous in thread | Find similar | Unroll 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