Extending a class' __bases__ at runtime
Hia, trying to modify a class at runtime in Zope is proving hard to do. The first interactive session is a 'Zope debugging session': Python 1.5.2 (#0, Apr 10 2001, 10:03:44) [GCC 2.95.3 20010219 (prerelease)] Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam Welcome to <secure_monitor_channel connected 127.0.0.1:46914 at 90a7df8>
import Zope a=Zope.app() s=a['Secret'] class a1:pass class a2:pass class a3:pass s.__class__.__bases__ = s.__class__.__bases__ + (a1,a2,a3) s.__class__.__bases__ (<extension class App.PersistentExtra.Persistent at 4022be80>, <extension class Products.BTreeFolder.BTreeFolder.BTreeFolder at 84fecf8>, <extension class Acquisition.Acquirer at 4024ac80>, <extension class AccessControl.Role.RoleManager at 82819f0>, <class Products.WarpFramework.product_base_item.product_base_item at 845e058>, <extension class Products.WarpFramework.base_item.base_item at 848f1b0>, <class Products.Secret.imports.imports at 8532fa0>, <class Products.Secret.initializations.initializations at 858ee28>, <class Products.Secret.trash_test.trash_test at 857e2d0>)
where, as you can see, trying to change the base classes doesn't work, while in an ordinary, 'plain python' interactive session, it works: Python 1.5.2 (#0, Apr 10 2001, 10:03:44) [GCC 2.95.3 20010219 (prerelease)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
class a: ... pass ... b=a() class a1: pass ... class a2: pass ... class a3: pass ... b.__class__.__bases__ () b.__class__.__bases__ = b.__class__.__bases__ + (a1,a2,a3) b.__class__.__bases__ (<class __main__.a1 at 8070db0>, <class __main__.a2 at 806ff80>, <class __main__.a3 at 80709f8>) a.__bases__ (<class __main__.a1 at 8070db0>, <class __main__.a2 at 806ff80>, <class __main__.a3 at 80709f8>)
Any clues on what needs to be done to be able to modify a class' base classes at runtime? Thanks & cheers, Morten
On Sun, 19 Aug 2001, Chris Withers wrote:
Any clues on what needs to be done to be able to modify a class' base classes at runtime?
I do feel the need to ask why on earth you want to do it?!
If you take a look here: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/~checkout~/warp-framework/war... It's part of a scheme to easily register subclasses (not subclasses in the pythonish sense, rather the Zope-and-meta-type sense) where modification of the containing class is needed (some extra methods for example). Cheers, Morten
"Morten W. Petersen" wrote:
It's part of a scheme to easily register subclasses (not subclasses in the pythonish sense, rather the Zope-and-meta-type sense) where modification of the containing class is needed (some extra methods for example).
I'd use the CMF instead, Types Tool is great for that ;-) Chris
On Sun, 19 Aug 2001, Chris Withers wrote:
It's part of a scheme to easily register subclasses (not subclasses in the pythonish sense, rather the Zope-and-meta-type sense) where modification of the containing class is needed (some extra methods for example).
I'd use the CMF instead, Types Tool is great for that ;-)
There's another reason to want to know the answer to this: the ability to hotpatch the base classes of an existing zope class instead of having to modify the source code for that class. This came up for the person who was trying to create a version of the CMF that used ZPatterns...and I don't recall hearing a solution. Plus, it sure is a "surprise" that something that works in normal python code doesn't work in Zope! I presume this is an ExtensionClass issue, but I don't know enough to suss it out. --RDM
On Tue, 28 Aug 2001, R. David Murray wrote:
There's another reason to want to know the answer to this: the ability to hotpatch the base classes of an existing zope class instead of having to modify the source code for that class. This came up for the person who was trying to create a version of the CMF that used ZPatterns...and I don't recall hearing a solution.
Plus, it sure is a "surprise" that something that works in normal python code doesn't work in Zope! I presume this is an ExtensionClass issue, but I don't know enough to suss it out.
I'd offer $50 of merchandise at <put your favourite online store here> to anyone who fixes this issue. Cheers, Morten
participants (3)
-
Chris Withers -
Morten W. Petersen -
R. David Murray