113.0-1
[arch-packages.git] / gst-python / trunk / 0001-python-Avoid-treating-float-as-int.patch
blob6d788f21e823cbc6d2a2f5281dee28f94f3d7825
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Thibault Saunier <tsaunier@igalia.com>
3 Date: Tue, 16 Nov 2021 23:36:10 -0300
4 Subject: [PATCH] python: Avoid treating float as int
6 Since python 3.10 implicit conversion to integers using `__int__` as
7 been completely removed (was deprecated behavior in 3.9) so we need
8 to cleanly handle it now.
10 See https://gitlab.gnome.org/GNOME/pitivi/-/issues/2589
12 Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1358>
13 ---
14 .../gst-python/gi/overrides/gstmodule.c | 54 ++++++++++++++++---
15 1 file changed, 47 insertions(+), 7 deletions(-)
17 diff --git a/subprojects/gst-python/gi/overrides/gstmodule.c b/subprojects/gst-python/gi/overrides/gstmodule.c
18 index 167a1c27539a..2308eb7dcde6 100644
19 --- a/subprojects/gst-python/gi/overrides/gstmodule.c
20 +++ b/subprojects/gst-python/gi/overrides/gstmodule.c
21 @@ -104,18 +104,58 @@ gi_gst_fraction_from_value (const GValue * value)
22 static int
23 gi_gst_fraction_to_value (GValue * value, PyObject * object)
25 - PyObject *numerator, *denominator;
26 + glong numerator, denominator;
27 + PyObject *numerator_obj, *denominator_obj, *is_integer;
29 - numerator = PyObject_GetAttrString (object, "num");
30 - if (numerator == NULL)
31 + numerator_obj = PyObject_GetAttrString (object, "num");
32 + if (numerator_obj == NULL)
33 goto fail;
35 - denominator = PyObject_GetAttrString (object, "denom");
36 - if (denominator == NULL)
37 + is_integer = PyObject_CallMethod (numerator_obj, "is_integer", NULL);
38 + if (is_integer != Py_True) {
39 + PyErr_Format (PyExc_TypeError,
40 + "numerator %f is not an integer.", PyFloat_AsDouble (numerator_obj));
41 + Py_DECREF (is_integer);
42 + goto fail;
43 + }
44 + Py_DECREF (is_integer);
46 + numerator = PyFloat_AsDouble (numerator_obj);
47 + if (numerator < -G_MAXINT || numerator > G_MAXINT) {
48 + PyErr_Format (PyExc_ValueError,
49 + "numerator %" G_GINT64_FORMAT " is out of bound. [-%d - %d]",
50 + numerator, G_MAXINT, G_MAXINT);
51 + goto fail;
52 + }
54 + denominator_obj = PyObject_GetAttrString (object, "denom");
55 + if (denominator_obj == NULL)
56 goto fail;
58 - gst_value_set_fraction (value,
59 - PyLong_AsLong (numerator), PyLong_AsLong (denominator));
60 + is_integer = PyObject_CallMethod (denominator_obj, "is_integer", NULL);
61 + if (is_integer != Py_True) {
62 + PyErr_Format (PyExc_TypeError,
63 + "denominator %f is not an integer.",
64 + PyFloat_AsDouble (denominator_obj));
65 + Py_DECREF (is_integer);
66 + goto fail;
67 + }
68 + Py_DECREF (is_integer);
70 + denominator = PyFloat_AsDouble (denominator_obj);
71 + if (denominator == 0) {
72 + PyErr_SetString (PyExc_ValueError, "denominator is 0.");
73 + goto fail;
74 + }
76 + if (denominator < -G_MAXINT || denominator > G_MAXINT) {
77 + PyErr_Format (PyExc_ValueError,
78 + "denominator %" G_GINT64_FORMAT " is out of bound. [-%d - %d]",
79 + denominator, G_MAXINT, G_MAXINT);
80 + goto fail;
81 + }
83 + gst_value_set_fraction (value, numerator, denominator);
85 return 0;