Re: What's the proper way of handling error-checking return values?
Thomas Heller <theller <at> python.net>
2005-09-02 19:52:17 GMT
Thomas Heller <theller <at> python.net> writes:
> Thomas Heller <theller <at> python.net> writes:
>
>> "Mike C. Fletcher" <mcfletch <at> rogers.com> writes:
>>
>>> How about an alternate proposal:
>>>
>>> For the cFunction return-type protocol there shall be two entry points:
>>>
>>> restype -- a ctypes instance *or* a callable value, required,
>>> default c_int
>>> errcheck -- a callable instance taking { return-value from
>>> restype, the cFunction instance, and the set of argument objects
>>> passed into the call }, and checking for errors, optional
>>>
>>> The advantage here is that we don't need a different "type" of c_int
>>> to do error checking for a return value, instead, we assign a
>>> property on the function that wants the error checking. This should
>>> be completely backward compatible, it simply introduces a new entry
>>> point. In psuedo-code it looks like this:
>>>
>>> result = TraditionalGetResult( ... )
>>> if (function.errcheck):
>>> # errcheck raises errors on failure, or can mutate result
>>> result = function.errcheck( result, function, arguments )
>>> return result
>>>
>>> The use of callable values for restype is deprecated, but provided
>>> for backward compatability. I'm not sure, is there the potential
>>> for the raw copy of memory into result type to get "result" has a
>>> failure case? Anyway, that would be no different than the current
>>> situation if I'm reading correctly.
>>>
>>> Why all the extra arguments to errcheck? To allow for proper
>>> error-messages and to allow the handlers to be generically coded
>>> (i.e. you get a pointer to the cFunction so that you can decide
>>> which function it is in which you are checking for errors).
>>>
>>> In short, I'd rather see the checking code stored on the cFunction
>>> object. I actually have this functionality in the "wrapper" module in
>>> OpenGL-ctypes, it's just that I don't want to have to use (the
>>> comparatively heavy) wrapper module for *every* OpenGL call.
>>
>> This proposal looks rather good. It lets me even implement proper error
>> handling for COM method calls where the interface id is needed to call
>> InterfaceSupportsErrorInfo. I think I'll go with this.
>
> I just wanted to start implementing Mike's suggestion when I found that
> there *is* already an implementation of the protocol that *I* suggested.
> From the ChangeLog:
>
> 2004-10-29 Thomas Heller <theller <at> python.net>
>
> * (Message): Define a new protocol for function's restype. If it
> is a ctypes' type, it is handled in the usual way. But if it
> additionally has an _check_retval_ attribute, this is called with
> the result, and the return value of this call is the functions
> return value.
>
> This has not yet been advertized, and it does neither pass the function
> nor the arguments to the 'checker', but maybe it allows some experiments.
> Anyway, I'll implement the new protocol although I cannot promise how
> fast it will go.
I've repaired the _check_retval_ protocol in CVS (it worked only for
simple restypes, like c_int or c_void_p and subclasses, and leaked
references in error situations), but I would discourage using it. I
will probably change it to be more compatible with the new 'errcheck'
protocol.
I've also made a first implementation of Mike's 'errcheck' protocol,
although the complete details are not yet fleshed out (not about the
protocol itself, but its further integration in ctypes).
I have made one extension to Mike's proposal. The errcheck function
normally receives (result, function, arguments) results, but should be
able to accept more arguments. So, the signature should be
def errcheck(result, arguments, function, *others):
...
Thomas
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf