[PEAK] Dispatch on isinstance() for Zope Extension classes
Alain Poirier
alain.poirier at net-ng.com
Wed Mar 29 11:07:46 EST 2006
Le Mardi 28 Mars 2006 17:26, Phillip J. Eby a écrit :
> At 02:46 PM 3/28/2006 +0200, Alain Poirier wrote:
> >Hi Phillip,
> >
> >Using RuleDispatch with Zope 2.x, I've got the problem that the
> >'isinstance()' rule doesn't accept a Zope extension class.
> >
> >Do you think it could be possible to fix this problem ?
>
> You need to define an adapter from ExtensionClass.ExtensionClass (I think
> that's the right type) to dispatch.interfaces.ICriterion. The adapter
> you should use is dispatch.strategy.ClassCriterion. Something like:
>
> protocols.declareAdapter(ClassCriterion, [ICriterion],
> [ExtensionClass])
>
> Let me know if that doesn't fix it for you.
After declaring this adapter, I had :
"""
...
Module dispatch.functions, line 5, in processEvent
Module dispatch._speedups, line 329, in
_speedups.BaseDispatcher.__getitem__
Module dispatch._speedups, line 240, in _speedups.dispatch_by_mro
TypeError: ('Not a class or type:', <extension class
pjsproduct_zb.pjsproduct.CPJSProduct at b58e4ae8>)
"""
So I tried to patch the speedups.pyx module as below. Now 'instance(o, X)'
seems to work if o is a direct instance of X but not if o is a instance of
a sub-class of X. Need more testing.
Here is where I'am for the moment. Any ideas ?
"""
--- RuleDispatch-0.5a0/src/dispatch/_speedups.pyx 2005-11-03
16:57:16.000000000 +0100
+++ RuleDispatch-0.5a0.new/src/dispatch/_speedups.pyx 2006-03-29
17:02:08.000000000 +0200
@@ -6,6 +6,7 @@
cdef object InstanceType, NoApplicableMethods, DispatchError, _NF, __nclass
from dispatch.interfaces import NoApplicableMethods, DispatchError
from types import InstanceType
+from ExtensionClass import ExtensionClass
__nclass = "__class__"
_NF = [0,None, NoApplicableMethods, (None,None)]
@@ -98,11 +99,10 @@
object __Pyx_GetExcValue()
+cdef extern from "ExtensionClass.h":
-
-
-
-
+ ctypedef struct PyExtensionClass:
+ PyTupleObject *bases
@@ -209,10 +209,10 @@
cdef int bc
cdef void *tmp
cdef PyTupleObject *bases
-
+
if not PyDict_Check(table):
raise TypeError("Not a dict subclass", table)
-
+
tmp = PyObject_GetAttr(ob, __nclass)
if tmp:
klass = <object> tmp
@@ -236,6 +236,8 @@
bases = (<PyClassObject *>klass).cl_bases
elif PyType_Check(klass):
bases = (<PyTypeObject *>klass).tp_bases
+ elif (<PyObject *>klass).ob_type == <PyTypeObject *>ExtensionClass:
+ bases = (<PyExtensionClass *>klass).bases
else:
raise TypeError("Not a class or type:", klass)
"""
More information about the PEAK
mailing list