[Python-Dev] Attribute error: providing type name (original) (raw)
Nick Coghlan ncoghlan at gmail.com
Tue Dec 2 11:37:56 CET 2008
- Previous message: [Python-Dev] Attribute error: providing type name
- Next message: [Python-Dev] Attribute error: providing type name
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Martin v. Löwis wrote:
Alex Martelli wrote: I think the standard exception hierarchy should grow additional standard fields. E.g. AttributeError should have attributes 'type','name', or perhaps even 'object','name'. TypeError should have attributes 'expected', 'actual' (or, again, 'expected', 'object').
And so on - that might produce quite a large PEP.
I don't think there's any reason to do it in one big bang. And approached individually, each such alternate constructor is a small RFE consisting of:
- Specific C API for creating exceptions of that type with a standard message and attributes
- Python level class method
- New attributes on the affected object
Point 3 would be optional really, since most of the gain comes from the better error messages. If extra attributes were included in such an RFE, the potential lifecycle implications of including references to actual objects rather than merely their types makes the better choice fairly obvious to me (i.e. just include the type information, since it generally tells you everything you need to know for TypeErrors and AttributeErrors).
As 3.0 missed the chance to fix this, compatibility is also an issue. It might be possible to overload exception constructors on the number of parameters, or using keyword parameters for the new way of filling the exception.
Or go the traditional "multiple constructor" route and provide class methods for the alternative mechanisms.
And no, I don't volunteer to write this PEP :-)
Assuming I understand what you mean by "nested exceptions" correctly, they should be covered by the context and cause attributes in Py3k:
Exception context:
try: ... raise IOError ... except: ... raise AttributeError ... Traceback (most recent call last): File "", line 2, in IOError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "", line 4, in AttributeError
Exception cause:
raise AttributeError from KeyError KeyError
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "", line 1, in AttributeError
Putting it all together:
try: ... raise IOError ... except: ... try: ... raise KeyError ... except Exception as ex: ... raise AttributeError from ex ... Traceback (most recent call last): File "", line 2, in IOError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "", line 5, in KeyError
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "", line 7, in AttributeError
Cheers, Nick.
-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
- Previous message: [Python-Dev] Attribute error: providing type name
- Next message: [Python-Dev] Attribute error: providing type name
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]