Suppressing __context__

Martin v. Loewis martin at v.loewis.de
Fri Oct 29 10:31:17 EDT 2010


> It is not easily discoverable, but it is possible to suppress
> __context__ by using a bare re-raise afterwards:

I see. I'd wrap this like this:

def raise_no_context(e):
    try:
        raise e
    except:
        e.__context__=None
        raise

d = {}
try:
    val = d['nosuch']
except KeyError:
    raise_no_context(AttributeError("No attribute 'nosuch'"))

The downside of this is that the innermost frame will be
raise_no_context, but I suppose that's ok because even the
next-inner frame already reveals implementation details that
developers have learned to ignore.

Regards,
Martin



More information about the Python-list mailing list