[PATCH 1/1] fix get_module errno setting.

Antoni Segura Puimedon asegurap at redhat.com
Mon Nov 11 15:21:26 UTC 2013


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 at redhat.com>
---
 python-ethtool/ethtool.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/python-ethtool/ethtool.c b/python-ethtool/ethtool.c
index a79c438..881cdc4 100644
--- a/python-ethtool/ethtool.c
+++ b/python-ethtool/ethtool.c
@@ -490,8 +490,7 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
 	/* Open control socket. */
 	fd = socket(AF_INET, SOCK_DGRAM, 0);
 	if (fd < 0) {
-		PyErr_SetString(PyExc_OSError, strerror(errno));
-		return NULL;
+		return PyErr_SetFromErrno(PyExc_OSError);
 	}
 
 	/* Get current settings. */
@@ -499,6 +498,7 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
 
 	if (err < 0) {  /* failed? */
 		int eno = errno;
+		PyObject *err_obj;
 		FILE *file;
 		int found = 0;
 		char driver[101], dev[101];
@@ -507,8 +507,11 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
 		/* Before bailing, maybe it is a PCMCIA/PC Card? */
 		file = fopen("/var/lib/pcmcia/stab", "r");
 		if (file == NULL) {
-			sprintf(buf, "[Errno %d] %s", eno, strerror(eno));
-			PyErr_SetString(PyExc_IOError, buf);
+			err_obj = Py_BuildValue("(is)", eno, strerror(eno));
+                        if (err_obj != NULL) {
+				PyErr_SetObject(PyExc_IOError, err_obj);
+				Py_DECREF(err_obj);
+			}
 			return NULL;
 		}
 
@@ -529,8 +532,11 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
 		}
 		fclose(file);
 		if (!found) {
-			sprintf(buf, "[Errno %d] %s", eno, strerror(eno));
-			PyErr_SetString(PyExc_IOError, buf);
+			err_obj = Py_BuildValue("(is)", eno, strerror(eno));
+                        if (err_obj != NULL) {
+				PyErr_SetObject(PyExc_IOError, err_obj);
+				Py_DECREF(err_obj);
+			}
 			return NULL;
 		} else
 			return PyString_FromString(driver);
-- 
1.8.4.2


More information about the python-ethtool-devel mailing list