I'm building a DataBlob class (which inherits from SimpleItem.Item, Persistent and RoleManager), a generic object for storing data is such that you can do the following in a form-processing method: <dtml-with "manage_addProduct['DataBlob']"> <dtml-call "manage_addDataBlob(id='Fred')"> <dtml-call "fred.setFromForm()"> </dtml-with> The other idea is that blob contents can be accessed either as attributes or dictionary keys. So, you could do: a.x = 'my value for x' or a['x'] = 'another value' Unfortunately, that also means you could do: a.setFromForm = my_malicious_function ...for example So, I was going to override __setattr__ with something like the following: (data is a list of names of attributes that may be set...) def __setattr__(self,name,value): if hasattr(self,name) and not name in self.data: raise 'Oh no you don't!' self.data.append(name) SimpleItem.Item.__setattr__(self,name,value) # this is the bit that worries me I think this should work in 2.2, but what about that line, should it be Item.__setattr__ or Persistent.__setattr__ ? Is ther eanything else I've missed? cheers, Chris
Chris Withers wrote: <snip last idea> At Steve Alexander's suggestion, I'm going to try subclassing DataBlob from PersistentMapping. So now I need to add attribute support... How do the following three methods sound: def __getattr__(self,name): try: return PersistentMapping.__getattr__(self,name) except AttributeError,e: try: return self.__getitem__(name) except KeyError: raise AttributeError, e def __setattr__(self,name,value): self.__setitem__(self,name,value) def __delattr__(self,name): try: self.__delitem__(self,name) except KeyError,e: raise AttributeError,e Can anyone see any problems? cheers, Chris
Chris Withers wrote:
def __setattr__(self,name,value): self.__setitem__(self,name,value)
Erk... first problem, why does a Zope __setattr__ take four arguments?! DataBlob.py, line 62, in __setattr__:
Error Type: TypeError Error Value: too many arguments; expected 3, got 4
Do either of __delattr__ or __getattr__ behave differently? cheers, Chris
Wow... I'm writing this whole thread myself ;-) def __setattr__(self,name,value): < self.__setitem__(self,name,value)
self.__setitem__(name,value)
Erk...
first problem, why does a Zope __setattr__ take four arguments?!
It doesn't, I just gave it four... d'oh! However, if I now try to add a DataBlob, I get quite an impressive NT exception thrown :S The application, ./python.exe, generated an application error The error occurred on 8/ 9/2000 @ 11:48:41.189 The exception generated was c00000fd at address 7800118b (lock) DrWatson also said this was a Stack Overflow error. Now what am I doing wrong? ;-) Chris
participants (1)
-
Chris Withers