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.
This commit is contained in:
Edward Hervey 2007-03-01 14:21:52 +00:00
parent 78dc74984d
commit 34f77d7db3
5 changed files with 50 additions and 5 deletions

View file

@ -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>

View 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)

View 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)) {

View 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")

View 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