Access to errno from Python?

前端 未结 6 1340
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 05:00

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

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 05:21

    Gave up and tracked through the C headers.

    import ctypes
    c = ctypes.CDLL("libc.so.6")
    c.__errno_location.restype = ctypes.POINTER(ctypes.c_int)
    c.write(5000, "foo", 4)
    print c.__errno_location().contents # -> c_long(9)
    

    It doesn't work in the python command prompt because it resets errno to read from stdin.

    Once you know the magic word of __errno_location this looks like a common pattern. But with just errno I was pretty lost.

提交回复
热议问题