How to get error message for errno value in python?

I use the ctypes module to make some ptrace system calls on Linux, which actually work pretty well. But if I get an error, I want to provide useful information. Therefore, I make a call to the get_errno () function that returns errno, but I did not find any function or something else that interprets the errno value and gives me an associated error message.

Am I missing something? Is there a ctypes based solution?

Here is my setup:

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) ... 

Example:

  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) 
+4
source share
2 answers

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) 
+3
source
 >>> import errno >>> import os >>> os.strerror(errno.ENODEV) 'No such device' 
+1
source

All Articles