[PyKDE] __truediv__ support in SIP

Giovanni Bajo rasky at develer.com
Tue Jan 9 14:36:39 GMT 2007


Hello,

SIP does not currently support __truediv__. The attached patch lets the 
user manually define it through %MethodCode in a SIP file.

I think SIP should also automatically generate __truediv__ for 
operator/, with exactly the same code that it uses for __div__. In fact, 
the difference between __truediv__ and __div__ exists only for builtin 
int/float types, for backward compatibility: any other user-defined type 
should define both slots to the same code.

Phile, how do I make SIP generate __truediv__ code exactly like it does 
for __div__? Or if you have time to handle this yourself, be my guest, 
I'll just sit, relax and grab the snapshot :)

Thanks!
-- 
Giovanni Bajo
-------------- next part --------------
Index: siplib/sip.h
===================================================================
--- siplib/sip.h	(revision 9342)
+++ siplib/sip.h	(working copy)
@@ -295,7 +295,8 @@
 	repr_slot,			/* __repr__ */
 	hash_slot,			/* __hash__ */
 	pos_slot,			/* __pos__ */
-	abs_slot			/* __abs__ */
+	abs_slot,			/* __abs__ */
+        truediv_slot,			/* __truediv__ */
 } sipPySlotType;
 
 
Index: siplib/siplib.c
===================================================================
--- siplib/siplib.c	(revision 9342)
+++ siplib/siplib.c	(working copy)
@@ -6484,6 +6484,12 @@
 					nb->nb_divide = (binaryfunc)f;
 			break;
 
+		case truediv_slot:
+			if (nb != NULL)
+				if (force || nb->nb_true_divide == NULL)
+					nb->nb_true_divide = (binaryfunc)f;
+			break;
+
 		case mod_slot:
 			if (nb != NULL)
 				if (force || nb->nb_remainder == NULL)
Index: sipgen/sip.h
===================================================================
--- sipgen/sip.h	(revision 9342)
+++ sipgen/sip.h	(working copy)
@@ -358,7 +358,8 @@
 	abs_slot,
 	repr_slot,
 	hash_slot,
-	no_slot
+	no_slot,
+        truediv_slot
 } slotType;
 
 
Index: sipgen/gencode.c
===================================================================
--- sipgen/gencode.c	(revision 9342)
+++ sipgen/gencode.c	(working copy)
@@ -3863,7 +3863,7 @@
 	slotType st = md -> slot;
 
 	return (st == add_slot || st == sub_slot || st == mul_slot ||
-		st == div_slot || st == mod_slot ||
+		st == div_slot || st == truediv_slot || st == mod_slot ||
 		st == and_slot || st == or_slot || st == xor_slot ||
 		st == lshift_slot || st == rshift_slot);
 }
@@ -7369,6 +7369,10 @@
 		sn = "div_slot";
 		break;
 
+	case truediv_slot:
+		sn = "truediv_slot";
+		break;
+
 	case mod_slot:
 		sn = "mod_slot";
 		break;
@@ -8777,6 +8781,10 @@
 			generateNumberSlotCall(od,"/",fp);
 			break;
 
+		case truediv_slot:
+			generateNumberSlotCall(od,"/",fp);
+			break;
+
 		case mod_slot:
 			generateNumberSlotCall(od,"%",fp);
 			break;
@@ -9965,6 +9973,10 @@
 		pt2 = "/";
 		break;
 
+	case truediv_slot:
+		pt2 = "/";
+		break;
+
 	case mod_slot:
 		pt2 = "%";
 		break;
Index: sipgen/parser.c
===================================================================
--- sipgen/parser.c	(revision 9342)
+++ sipgen/parser.c	(working copy)
@@ -6698,6 +6698,7 @@
 		{"__sub__", sub_slot, FALSE, 1},
 		{"__mul__", mul_slot, FALSE, 1},
 		{"__div__", div_slot, FALSE, 1},
+                {"__truediv__", truediv_slot, FALSE, 1},
 		{"__mod__", mod_slot, FALSE, 1},
 		{"__and__", and_slot, FALSE, 1},
 		{"__or__", or_slot, FALSE, 1},
Index: sipdistutils.py
===================================================================
--- sipdistutils.py	(revision 9342)
+++ sipdistutils.py	(working copy)
@@ -5,7 +5,7 @@
 # Based on Pyrex.Distutils, written by Graham Fawcett and Darrel Gallion.
 
 import distutils.command.build_ext
-from distutils.dep_util import newer
+from distutils.dep_util import newer, newer_group
 import os
 import sys
 
@@ -48,10 +48,18 @@
         # Add the SIP include directory to the include path
         if extension is not None:
             extension.include_dirs.append(self._sip_inc_dir())
+            depends = extension.depends
         else:
             # pre-2.4 compatibility
             self.include_dirs.append(self._sip_inc_dir())
+            depends = []  # ?
 
+        # Filter dependencies list: we are interested only in .sip files,
+        # since the main .sip files can only depend on additional .sip
+        # files. For instance, if a .h changes, there is no need to
+        # run sip again.
+        depends = [f for f in depends if os.path.splitext(f)[1] == ".sip"]
+
         # Create the temporary directory if it does not exist already
         if not os.path.isdir(self.build_temp):
             os.makedirs(self.build_temp)
@@ -67,8 +75,8 @@
         for sip in sip_sources:
             # Use the sbf file as dependency check
             sipbasename = os.path.basename(sip)
-            sbf = os.path.join(self.build_temp, replace_suffix(sipbasename, "sbf"))
-            if newer(sip, sbf) or self.force:
+            sbf = os.path.join(self.build_temp, replace_suffix(sipbasename, ".sbf"))
+            if newer_group([sip]+depends, sbf) or self.force:
                 self._sip_compile(sip_bin, sip, sbf)
             out = self._get_sip_output_list(sbf)
             generated_sources.extend(out)


More information about the PyQt mailing list