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


Groups > comp.lang.python > #109170

Re: Handling exceptions with Py2 and Py3

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Handling exceptions with Py2 and Py3
Date Fri, 27 May 2016 23:18:37 +1000
Lines 42
Message-ID <mailman.19.1464355141.2277.python-list@python.org> (permalink)
References <57483C86.6030304@gmail.com> <8537p3abbm.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de LnP46a5Ko0lzyfDBOk3y3QGy6W+O8HAGX9ue4LtT223g==
Cancel-Lock sha1:5xqaEQKovXCtpWVcavoMCpTlxDA=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; 'wrapper': 0.07; 'errno': 0.09; 'nameerror:': 0.09; 'oserror': 0.09; 'raised.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'exception': 0.13; 'oserror.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'attribute': 0.18; 'try:': 0.18; 'precise': 0.22; 'trying': 0.22; 'import': 0.24; 'written': 0.24; 'module': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X -Complaints-To:1': 0.26; 'this.': 0.28; 'robust': 0.29; 'unchanged': 0.29; 'raise': 0.29; 'code': 0.30; 'equal': 0.34; 'except': 0.34; 'handle': 0.34; 'so,': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'provide': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'six': 0.65; 'inherit': 0.66; "they're": 0.66; 'here': 0.66; 'situation': 0.67; '8bit%:24': 0.84; '_o__)': 0.84; 'received:125': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
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 <8537p3abbm.fsf@benfinney.id.au>
X-Mailman-Original-References <57483C86.6030304@gmail.com>
Xref csiph.com comp.lang.python:109170

Show key headers only | View raw


Pavlos Parissis <pavlos.parissis@gmail.com> writes:

> So, we have ConnectionRefusedError in Python3 but not in Python2.
> Six module doesn't provide a wrapper about this.

There are many new exception types in Python 3 that inherit from
OSError. They are designed to be more precise than disambiguuating the
many reasons an OSError might be raised.

In Python 2, the same condition causes an OSError with the ‘errno’
attribute equal to ‘errno.ECONNREFUSED’.

> What is most efficient way to handle this situation in a try-catch
> block?

Others may come up with more efficient ways, depending on what you're
trying to maximise. Here is one robust way, with code that (if I've
written it right) works unchanged on Python 2 and Python 3::

    import errno

    try:
        ConnectionRefusedError
    except NameError:
        ConnectionRefusedError = NotImplemented

    try:
        short_routine()
    except ConnectionRefusedError as exc:
        handle_connection_refused(exc)
    except OSError as exc:
        if exc.errno == errno.ECONNREFUSED:
            handle_connection_refused(exc)
        else:
            raise

-- 
 \           “People are very open-minded about new things, as long as |
  `\         they're exactly like the old ones.” —Charles F. Kettering |
_o__)                                                                  |
Ben Finney

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


Thread

Re: Handling exceptions with Py2 and Py3 Ben Finney <ben+python@benfinney.id.au> - 2016-05-27 23:18 +1000

csiph-web