[issue30792] Add contextlib.convert_exception manager

2017-06-28 Thread Nick Coghlan

Nick Coghlan added the comment:

Also clarifying a point that came up later in the thread [1]: one of the 
motivating examples that came up in the thread was the idea of using this to 
catch AttributeError exceptions in property and __getattr__ implementations and 
convert them to RuntimeError.

I don't actually think that's a good use case: using a static analysis tool 
like 'pylint -E' or mypy, or the builtin error checking in an advanced IDE like 
PyCharm is a much better approach to detecting that kind of problem.

Instead, the valid use cases I see for runtime exception conversion are in:

- framework development, where you're invoking arbitrary code that you didn't 
write and want to ensure it can't falsely trigger an exception based event 
reporting protocol (this is the PEP 479 use case, and we may end up doing this 
for the exec_module() hook in the import system as well)

- adapting between two different exception based event reporting protocols 
(e.g. KeyError <-> AttributeError, StopIteration -> AsyncStopIteration)

[1] https://mail.python.org/pipermail/python-ideas/2017-June/046234.html

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30792] Add contextlib.convert_exception manager

2017-06-28 Thread Steven D'Aprano

New submission from Steven D'Aprano:

As discussed on Python-Ideas, there's some interest in a context manager that 
can convert an exception from one type to another (similarly to the way PEP 479 
has StopIteration converted to RuntimeError.

See the thread starting here:
https://mail.python.org/pipermail/python-ideas/2017-June/046109.html

and particularly Nick's comment here:
https://mail.python.org/pipermail/python-ideas/2017-June/046218.html

I've written a recipe on ActiveState:

https://code.activestate.com/recipes/580808-guard-against-an-exception-in-the-wrong-place/

which acts as both a context manager and function decorator. That can be used 
as proof-of-concept or as a basis for a PR.

--
components: Library (Lib)
messages: 297193
nosy: ncoghlan, steven.daprano
priority: normal
severity: normal
status: open
title: Add contextlib.convert_exception manager
type: enhancement
versions: Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com