Message273434
Thanks for being understanding of the decision. Regarding my comment above about __subclasscheck__ potentially letting you implement this without changing the signature of suppress, here's an example of how you might be able to do that.
First, define a metaclass that delegates type checks to the type itself (similar to abc.ABCMeta):
class FilteredExceptionMeta(type):
def __subclasscheck__(cls, other):
return cls.__subclasshook__(other)
def __instancecheck__(cls, other):
return cls.__subclasshook__(type(other))
Then, define a factory function to create custom classes based on that metaclass:
def filtered_exc(exc_type, *, unless=()):
class _FilteredException(metaclass=FilteredExceptionMeta):
@classmethod
def __subclasshook__(cls, other):
return (issubclass(other, exc_type)
and not issubclass(other, unless))
return _FilteredException
>>> from contextlib import suppress
>>> selective_filter = suppress(filtered_exc(OSError, unless=FileNotFoundError))
>>> with selective_filter:
... raise OSError("Suppressed")
...
>>> with selective_filter:
... raise FileNotFoundError("Not suppressed")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
FileNotFoundError: Not suppressed
This works because suppress() calls issubclass() explicitly, unlike the current implementation of except clause processing. |
|
Date |
User |
Action |
Args |
2016-08-23 12:02:28 | ncoghlan | set | recipients:
+ ncoghlan, barry, rhettinger, SilentGhost, yselivanov, mb_ |
2016-08-23 12:02:28 | ncoghlan | set | messageid: <1471953748.92.0.674819623098.issue27814@psf.upfronthosting.co.za> |
2016-08-23 12:02:28 | ncoghlan | link | issue27814 messages |
2016-08-23 12:02:28 | ncoghlan | create | |
|