#8 SimpleHTMLGen.Base irritates inspect module

open
nobody
None
5
2003-04-10
2003-04-10
Stefan Schwarzer
No

The class Base in the module SimpleHTMLGen.py containes
a __getattr__ method that fools the function isclass in
Python's inspect module into believing that instances
of Base (or derived classes) are classes. That is
because Base's __getattr__ allows _any_ attribute name
and inspect.isclass asks an object if it has a
__bases__ attribute. This in turn causes pydoc to fail
on importing SimpleHTMLGen.py .

So Base.__getattr__ should be

def __getattr__(self, attr):
attr = attr.lower()
if attr == "input":
return UnfinishedInput('input')
elif attr == "comment":
return UnfinishedComment()
elif attr == "__bases__":
raise AttributeError("no attribute __bases__")
else:
return UnfinishedTag(attr)

or even

def __getattr__(self, attr):
attr = attr.lower()
if attr == "input":
return UnfinishedInput('input')
elif attr == "comment":
return UnfinishedComment()
elif attr.startswith('_'):
raise AttributeError("no attribute %s" % attr)
else:
return UnfinishedTag(attr)

Discussion