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

Armin Rigo report at bugs.python.org
Thu May 22 21:57:04 CEST 2008


Armin Rigo <arigo at users.sourceforge.net> added the comment:

Alternatively, we can try to make ctypes "feel like" C itself:

     ctypes.set_errno(0)
     while True:
         dirent = linux_c_lib.readdir(byref(dir))
         if not dirent:
             if ctypes.get_errno() == 0:
                 break
             else:
                 raise IOError("oups!")

We are doing this kind of thing in PyPy with the "rffi" foreign function
interface.  To achieve this result, ctypes would have to maintain
internally an internal, global (but thread-local) variable representing
the current errno value.  Just before doing a C library function call,
ctypes would copy this variable into the real C-level errno; and
immediately after the call it would read the C-level errno back into its
internal variable.  In this way, other calls to C functions unrelated to
ctypes don't interfere.  The get_errno() and set_errno() functions
merely access the thread-local variable (not the real C-level errno).

----------
nosy: +arigo

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


More information about the Python-bugs-list mailing list