[issue37379] Provide triggering AttributeError exception to __getattr__ for reraising

2019-06-23 Thread nother


New submission from nother :

Not sure where to file this suggestion properly and how.

Currently when mixing attributes and properties it can happen that an 
AttributeError not raised intentionally by @property getter can cause @property 
to vanish from from classinstance. This happens when on the class or any of its 
super classes __getattr__ method is defined and has no clue on how to handle 
attribute with name of property. In this case the latter is reported as not 
existent.

The understood purpose of this behavior is that this is the only means to 
enable @property getters to indicate the availability of the property for a 
specific class instance dependent upon local state of instance object. Another 
purpose is to implement canonical load/creation on deemand schema for 
@properties the same way as for attributes.

The down of this is that in case inside a more complex getter method an 
AttribteError is triggered unintentionally this also triggers call to 
__getattr__ whithout any means for the latter to figure whether the @property 
getter needs assistance or the error should be passed on unhandled.

Therefore i do suggest the following little change to slot_tp_getattr_hook 
method in file typeobject.c (github python/cpython master) on lines 6604 - 6607 
from

`
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
res = call_attribute(self, getattr, name);
}
`

to 

`
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
res = call_attribute(self, getattr, name);
if ( res != NULL) {
 PyErr_Clear();
}
}
`

this little change would allow __getattr__ special method to 

  1) use plain raise (see pythoncode example attached) to reraise exception 
which caused the call to it

  2) allow @property getters convey by adding custom attribute to 
AttributteExcpetion  the reason for an intentionally raised AttributeError over 
to __getattr__ allowing for distinction between AttributeError raised to hide 
@property from class instance versus triggering load/creation on demmand for 
class instance vs AttributeError accidentially triggered by 
getter/setter/deleter code.


Still not possible would be for getattr and hasattr methods to figure whether 
the AttributeError is raised cause class instance does not have the requested 
attirbute or whether accessing the attribute or @property respekctive caused 
some there downstream an AttributeError. In case that would be reliably 
feasible at all.

--
components: Interpreter Core
files: test6.py
messages: 346323
nosy: nother
priority: normal
severity: normal
status: open
title: Provide triggering AttributeError exception to __getattr__ for reraising
type: enhancement
Added file: https://bugs.python.org/file48432/test6.py

___
Python tracker 
<https://bugs.python.org/issue37379>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37379] Provide triggering AttributeError exception to __getattr__ for reraising

2019-06-23 Thread nother


nother  added the comment:

Ah sorry, sure. Attached some examples hope they are not too artificially 
simple to still render real world scenarios.

The one loaded was just the test that a function called from within the except 
block inside another method can use raise keyword to reraise exception caught 
by caller.

--
Added file: https://bugs.python.org/file48433/tes_attr1.py

___
Python tracker 
<https://bugs.python.org/issue37379>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37379] Provide triggering AttributeError exception to __getattr__ for reraising

2019-06-23 Thread nother


nother  added the comment:

The first two exampel should produce the same result not throwing any 
exception. The third example should as it does report that prop is not 
available for instance.

The fourth example should pass on the AttributeError raised for the not 
available attr2 instead of raising a new one stating the prop2 is not available 
form instance.

In all cases the change would allow __getattr__ instead of raising a new 
AttributeError exception reraise the one triggering the  __getattr__ call

--

___
Python tracker 
<https://bugs.python.org/issue37379>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com