Exception __name__ missing?
Is this on purpose? Python 3.2rc1 (py3k:88040, Jan 15 2011, 18:11:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information.
Exception.__name__ 'Exception' e = Exception('has no name') e.__name__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Exception' object has no attribute '__name__'
Ron Adam
Am 17.01.2011 21:22, schrieb Ron Adam:
Is this on purpose?
Python 3.2rc1 (py3k:88040, Jan 15 2011, 18:11:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information.
Exception.__name__ 'Exception' e = Exception('has no name') e.__name__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Exception' object has no attribute '__name__'
It's not on purpose in the sense that it's not something special to exceptions. The class __name__ attribute is not accessible from instances of any class. Georg
On 01/17/2011 02:27 PM, Georg Brandl wrote:
Am 17.01.2011 21:22, schrieb Ron Adam:
Is this on purpose?
Python 3.2rc1 (py3k:88040, Jan 15 2011, 18:11:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information.
Exception.__name__ 'Exception' e = Exception('has no name') e.__name__ Traceback (most recent call last): File "<stdin>", line 1, in<module> AttributeError: 'Exception' object has no attribute '__name__'
It's not on purpose in the sense that it's not something special to exceptions. The class __name__ attribute is not accessible from instances of any class.
Yes, I realised this on the way to an appointment. Oh well. ;-) What I needed was e.__class__.__name__ instead of e.__name__. I should have thought about this a little more before posting. The particular reason I wanted it was to format a nice message for displaying in pydoc browser mode. The server errors, like a missing .css file, and any other server related errors, go the server console, while the content errors get displayed in a web page. ie... object not found, or some other content related reason for not giving what was asked for. Doing repr(e) was giving me too much. UnicodeDecodeError('utf8', b'\x7fELF\x02\x01\x01\x00\x00\x00\x .... With pages of bytes, and I'd rather not truncate it, although that would be ok. str(e) was more useful, but didn't include the exception name. 'utf8' codec can't decode byte 0xe0 in position 24: invalid continuation byte So doing e.__name__ was the obvious next thing... for some reason I expected the __name__ attribute in exception instances to be inherited from the class. Beats me why. <shrug> Thanks, Ron
Am 18.01.2011 03:41, schrieb Ron Adam:
On 01/17/2011 02:27 PM, Georg Brandl wrote:
Am 17.01.2011 21:22, schrieb Ron Adam:
Is this on purpose?
Python 3.2rc1 (py3k:88040, Jan 15 2011, 18:11:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information.
Exception.__name__ 'Exception' e = Exception('has no name') e.__name__ Traceback (most recent call last): File "<stdin>", line 1, in<module> AttributeError: 'Exception' object has no attribute '__name__'
It's not on purpose in the sense that it's not something special to exceptions. The class __name__ attribute is not accessible from instances of any class.
Yes, I realised this on the way to an appointment. Oh well. ;-)
What I needed was e.__class__.__name__ instead of e.__name__.
I should have thought about this a little more before posting.
The particular reason I wanted it was to format a nice message for displaying in pydoc browser mode. The server errors, like a missing .css file, and any other server related errors, go the server console, while the content errors get displayed in a web page. ie... object not found, or some other content related reason for not giving what was asked for.
Doing repr(e) was giving me too much.
UnicodeDecodeError('utf8', b'\x7fELF\x02\x01\x01\x00\x00\x00\x ....
With pages of bytes, and I'd rather not truncate it, although that would be ok.
str(e) was more useful, but didn't include the exception name.
'utf8' codec can't decode byte 0xe0 in position 24: invalid continuation byte
For these cases, you can use traceback.format_exception_only(). Georg
On 1/17/2011 3:22 PM, Ron Adam wrote:
Is this on purpose?
This reminds me of something I ran into a few years ago wrt. the attribute on exceptions. Namely, that instances of built-in exceptions do not have a __module__ attribute, but instance of user exceptions do -- a change which appeared in Python 2.5: http://mail.python.org/pipermail/python-list/2007-November/1088229.html I had a use case, using ZSI to provide a SOAP interface, where being able to get the __module__ and __name__ was needed (to serialize into a SOAP "fault" message). I worked around the issue by referencing the __class__ (as the other replier mentioned). But, I didn't receive any responses then, so I think not a lot of attention was put into these type of attributes on exceptions. -- Scott Dial scott@scottdial.com scodial@cs.indiana.edu
On Tue, Jan 18, 2011 at 7:09 AM, Scott Dial <scott+python-dev@scottdial.com> wrote:
I worked around the issue by referencing the __class__ (as the other replier mentioned). But, I didn't receive any responses then, so I think not a lot of attention was put into these type of attributes on exceptions.
That's not a workaround, it is the way you're meant to access __module__ and __name__ on new-style classes (which was the transition that happened for Exception in 2.5). The fact that user-defined classes get a __module__ attribute on instances while builtin and extension types don't isn't unique to exceptions though:
class C: pass ... C.__module__ '__main__' C().__module__ '__main__'
str.__module__ 'builtins' str().__module__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute '__module__'
import datetime datetime.datetime.__module__ 'datetime' datetime.datetime.now().__module__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'datetime.datetime' object has no attribute '__module__'
The addition of __module__ to user defined class instances strikes me as a bug. You can see in the language reference [1] that __dict__ and __class__ are the only expected data attributes for class instances. [1] http://docs.python.org/dev/reference/datamodel.html (search for the entry on "class instances", then scroll back up and contrast with the sections on class objects, functions and methods) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (5)
-
Benjamin Peterson
-
Georg Brandl
-
Nick Coghlan
-
Ron Adam
-
Scott Dial