[Python-au] help debugging cacheability.py

Andrew McNamara andrewm at object-craft.com.au
Wed Jul 20 14:32:31 CEST 2005

>Hello, I'm trying to get the cacheability script from 
>http://www.mnot.net/cacheability/download.html working with python 2.3.4
>It works fine with python 1.5.2
>Here's two test URLs to demonstrate the problem:
>Traceback (most recent call last):
>   File "./cacheability.py", line 994, in ?
>     clui_main()
>   File "./cacheability.py", line 961, in clui_main
>     print f
>   File "/var/www/cgi-bin/cacheability/spider.py", line 355, in __repr__
>     p("  Cache-Control: %s" % (self._hdrs.get('cache-control', self.no_data)))
>   File "/var/www/cgi-bin/cacheability/httpheadertypes.py", line 280, in 
>     return self.__dict__[attr]
>KeyError: '__str__'

The __getattr__ method in question reads:

	def __getattr__(self, attr):
		if attr[0] == '_':
			return self.__dict__[attr]
		attr = lower(replace(attr, '_', '-'))
		return self.data[attr]

The problem is, __getattr__ is only called if the attribute isn't already
in self.__dict__ (the class's attribute dictionary), so there's little
point trying to return it by accessing the dictionary directly. Also,
the method should raise AttributeError (rather than KeyError) for missing
attributes, as this is what hasattr() checks for. The code is probably
better written:

	def __getattr__(self, attr):
		if attr[0] == '_':
			raise AttributeError
		attr = lower(replace(attr, '_', '-'))
		return self.data[attr]

I haven't verified it, but I imagine Python 1.5's hasattr() caught any
exception and treated it as a missing attribute, rather than selectively
catching AttributeError (I seem to remember some discussion of this on
python-dev in the mists of time).

Andrew McNamara, Senior Developer, Object Craft

More information about the python-au mailing list