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. --- partition.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'partition.c') diff --git a/partition.c b/partition.c index 5284869..8685c3f 100644 --- a/partition.c +++ b/partition.c @@ -48,7 +48,8 @@ static int Partition_init(PartitionObject *self, PyObject *args, PyObject *kwds) }; int partno_follow_default = 0, start_follow_default = 0, - end_follow_default = 0; + end_follow_default = 0, + rc; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ppp", kwlist, @@ -60,16 +61,16 @@ static int Partition_init(PartitionObject *self, PyObject *args, PyObject *kwds) } self->pa = fdisk_new_partition(); - if (fdisk_partition_partno_follow_default(self->pa, partno_follow_default) < 0) { - PyErr_SetString(PyExc_RuntimeError, "Error setting partno_follow_default"); + if ((rc = fdisk_partition_partno_follow_default(self->pa, partno_follow_default) < 0)) { + set_PyErr_from_rc(-rc); return -1; } - if (fdisk_partition_start_follow_default(self->pa, start_follow_default) < 0) { - PyErr_SetString(PyExc_RuntimeError, "Error setting start_follow_default"); + if ((rc = fdisk_partition_start_follow_default(self->pa, start_follow_default) < 0)) { + set_PyErr_from_rc(-rc); return -1; } - if (fdisk_partition_end_follow_default(self->pa, end_follow_default) < 0) { - PyErr_SetString(PyExc_RuntimeError, "Error setting end_follow_default"); + if ((rc = fdisk_partition_end_follow_default(self->pa, end_follow_default) < 0)) { + set_PyErr_from_rc(-rc); return -1; } -- cgit v1.2.3-18-g5258