[Patches] [ python-Patches-532638 ] Better AttributeError formatting

noreply@sourceforge.net noreply@sourceforge.net
Wed, 20 Mar 2002 10:42:49 -0800


Patches item #532638, was opened at 2002-03-20 12:42
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=532638&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Skip Montanaro (montanaro)
Assigned to: Nobody/Anonymous (nobody)
Summary: Better AttributeError formatting

Initial Comment:
A user in c.l.py was confused when

  import m
  m.a

reported

  AttributeError: 'module' object has no attribute 
'a'

The attached patch displays the object's name in
the error message if it has a __name__ attribute.
This is a bit tricky because of the recursive 
nature of looking up an attribute during a getattr 
operation. My solution was to pull the error 
formatting code into a separate static routine
(the same basic thing happens in three places) and
define a static variable there that breaks any 
recursion.

While this might not be thread-safe, I
think it's okay in this situation.  The worst that 
should happen is you get either an extra round of
recursion while looking up a non-existent __name__
ttribute or fail to even check for __name__ and
use the default formatting when the object
actually has a __name__ attribute.  This can only
happen if you have two threads who both get 
attribute errors at the same time, and then only
if the process of looking things up takes you back
into Python code.

Perhaps a similar technique can be provided for 
other error formatting operations in object.c.

Example for objects with and without __name__
attributes:

>>> "".foo
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: str object has no attribute 'foo'
>>> import string
>>> string.foo
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: module object 'string' has no 
attribute 'foo'

Skip


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=532638&group_id=5470