On 03/21/2012 09:23 PM, Eric Blake wrote:
On 03/21/2012 07:22 AM, Guannan Ren wrote:
> On 03/21/2012 08:07 PM, Eric Blake wrote:
>> Maybe we need a wrapper:
>>
>> int libvirt_boolUnwrap(PyObject *obj, bool *value) {
>> int ret = PyObject_IsTrue(obj);
>> if (ret< 0)
>> return ret;
>> *value = ret> 0;
>> return 0;
>> }
>>
>> and then callers become:
>>
>> if (libvirt_boolUnwrap(value,&temp->value.b)< 0)
>> goto cleanup;
>>
> The libvirt_boolUnwrap is okay to be flexible like this, we still need
> the caller to check whether or not the value given from user python code
> is boolean argument, we need to do the data type checking in python C
> API
> before invoking the wrapper.
But that's my point - the wrapper should be doing the type checking
(PyObject_IsTrue already _does_ some type checking - that's why it can
fail with -1 with an already decent python exception created). Callers
shouldn't have to duplicate the work which can be done in just one
place, but instead just pass in an object of unknown type and let the
wrapper do the work of both type validation and conversion if the type
was usable as a boolean.
I get your points, sorry I don't express my idea clearly.
According to my experience, for PyObject_IsTrue, all of non-null
objects
belongs to True, null objects like {}, ""(str) ,None, False
counts as False, it's good way to work with Pybool_check to ensure
the type of value given by upper python code is right type. The
wrapper
should do the job.