[issue1798] Add ctypes calling convention that allows safe access of errno

Thomas Heller report at bugs.python.org
Fri May 23 21:34:29 CEST 2008


Thomas Heller <theller at ctypes.org> added the comment:

>> AFAIU, set_errno/get_errno should provide a ctypes-private copy of the real errno.
>> The copy is copied into the 'real' errno just before ffi_call (in Modules/_ctypes/callproc.c),
>> and the real errno is copied in to ctypes copy right after the call.
> 
> If you clear errno, anyway, you can also drop the set_errno call, and

My code sample did not intend to clear errno, it was intended to set errno
to the last value that the ctypes-copy had before the call.

> zero-initialize errno before each call. The point of set_errno would
> be that you have the choice of *not* calling it, i.e. passing into
> the function the errno value that was there before you made the API
> call. If you fill something else into errno always, the application has
> no way of not modifying errno before the API call.

To make my point clear (in case is isn't already): In the code
sample that Armin posted, it is impossible to guarantee that no
stdlib function is called between the readdir() and the get_errno() call:

         dirent = linux_c_lib.readdir(byref(dir))
         if not dirent:
             if ctypes.get_errno() == 0:

Consider the garbage collector free some objects that release resources
they have, in other words call functions different from free(3).

Similarly for calling set_errno() or not calling it; other stdlib function
calls in the meantime may have changed errno and that might not be what
the Python programmer expects.

Anyway, how can we proceed?  Do you have a suggestion?

Should set_errno() set a flag that is examined and consumed before
call_function_pointer() is called?

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1798>
__________________________________


More information about the Python-bugs-list mailing list