Commit 1ea654b4 authored by Simon Feltman's avatar Simon Feltman
Browse files

Unify Python float and double to GI marshaling code

Change _pygi_argument_from_object to use the cachers marshalers
(_pygi_marshal_from_py_float/double) directly instead of keeping a
copy of the code.
Refactor _pygi_marshal_from_py_float/double to use a common utility
_pygi_py_arg_to_double for initial error checking and conversion.

https://bugzilla.gnome.org/show_bug.cgi?id=693405
parent 2eb2a712
......@@ -999,32 +999,14 @@ _pygi_argument_from_object (PyObject *object,
}
case GI_TYPE_TAG_FLOAT:
{
PyObject *float_;
float_ = PyNumber_Float (object);
if (float_ == NULL) {
PyErr_SetString (PyExc_TypeError, "expected float or int argument");
break;
}
arg.v_float = (float) PyFloat_AsDouble (float_);
Py_DECREF (float_);
_pygi_marshal_from_py_float (NULL, NULL, NULL,
object, &arg);
break;
}
case GI_TYPE_TAG_DOUBLE:
{
PyObject *float_;
float_ = PyNumber_Float (object);
if (float_ == NULL) {
PyErr_SetString (PyExc_TypeError, "expected float or int argument");
break;
}
arg.v_double = PyFloat_AsDouble (float_);
Py_DECREF (float_);
_pygi_marshal_from_py_double (NULL, NULL, NULL,
object, &arg);
break;
}
case GI_TYPE_TAG_GTYPE:
......
......@@ -696,15 +696,10 @@ check_valid_double (double x, double min, double max)
return TRUE;
}
gboolean
_pygi_marshal_from_py_float (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
static gboolean
_pygi_py_arg_to_double (PyObject *py_arg, double *double_)
{
PyObject *py_float;
double double_;
if (!PyNumber_Check (py_arg)) {
PyErr_Format (PyExc_TypeError, "Must be number, not %s",
......@@ -716,14 +711,29 @@ _pygi_marshal_from_py_float (PyGIInvokeState *state,
if (!py_float)
return FALSE;
double_ = PyFloat_AsDouble (py_float);
*double_ = PyFloat_AsDouble (py_float);
Py_DECREF (py_float);
return TRUE;
}
gboolean
_pygi_marshal_from_py_float (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
double double_;
if (!_pygi_py_arg_to_double (py_arg, &double_))
return FALSE;
if (PyErr_Occurred () || !check_valid_double (double_, -G_MAXFLOAT, G_MAXFLOAT))
return FALSE;
arg->v_float = double_;
return TRUE;
}
......@@ -734,27 +744,15 @@ _pygi_marshal_from_py_double (PyGIInvokeState *state,
PyObject *py_arg,
GIArgument *arg)
{
PyObject *py_float;
double double_;
if (!PyNumber_Check (py_arg)) {
PyErr_Format (PyExc_TypeError, "Must be number, not %s",
py_arg->ob_type->tp_name);
return FALSE;
}
py_float = PyNumber_Float (py_arg);
if (!py_float)
if (!_pygi_py_arg_to_double (py_arg, &double_))
return FALSE;
double_ = PyFloat_AsDouble (py_float);
Py_DECREF (py_float);
if (PyErr_Occurred () || !check_valid_double (double_, -G_MAXDOUBLE, G_MAXDOUBLE))
return FALSE;
arg->v_double = double_;
return TRUE;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment