I am stuck with a fairly complex Python module that does not return useful error codes (it actually fails disturbingly silently). However, the underlying C library it calls sets errno.
Normally errno comes in over OSError attributes, but since I don’t have an exception, I can’t get at it.
Using ctypes, libc.errno doesn’t work because errno is a macro in GNU libc. Python 2.6 has some affordances but Debian still uses Python 2.5. Inserting a C module into my pure Python program just to read errno disgusts me.
Is there some way to access errno? A Linux-only solution is fine, since the library being wrapped is Linux-only. I also don’t have to worry about threads, as I’m only running one thread during the time in which this can fail.
Update: On Python 2.6+, use
ctypes.get_errno().Python 2.5
Belowed code is not reliable (or comprehensive, there are a plefora of ways
errnocould be defined) but it should get you started (or reconsider your position on a tiny extension module (after all on Debianpython setup.py installoreasy_installshould have no problem to build it)). From http://codespeak.net/pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.pyWhere
standard_c_lib: