This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients SilentGhost, barry, mb_, ncoghlan, rhettinger, yselivanov
Date 2016-08-23.12:02:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1471953748.92.0.674819623098.issue27814@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-08-23 12:02:28ncoghlansetrecipients: + ncoghlan, barry, rhettinger, SilentGhost, yselivanov, mb_
2016-08-23 12:02:28ncoghlansetmessageid: <1471953748.92.0.674819623098.issue27814@psf.upfronthosting.co.za>
2016-08-23 12:02:28ncoghlanlinkissue27814 messages
2016-08-23 12:02:28ncoghlancreate