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 Arusekk
Recipients Arusekk, ammar2, pasenor
Date 2020-05-05.14:32:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1588689160.61.0.0507579486079.issue40519@roundup.psfhosted.org>
In-reply-to
Content
This is another attempt at issue 39865, but with a different attitude. Please see that issue for some extra motivation for this change.

My suggestion is to change getattr logic, which now is (in this case):

def getattr(obj, attr):
    try:
        return normal_getattr(obj, attr)
    except AttributeError:
        pass
    return obj.__getattr__(attr)

to be more like:

def getattr(obj, attr):
    try:
        return normal_getattr(obj, attr)
    except AttributeError:
        return obj.__getattr__(attr)

A __getattr__ function would then be able to know the exception context
(through sys.exc_info()) and to have the context automatically attached to all the exceptions raised (still respecting raise ... from None).

This particular issue only lies in Objects/typeobject.c, but is probably valid for other uses of PyErr_Clear() in the interpreter.
I checked some using a simple shell pipeline:

$ grep -r -A5 PyErr_ExceptionMatches |grep -C5 PyErr_Clear

And found some interesting examples of what be worth looking into:
Python/sysmodule.c:708
Parser/tokenizer.c:1110
Objects/memoryobject.c:fix_error_int

I prepared two patches for this (please forgive if I violated code style somewhere, I am not saying that they are a final version ready to be merged): a simple one, addressing just this very issue, and a robust one, allowing other places (e.g. from the list above) to reuse it.
History
Date User Action Args
2020-05-05 14:32:40Arusekksetrecipients: + Arusekk, ammar2, pasenor
2020-05-05 14:32:40Arusekksetmessageid: <1588689160.61.0.0507579486079.issue40519@roundup.psfhosted.org>
2020-05-05 14:32:40Arusekklinkissue40519 messages
2020-05-05 14:32:40Arusekkcreate