Python 中包装的 c 函数出错后获取 errno

Getting errno after an error in wrapped c function in Python

我正在学习如何在出现错误后使用 Python 的 ctypes' and am able to wrap and call a function, but I can't figure out how to get theerrno` 包装 c 函数。对于此示例,我将 inotify_add_watch 换行。这不是完整的代码,只是会导致错误的示例:

import ctypes

c_lib = ctypes.cdll.LoadLibrary('libc.so.6')

inotify_add = c_lib.inotify_add_watch
inotify_add.argtypes = (ctypes.c_int, ctypes.c_char_p, ctypes.c_uint32)
inotify_add.restype = ctypes.c_int

# This will cause an EBADF error
inotify_add(32, b'/tmp', 1)  # returns -1

我链接的文档说这将 return -1 它确实如此,但它也会适当地设置 errno 。我现在不知道如何访问 errno。如果我尝试 ctypes.get_errno(),这只是 returns 0。如果我尝试调用 c_lib.errno(),这会导致 segmentation error,因此它也不起作用。有什么方法可以检索 errno?

您必须从构建 Python 时使用的同一个 CRT 库中获取 errno。因此 Python 3.7 在 Windows 上。我没有 Linux 方便的尝试,所以希望这能让你朝着正确的方向前进。

>>> dll = CDLL('ucrtbase',use_errno=True)
>>> get_errno()
0
>>> dll._access(b'nonexisting',0)
-1
>>> get_errno()
2

Errno 2 是 ENOENT(没有这样的文件或目录)所以它已设置并且看起来正确。

不同的 CRT 库具有不同的 errno 实例,因此 Python 无法正确捕获它以用于 set_errno()/get_errno()