New submission from Arusekk <arek_...@o2.pl>:

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.

----------
components: Interpreter Core
files: typeobject.patch
keywords: patch
messages: 368152
nosy: Arusekk, ammar2, pasenor
priority: normal
severity: normal
status: open
title: Preserve AttributeError exception context in __getattr__
type: behavior
versions: Python 3.9
Added file: https://bugs.python.org/file49122/typeobject.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40519>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to