Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Pavlos Parissis 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: References: <57483C86.6030304@gmail.com> <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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <57484EAF.9060507@gmail.com> X-Mailman-Original-References: <57483C86.6030304@gmail.com> <574842dc$0$1611$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:109171 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --fQ13x7Si4vgcO00xa7P2fm5X7pGOX1UWr Content-Type: multipart/mixed; boundary="GCoaW32moTQ5WrcXafIpDMX5qilA62LjA" From: Pavlos Parissis To: python-list@python.org Message-ID: <57484EAF.9060507@gmail.com> Subject: Re: Handling exceptions with Py2 and Py3 References: <57483C86.6030304@gmail.com> <574842dc$0$1611$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <574842dc$0$1611$c3e8da3$5496439d@news.astraweb.com> --GCoaW32moTQ5WrcXafIpDMX5qilA62LjA Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 27/05/2016 02:51 =CE=BC=CE=BC, Steven D'Aprano wrote: > On Fri, 27 May 2016 10:24 pm, Pavlos Parissis wrote: >=20 >> 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 blo= ck? >=20 > Um, could you give us a hint as to context? >=20 The following is inside a while true loop which implements a retry logic. try: unix_socket =3D 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 =3D unix_socket.makefile() data =3D file_handle.read().splitlines() except ConnectionRefusedError: raised =3D SocketConnectionError(self.socket_file) except socket.timeout: raised =3D SocketTimeout(socket_file=3Dself.socket_file) except OSError as error: if error.errno =3D=3D 106: raised =3D SocketTransportError(socket_file=3Dself.socket_file) else: raised =3D 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 kno= w, 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: >=20 >=20 > if sys.version < "3": > ConnectionRefusedError =3D FooError >=20 > # much later > try: > do_the_thing() > except ConnectionRefusedError: > ... >=20 >=20 >=20 > Or instead of checking for the version, you can check for the name: >=20 >=20 > try: > from some_module import ConnectionRefusedError > except ImportError: > # Must be Python 2 > from another_module import FooError as ConnectionRefusedError >=20 >=20 > If neither of these techniques help, please give more context and we ca= n be > more specific. >=20 >=20 >=20 I will try the approach mentioned by Ben Finney as I am already checking = 'errno'. Thanks everyone for the replies. Cheers, Pavlos --GCoaW32moTQ5WrcXafIpDMX5qilA62LjA-- --fQ13x7Si4vgcO00xa7P2fm5X7pGOX1UWr Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJXSE6vAAoJEIP8ktofcXa5bEoP/jATA8q00/kb4rxggZiSQZgZ 9L+AXyJXdrzcI94vsp88RzvzlPRzEJg5GtWwy8HSrnZVd7gIygkM3GawVaG6rFne 5xAgsc/dpbwwr4NJtDm7/aAp9hAw8JkAQ3+iynbekPRK1r3sQMTb67CSk2NzpgqH vmr7sqFEuFBbd463sFXB+2Pw3jaFA1cTtEFnSlnmy89MQzsgL9P9a6Y+VZKDhVz0 0MFJPFyieJO/OhGj6uJiTWzJj7mvsHhx71EzVQ//kUPCFCryEOMGGixi4tZaXHIH InEQcGp1BE+/JFT1cb46tRdCYe5GQmisWCNfc10+vNa0F8T5GHVGmZpkseamAAfk 9Nc3pBSqcdxUH2Tt+aN4mkaWYEHd6XDD5G47k/ba3XczObHIHaWfYbcwc2Ljaac4 n7HOok6/5anigivfRgE1KWFMj4hQFFiJLO7KWxZShZgVNGBKvKj/pESDLHsZ5GMc Z5LzyPs417vY8KE/BXR99TA+0ZAVXFFO5LIKphrqkw5QNnV/bV2pPYrMpxT5V6Yw A7Z+2wJxuoyAx9y1F0zPbGxIcVyMJ7WUU/Eva5e4MIHjPA3DOVie1pkxCMTFDGQY 8Un2EOfPm4sf4Wbq/KbJxJ4KaFmp6fk+gsiXajYgQ98wOhJ6KQLrwyIb9leyZ7kd GHtmyi7ur8akAxoZqi/x =aNNC -----END PGP SIGNATURE----- --fQ13x7Si4vgcO00xa7P2fm5X7pGOX1UWr--