Storing callable references in the ZODB
I recently discovered that the ZODB (possibly newly, or maybe I just haven't noticed this problem before) will not allow a persistent instance to have an attribute assigned to it which is a function or a method. In other words, the following is not allowed: class MyZopeObj(Persistent,...): def __init__(self, callme=None): self.func = callme although you can do this: class MyZopeObj(Persistent,...): callme = ... (e.g. archived message http://gossamer-threads.com/lists/zope/users/168215 ) I have a situation in which I need to find a way to reference a callable object (doesn't strictly have to be a method/function, but I do want to call it), and I'm having a little difficulty getting it to fail for me so that I can test some workaround ideas I have. Here's what I'm doing so far. I'm using Python 2.3.4 and Zope 2.7.0: import Zope Zope.configure('/location/of/zope.conf') app = Zope.app() app.objectIds() # This displays the contents of my root folder in my # local Zope instance. So far so good. # make a sandbox folder to play with app.manage_addFolder('test_perscall') f = app.test_perscall # define a function to evilly reference from ZODB: def spam(a): return a # OK, why doesn't *this* fail? f.spam = spam #(Surely this is storing the function # reference in the ZODB?) # Maybe I need to specifically init this reference in # a persistent object? Let's try that . . . from Persistence import Persistent from OFS import Folder class WCall(Persistent, Folder): """ Test object to hold a method in it's instance. """ def __init__(self, id, title=''): self.id = id self.title = title self.spam = spam ob = WCall('wcall', 'Test callable object instance method') f._setOb('wcall', ob) # WHAT?! *Still* no failure? I figure I must need to get a ZODB commit to happen or something to make my "unpickleable" error come up, but how do I make that happen from the Python interpreter command line? Any comments would be much appreciated. What I need to do is get the error to come up reliably so that I know I understand its limits, and then I need to test my workaround (which is to trace the function's module and name, store the result in a persistent callable object wrapper which can retrieve the function and call it at need). And of course, I have to make it work without error for my workaround code. TIA & Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
participants (1)
-
Terry Hancock