@@ -65,6 +65,12 @@ def generate(self):
6565
6666 self .outputSetattr ()
6767
68+ self .outputCompare ()
69+
70+ self .outputRepr ()
71+
72+ self .outputHash ()
73+
6874 self .outputTypeObject ()
6975
7076 OutHeader2 ("End object type " + self .name )
@@ -153,6 +159,18 @@ def outputSetattr(self):
153159 Output ()
154160 Output ("#define %s_setattr NULL" , self .prefix )
155161
162+ def outputCompare (self ):
163+ Output ()
164+ Output ("#define %s_compare NULL" , self .prefix )
165+
166+ def outputRepr (self ):
167+ Output ()
168+ Output ("#define %s_repr NULL" , self .prefix )
169+
170+ def outputHash (self ):
171+ Output ()
172+ Output ("#define %s_hash NULL" , self .prefix )
173+
156174 def outputTypeObject (self ):
157175 sf = self .static and "staticforward "
158176 Output ()
@@ -168,6 +186,12 @@ def outputTypeObject(self):
168186 Output ("0, /*tp_print*/" )
169187 Output ("(getattrfunc) %s_getattr, /*tp_getattr*/" , self .prefix )
170188 Output ("(setattrfunc) %s_setattr, /*tp_setattr*/" , self .prefix )
189+ Output ("(cmpfunc) %s_compare, /*tp_compare*/" , self .prefix )
190+ Output ("(reprfunc) %s_repr, /*tp_repr*/" , self .prefix )
191+ Output ("(PyNumberMethods *)0, /* tp_as_number */" )
192+ Output ("(PySequenceMethods *)0, /* tp_as_sequence */" )
193+ Output ("(PyMappingMethods *)0, /* tp_as_mapping */" )
194+ Output ("(hashfunc) %s_hash, /*tp_hash*/" , self .prefix )
171195 DedentLevel ()
172196 Output ("};" )
173197
@@ -192,3 +216,46 @@ class GlobalObjectDefinition(ObjectDefinition):
192216 def __init__ (self , name , prefix = None , itselftype = None ):
193217 ObjectDefinition .__init__ (self , name , prefix or name , itselftype or name )
194218 self .static = ""
219+
220+ class ObjectIdentityMixin :
221+ """A mixin class for objects that makes the identity of ob_itself
222+ govern comparisons and dictionary lookups. Useful if the C object can
223+ be returned by library calls and it is difficult (or impossible) to find
224+ the corresponding Python objects. With this you can create Python object
225+ wrappers on the fly"""
226+
227+ def outputCompare (self ):
228+ Output ()
229+ Output ("static int %s_compare(self, other)" , self .prefix )
230+ IndentLevel ()
231+ Output ("%s *self, *other;" , self .objecttype )
232+ DedentLevel ()
233+ OutLbrace ()
234+ Output ("unsigned long v, w;" )
235+ Output ()
236+ Output ("if (!%s_Check((PyObject *)other))" , self .prefix )
237+ OutLbrace ()
238+ Output ("v=(unsigned long)self;" )
239+ Output ("w=(unsigned long)other;" )
240+ OutRbrace ()
241+ Output ("else" )
242+ OutLbrace ()
243+ Output ("v=(unsigned long)self->ob_itself;" )
244+ Output ("w=(unsigned long)other->ob_itself;" )
245+ OutRbrace ()
246+ Output ("if( v < w ) return -1;" )
247+ Output ("if( v > w ) return 1;" )
248+ Output ("return 0;" )
249+ OutRbrace ()
250+
251+ def outputHash (self ):
252+ Output ()
253+ Output ("static long %s_hash(self)" , self .prefix )
254+ IndentLevel ()
255+ Output ("%s *self;" , self .objecttype )
256+ DedentLevel ()
257+ OutLbrace ()
258+ Output ("return (long)self->ob_itself;" )
259+ OutRbrace ()
260+
261+
0 commit comments