Skip to content

Commit a6f163f

Browse files
celebdorDavid Sommerseth
authored andcommitted
fix get_module errno setting.
The current code is only setting the errno string but doesn't set the first argument to the IOError exception, i.e., errno. This patch builds a tuple (errno, strerror(errno)) so that the Exception has the errno properly set. Signed-off-by: Antoni S. Puimedon <asegurap@redhat.com> Signed-off-by: David Sommerseth <davids@redhat.com>
1 parent b15cd54 commit a6f163f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

python-ethtool/ethtool.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
450450

451451
if (err < 0) { /* failed? */
452452
int eno = errno;
453+
PyObject *err_obj;
453454
FILE *file;
454455
int found = 0;
455456
char driver[101], dev[101];
@@ -458,8 +459,11 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
458459
/* Before bailing, maybe it is a PCMCIA/PC Card? */
459460
file = fopen("/var/lib/pcmcia/stab", "r");
460461
if (file == NULL) {
461-
sprintf(buf, "[Errno %d] %s", eno, strerror(eno));
462-
PyErr_SetString(PyExc_IOError, buf);
462+
err_obj = Py_BuildValue("(is)", eno, strerror(eno));
463+
if (err_obj != NULL) {
464+
PyErr_SetObject(PyExc_IOError, err_obj);
465+
Py_DECREF(err_obj);
466+
}
463467
return NULL;
464468
}
465469

@@ -480,8 +484,11 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
480484
}
481485
fclose(file);
482486
if (!found) {
483-
sprintf(buf, "[Errno %d] %s", eno, strerror(eno));
484-
PyErr_SetString(PyExc_IOError, buf);
487+
err_obj = Py_BuildValue("(is)", eno, strerror(eno));
488+
if (err_obj != NULL) {
489+
PyErr_SetObject(PyExc_IOError, err_obj);
490+
Py_DECREF(err_obj);
491+
}
485492
return NULL;
486493
} else
487494
return PyString_FromString(driver);
@@ -514,8 +521,7 @@ static PyObject *get_businfo(PyObject *self __unused, PyObject *args)
514521
/* Open control socket. */
515522
fd = socket(AF_INET, SOCK_DGRAM, 0);
516523
if (fd < 0) {
517-
PyErr_SetString(PyExc_OSError, strerror(errno));
518-
return NULL;
524+
return PyErr_SetFromErrno(PyExc_OSError);
519525
}
520526

521527
/* Get current settings. */

0 commit comments

Comments
 (0)