[Zope-dev] More on the getId issue...

Richard Jones richard@commonground.com.au
Tue, 1 Jul 2003 12:03:25 +1000


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In my last email, I said "Also, is there a reason why Item_w__name__ 
doesn't define getId() when it does define _setId()?" This question 
doesn't really capture the essence of the problem. In a nutshell, the 
following happened...

PageTemplateFile (via Script and SimpleItem) inherits Item. This class 
has an attribute "id" set to '' by default. PageTemplateFiles don't use 
"id" though, they use __name__. The getId implementation that 
PageTemplateFiles use has some mention of __name__ in it, but it'll 
never get used because:

     id = ''
     def getId(self):
         name=getattr(self, 'id', None)
         if callable(name):
             return name()
         if name is not None:
             return name
         if hasattr(self, '__name__'):
             return self.__name__
         raise AttributeError, 'This object has no id'

Note the default value of "None" in the getattr at the start, and then 
the test for None later on. Oh, hang on, except we've got a *class* 
level default value for "id" of ''. Ehem. I suspect that the "if name 
is not None" test *should* read
"if name". And there doesn't need to be the default value for the 
getattr.

I have no idea how much code that assumes that objects will at least 
have an id of '' will break.

My proposed new getId() method:

     def getId(self):
         name = self.id
         if callable(name):
             return name()
         if name:
             return name
         if hasattr(self, '__name__'):
             return self.__name__
         raise AttributeError, 'This object has no id'


     Richard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (Darwin)

iD8DBQE/AOvurGisBEHG6TARAsXQAJsGEri4RIIWpjrSTbjQUZKU37hfLgCfVZTp
jax496YYNjtVosNZHpv8VGc=
=lzBT
-----END PGP SIGNATURE-----