如何在 python 中获取 errno 值的错误消息?

Nic*_*tti 6 python linux ctypes errno system-calls

我正在使用ctypes模块在 Linux 上执行一些 ptrace 系统调用,实际上效果很好。但如果我遇到错误,我想提供一些有用的信息。因此,我执行了get_errno()函数调用,它返回 errno 的值,但我没有找到任何函数或其他东西来解释 errno 值并给我相关的错误消息。

我错过了什么吗?有基于ctypes的解决方案吗?

这是我的设置:

import logging
from ctypes import get_errno, cdll
from ctypes.util import find_library, errno

# load the c lib
libc = cdll.LoadLibrary(find_library("c"), use_errno=True)
...
Run Code Online (Sandbox Code Playgroud)

例子:

 return_code = libc.ptrace(PTRACE_ATTACH, pid, None, None)
 if return_code == -1:
   errno = get_errno()
   error_msg = # here i wanna provide some information about the error
   logger.error(error_msg)
Run Code Online (Sandbox Code Playgroud)

wbe*_*rry 5

这打印ENODEV: No such device.

import errno, os

def error_text(errnumber):
  return '%s: %s' % (errno.errorcode[errnumber], os.strerror(errnumber))

print error_text(errno.ENODEV)
Run Code Online (Sandbox Code Playgroud)