[Bf-python] Consistency issue
Willian Padovani Germano
wgermano at ig.com.br
Thu Oct 2 03:14:26 CEST 2003
Hi,
Months ago, Jacques suggested that the better method would be to have tuples
everywhere. That's not a bad idea, but we had to keep compatibility with
2.25.
Now's a good time to start fixing this where it happens, allowing both cases
(ex: someFunc([f,f,f]) or someFunc(f,f,f) calls).
From: "Michel Selten" <michel.s at home.nl>
(...)
> 3 - Update the setLocation() function to both accept a tuple and a
> list of values. This would imply that both the following calls
> are valid in a script:
> obj.setLocation (0.0, 0.0, 0.0)
> obj.setLocation ((0.0, 0.0, 0.0))
>
> My personal preference would go to option 3.
I like this one, too. And you're right, once we "unwrap" a pytuple, it
isn't available for another parsing. A simple way to solve this: first
check the size of the PyObject *args parameter. If the function expects
[f,f,f] or f, f, f, it's simple to decide: the size of 'args' will be either
1 or 3.
Check this method in rgbTuple.c, for example:
------------------------------------------
PyObject *rgbTuple_setCol (BPy_rgbTuple *self, PyObject *args)
{
int ok;
float r = 0, g = 0, b = 0;
if (PyObject_Length (args) == 3)
ok = PyArg_ParseTuple (args, "fff", &r, &g, &b);
else ok = PyArg_ParseTuple (args, "|(fff)", &r, &g, &b);
if (!ok)
return EXPP_ReturnPyObjError (PyExc_TypeError,
"expected [f,f,f] or f,f,f as arguments (or nothing)");
*(self->rgb[0]) = EXPP_ClampFloat (r, 0.0, 1.0);
*(self->rgb[1]) = EXPP_ClampFloat (g, 0.0, 1.0);
*(self->rgb[2]) = EXPP_ClampFloat (b, 0.0, 1.0);
return EXPP_incr_ret (Py_None);
}
---------------------------------
See? PyObject_Length(args) is used before PyArg_ParseTuple, to handle three
cases: "f,f,f" or "|(f,f,f)" == nothing or (f,f,f).
> the deprecation of the old method in a few releases and support the new
> method only after that.
Well, no need to, we can document the better one and let the old still
there, undocumented.
BTW: Ton mentioned he's on a coding spree (ok, not his words). Our little
corner wouldn't interfere with anything, but even so, I'll wait one or two
days before committing anything.
--
Willian, wgermano at ig.com.br
More information about the Bf-python
mailing list