gst/__init__.py: Added __eq__ method to fractions so we can check if two fractions...
authorEdward Hervey <bilboed@bilboed.com>
Thu, 1 Mar 2007 14:21:52 +0000 (14:21 +0000)
committerEdward Hervey <bilboed@bilboed.com>
Thu, 1 Mar 2007 14:21:52 +0000 (14:21 +0000)
Original commit message from CVS:
* gst/__init__.py:
Added __eq__ method to fractions so we can check if two fractions are
equal.
* gst/pygstvalue.c: (my_gcd), (pygst_value_from_pyobject):
Attempt to simplify gst.Fraction before filling in a GValue.
Fixes #381243
* testsuite/test_caps.py:
* testsuite/test_struct.py:
Minor beauty fixes. framerates are fractions, not floats.

ChangeLog
gst/__init__.py
gst/pygstvalue.c
testsuite/test_caps.py
testsuite/test_struct.py

index 395f57e..e9e96b6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2007-03-01  Edward Hervey  <edward@fluendo.com>
+
+       * gst/__init__.py:
+       Added __eq__ method to fractions so we can check if two fractions are
+       equal.
+       * gst/pygstvalue.c: (my_gcd), (pygst_value_from_pyobject):
+       Attempt to simplify gst.Fraction before filling in a GValue.
+       Fixes #381243
+       * testsuite/test_caps.py:
+       * testsuite/test_struct.py:
+       Minor beauty fixes. framerates are fractions, not floats.
+
 2007-03-01  Jan Schmidt  <thaytan@mad.scientist.com>
 
        reviewed by: Edward Hervey  <edward@fluendo.com>
index 8bba5be..a87da9c 100644 (file)
@@ -80,6 +80,11 @@ class Fraction(Value):
       Value.__init__(self, 'fraction')
       self.num = num
       self.denom = denom
+   def __eq__(self, other):
+       if isinstance(other, Fraction):
+           return self.num * other.denom == other.num * self.denom
+       return False
+       
    def __repr__(self):
       return '<gst.Fraction %d/%d>' % (self.num, self.denom)
 
index 59e2e1d..15734f0 100644 (file)
@@ -32,6 +32,24 @@ static PyObject *gstdoublerange_class = NULL;
 static PyObject *gstfraction_class = NULL;
 static PyObject *gstfractionrange_class = NULL;
 
+/* helper function */
+
+/* Finds the greatest common divisor.
+ * Returns 1 if none other found.
+ * This is Euclid's algorithm. */
+static long
+my_gcd(long num, long denom)
+{
+  while (denom != 0) {
+    long temp = num;
+
+    num = denom;
+    denom = temp % denom;
+  }
+
+  return ABS (num);
+}
+
 /**
  * pygst_value_as_pyobject:
  * @value: the GValue object.
@@ -225,14 +243,24 @@ pygst_value_from_pyobject (GValue *value, PyObject *obj)
     } else if (PyObject_IsInstance (obj, gstfraction_class)) {
       PyObject *pyval;
       long num, denom;
+      long gcd = 0;
       VALUE_TYPE_CHECK (value, GST_TYPE_FRACTION);
       if (!(pyval = PyObject_GetAttrString (obj, "num")))
         return -1;
       num = PyInt_AsLong (pyval);
+      if ((num == -1) && PyErr_Occurred())
+       return -1;
       g_assert (G_MININT <= num && num <= G_MAXINT);
       if (!(pyval = PyObject_GetAttrString (obj, "denom")))
         return -1;
       denom = PyInt_AsLong (pyval);
+      if ((denom == -1) && PyErr_Occurred())
+       return -1;
+      /* we need to reduce the values to be smaller than MAXINT */
+      if ((gcd = my_gcd(num, denom))) {
+       num /= gcd;
+       denom /= gcd;
+      }
       g_assert (G_MININT <= denom && denom <= G_MAXINT);
       gst_value_set_fraction (value, (int)num, (int)denom);
     } else if (PyObject_IsInstance (obj, gstfractionrange_class)) {
index 60360c5..0afac28 100644 (file)
@@ -26,7 +26,7 @@ from common import gst, unittest, TestCase
 class CapsTest(TestCase):
     def setUp(self):
         TestCase.setUp(self)
-        self.caps = gst.caps_from_string('video/x-raw-yuv,width=10,framerate=5.0;video/x-raw-rgb,width=15,framerate=10.0')
+        self.caps = gst.caps_from_string('video/x-raw-yuv,width=10,framerate=5/1;video/x-raw-rgb,width=15,framerate=10/1')
         self.assertEquals(self.caps.__refcount__, 1)
         self.structure = self.caps[0]
         self.any = gst.Caps("ANY")
index 1b45988..d86339d 100644 (file)
@@ -26,7 +26,7 @@ from common import gst, unittest, TestCase
 class StructureTest(TestCase):
     def setUp(self):
         TestCase.setUp(self)
-        self.struct = gst.structure_from_string('video/x-raw-yuv,width=10,foo="bar",pixel-aspect-ratio=1/2,framerate=5.0,boolean=(boolean)true')
+        self.struct = gst.structure_from_string('video/x-raw-yuv,width=10,foo="bar",pixel-aspect-ratio=1/2,framerate=5/1,boolean=(boolean)true')
 
     def testName(self):
         assert self.struct.get_name() == 'video/x-raw-yuv'
@@ -102,9 +102,9 @@ class StructureTest(TestCase):
         assert s['rlist'] == [([(['a', 'b'], ['c', 'd']),'e'], ['f', 'g']), 'h']
 
     def testStructureChange(self):
-        assert self.struct['framerate'] == 5.0
-        self.struct['framerate'] = 10.0
-        assert self.struct['framerate'] == 10.0
+        assert self.struct['framerate'] == gst.Fraction(5, 1)
+        self.struct['framerate'] = gst.Fraction(10, 1)
+        assert self.struct['framerate'] == gst.Fraction(10, 1)
         self.struct['pixel-aspect-ratio'] = gst.Fraction(4, 2)
         assert self.struct['pixel-aspect-ratio'].num == 2
         assert self.struct['pixel-aspect-ratio'].denom == 1