From 34f77d7db3735be7c9bc3f2e9bfc020c96af34df Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Thu, 1 Mar 2007 14:21:52 +0000 Subject: [PATCH] gst/__init__.py: Added __eq__ method to fractions so we can check if two fractions are equal. 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 | 12 ++++++++++++ gst/__init__.py | 5 +++++ gst/pygstvalue.c | 28 ++++++++++++++++++++++++++++ testsuite/test_caps.py | 2 +- testsuite/test_struct.py | 8 ++++---- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 395f57e0a6..e9e96b6a12 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2007-03-01 Edward Hervey + + * 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 reviewed by: Edward Hervey diff --git a/gst/__init__.py b/gst/__init__.py index 8bba5be575..a87da9cef6 100644 --- a/gst/__init__.py +++ b/gst/__init__.py @@ -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 '' % (self.num, self.denom) diff --git a/gst/pygstvalue.c b/gst/pygstvalue.c index 59e2e1dddb..15734f089f 100644 --- a/gst/pygstvalue.c +++ b/gst/pygstvalue.c @@ -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)) { diff --git a/testsuite/test_caps.py b/testsuite/test_caps.py index 60360c52e6..0afac289ed 100644 --- a/testsuite/test_caps.py +++ b/testsuite/test_caps.py @@ -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") diff --git a/testsuite/test_struct.py b/testsuite/test_struct.py index 1b45988746..d86339d824 100644 --- a/testsuite/test_struct.py +++ b/testsuite/test_struct.py @@ -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