A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2013-October/129632.html below:

[Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().

[Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().Victor Stinner victor.stinner at gmail.com
Wed Oct 16 17:10:21 CEST 2013
I would rewrite your examples using try/finally:

    try:
        try:
            os.unlink('missing.txt')
        finally:
            some
            other
            code
    except OSError as exc:
        do_something()

It's differently than yours, because it catchs OSError on "some; other; code".

Sometimes, I write it differently by copying the cleanup code:

    try:
        os.unlink('missing.txt')
        cleanup()
    except OSError as exc:
        cleanup()
        do_something()

(Yeah, it's ugly to duplicate code.)

Victor

2013/10/15 R. David Murray <rdmurray at bitdance.com>:
> You wouldn't use trap/as for that.  Instead you use it to replace this:
>
>     exc = None
>     try:
>         os.unlink('missing.txt')
>     except OSError as exc:
>         pass
>     some
>     other
>     code
>     if exc is not None:
>         do_something()
>
> with:
>
>     with trap(OSError) as cm:
>         os.unlink('missing.txt')
>     some
>     other
>     code
>     if cm.exc is not None:
>         do_something()
>
> which saves you *three* lines, not just two :)
>
> Of course in real life, in order for 'some other code' really be the
> best way to organize this snippet, you're going to be doing something
> more complicated than catching OSError from an unlink call.
>
> If this is accepted, I do *not* expect to see this in real code very
> often at all.  The main motivation for it is to make the semantics
> of the context manager clearer, and as a bonus make it work like
> assertRaises, which is our existing stdlib context manager that traps
> exceptions.
>
> I'd be happy with 'trap' or 'catch' without 'as' support, but the
> names make more sense if the exception is actually available after
> it is trapped/caught.
>
> --David
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/victor.stinner%40gmail.com
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4