summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThibault Saunier <tsaunier@igalia.com>2021-11-16 23:36:10 -0300
committerTim-Philipp Müller <tim@centricular.com>2021-11-20 20:44:32 +0000
commit434dd80c3616d99d5547fa76f3d93c0374ee0c11 (patch)
treee7659c5b06aa75fbabaa8e9c1ffe81a9d97d4b97
parent553adc645053f8fe6f02d3888cefe97bcb963918 (diff)
python: Avoid treating float as int
Since python 3.10 implicit conversion to integers using `__int__` as been completely removed (was deprecated behavior in 3.9) so we need to cleanly handle it now. See https://gitlab.gnome.org/GNOME/pitivi/-/issues/2589 Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-python/-/merge_requests/46>
-rw-r--r--gi/overrides/gstmodule.c54
1 files changed, 47 insertions, 7 deletions
diff --git a/gi/overrides/gstmodule.c b/gi/overrides/gstmodule.c
index 167a1c2..2308eb7 100644
--- a/gi/overrides/gstmodule.c
+++ b/gi/overrides/gstmodule.c
@@ -104,18 +104,58 @@ gi_gst_fraction_from_value (const GValue * value)
static int
gi_gst_fraction_to_value (GValue * value, PyObject * object)
{
- PyObject *numerator, *denominator;
+ glong numerator, denominator;
+ PyObject *numerator_obj, *denominator_obj, *is_integer;
- numerator = PyObject_GetAttrString (object, "num");
- if (numerator == NULL)
+ numerator_obj = PyObject_GetAttrString (object, "num");
+ if (numerator_obj == NULL)
goto fail;
- denominator = PyObject_GetAttrString (object, "denom");
- if (denominator == NULL)
+ is_integer = PyObject_CallMethod (numerator_obj, "is_integer", NULL);
+ if (is_integer != Py_True) {
+ PyErr_Format (PyExc_TypeError,
+ "numerator %f is not an integer.", PyFloat_AsDouble (numerator_obj));
+ Py_DECREF (is_integer);
goto fail;
+ }
+ Py_DECREF (is_integer);
+
+ numerator = PyFloat_AsDouble (numerator_obj);
+ if (numerator < -G_MAXINT || numerator > G_MAXINT) {
+ PyErr_Format (PyExc_ValueError,
+ "numerator %" G_GINT64_FORMAT " is out of bound. [-%d - %d]",
+ numerator, G_MAXINT, G_MAXINT);
+ goto fail;
+ }
+
+ denominator_obj = PyObject_GetAttrString (object, "denom");
+ if (denominator_obj == NULL)
+ goto fail;
+
+ is_integer = PyObject_CallMethod (denominator_obj, "is_integer", NULL);
+ if (is_integer != Py_True) {
+ PyErr_Format (PyExc_TypeError,
+ "denominator %f is not an integer.",
+ PyFloat_AsDouble (denominator_obj));
+ Py_DECREF (is_integer);
+ goto fail;
+ }
+ Py_DECREF (is_integer);
+
+ denominator = PyFloat_AsDouble (denominator_obj);
+ if (denominator == 0) {
+ PyErr_SetString (PyExc_ValueError, "denominator is 0.");
+ goto fail;
+ }
+
+ if (denominator < -G_MAXINT || denominator > G_MAXINT) {
+ PyErr_Format (PyExc_ValueError,
+ "denominator %" G_GINT64_FORMAT " is out of bound. [-%d - %d]",
+ denominator, G_MAXINT, G_MAXINT);
+ goto fail;
+ }
- gst_value_set_fraction (value,
- PyLong_AsLong (numerator), PyLong_AsLong (denominator));
+ gst_value_set_fraction (value, numerator, denominator);
return 0;