From 5ec9ec73c8189023256a6d737600382f9e3d288a Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Thu, 15 Dec 2022 14:58:22 +0100 Subject: fdisk: add set_PyErr_from_rc python-libfdisk raises Python exceptions when the libfdisk reports an error when executing some function. libfdisk returns negative errno values when reporting some error. Adds utility function to set PyErr string based on the strerror of a given errno code. Useful when raising Python exceptions. --- fdisk.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'fdisk.c') diff --git a/fdisk.c b/fdisk.c index d7d3fe4..d78e6e2 100644 --- a/fdisk.c +++ b/fdisk.c @@ -14,6 +14,25 @@ #include "fdisk.h" +void *set_PyErr_from_rc(int e) +{ + switch (e) { + case ENOMEM: + PyErr_SetString(PyExc_MemoryError, strerror(e)); + break; + case EINVAL: + PyErr_SetString(PyExc_TypeError, strerror(e)); + break; + case ENOSYS: + PyErr_SetString(PyExc_NotImplementedError, strerror(e)); + break; + default: + PyErr_SetString(PyExc_Exception, strerror(e)); + } + + return NULL; +} + PyObject *PyObjectResultStr(const char *s) { PyObject *result; -- cgit v1.2.3-18-g5258