[Python-checkins] cpython: Issue #1746656: make if_nameindex(), if_indextoname() and if_nametoindex()

charles-francois.natali python-checkins at python.org
Fri May 20 16:54:55 CEST 2011


http://hg.python.org/cpython/rev/cc60d0283fad
changeset:   70226:cc60d0283fad
user:        Charles-François Natali <neologix at free.fr>
date:        Fri May 20 16:55:06 2011 +0200
summary:
  Issue #1746656: make if_nameindex(), if_indextoname() and if_nametoindex()
accept and return str instead of bytes arrays. Add some more tests.

files:
  Lib/test/test_socket.py |  20 ++++++++-
  Modules/socketmodule.c  |  63 ++++++++++++++--------------
  2 files changed, 49 insertions(+), 34 deletions(-)


diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -377,10 +377,26 @@
     def testInterfaceNameIndex(self):
         interfaces = socket.if_nameindex()
         for index, name in interfaces:
+            self.assertIsInstance(index, int)
+            self.assertIsInstance(name, str)
             # interface indices are non-zero integers
             self.assertGreater(index, 0)
-            self.assertEqual(index, socket.if_nametoindex(name))
-            self.assertEqual(name, socket.if_indextoname(index))
+            _index = socket.if_nametoindex(name)
+            self.assertIsInstance(_index, int)
+            self.assertEqual(index, _index)
+            _name = socket.if_indextoname(index)
+            self.assertIsInstance(_name, str)
+            self.assertEqual(name, _name)
+
+    @unittest.skipUnless(hasattr(socket, 'if_nameindex'),
+                         'socket.if_nameindex() not available.')
+    def testInvalidInterfaceNameIndex(self):
+        # test nonexistent interface index/name
+        self.assertRaises(socket.error, socket.if_indextoname, 0)
+        self.assertRaises(socket.error, socket.if_nametoindex, '_DEADBEEF')
+        # test with invalid values
+        self.assertRaises(TypeError, socket.if_nametoindex, 0)
+        self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
 
     def testRefCountGetNameInfo(self):
         # Testing reference count for getnameinfo
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -4280,19 +4280,19 @@
 A value of None indicates that new socket objects have no timeout.\n\
 When the socket module is first imported, the default is None.");
 
-
 #ifdef HAVE_IF_NAMEINDEX
 /* Python API for getting interface indices and names */
 
 static PyObject *
 socket_if_nameindex(PyObject *self, PyObject *arg)
 {
-    int i = 0;
     PyObject *list;
-    struct if_nameindex *ni = if_nameindex();
-
+    int i;
+    struct if_nameindex *ni;
+  
+    ni = if_nameindex();
     if (ni == NULL) {
-        PyErr_SetString(socket_error, "if_nameindex() returned NULL.");
+        PyErr_SetFromErrno(socket_error);
         return NULL;
     }
 
@@ -4302,26 +4302,21 @@
         return NULL;
     }
 
-    while (ni[i].if_index != 0 && i < INT_MAX) {
-        PyObject *ni_tuple = Py_BuildValue(
-                "Iy", ni[i].if_index, ni[i].if_name);
+    for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
+        PyObject *ni_tuple = Py_BuildValue("IO&",
+                ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
 
         if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
             Py_XDECREF(ni_tuple);
-            goto error;
+            Py_DECREF(list);
+            if_freenameindex(ni);
+            return NULL;
         }
         Py_DECREF(ni_tuple);
-
-        ++i;
     }
 
     if_freenameindex(ni);
     return list;
-
-error:
-    Py_DECREF(list);
-    if_freenameindex(ni);
-    return NULL;
 }
 
 PyDoc_STRVAR(if_nameindex_doc,
@@ -4329,18 +4324,20 @@
 \n\
 Returns a list of network interface information (index, name) tuples.");
 
-
-PyObject*
-socket_if_nametoindex(PyObject *self, PyObject *arg)
+static PyObject *
+socket_if_nametoindex(PyObject *self, PyObject *args)
 {
-    char* ifname = PyBytes_AsString(arg);
+    PyObject *oname;
     unsigned long index;
 
-    if (ifname == NULL)
+    if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
+                          PyUnicode_FSConverter, &oname))
         return NULL;
 
-    index = if_nametoindex(ifname);
+    index = if_nametoindex(PyBytes_AS_STRING(oname));
+    Py_DECREF(oname);
     if (index == 0) {
+        /* if_nametoindex() doesn't set errno */
         PyErr_SetString(socket_error, "no interface with this name");
         return NULL;
     }
@@ -4353,20 +4350,22 @@
 \n\
 Returns the interface index corresponding to the interface name if_name.");
 
-
-PyObject*
+static PyObject *
 socket_if_indextoname(PyObject *self, PyObject *arg)
 {
-    unsigned long index = PyLong_AsUnsignedLongMask(arg);
-    char name[IF_NAMESIZE + 1];  /* or use IFNAMSIZ ?*/
-    char *ret = if_indextoname(index, &name[0]);
-
-    if (ret == NULL) {
-        PyErr_SetString(socket_error, "no interface with this index");
+    unsigned long index;
+    char name[IF_NAMESIZE + 1];
+
+    index = PyLong_AsUnsignedLong(arg);
+    if (index == (unsigned long) -1)
+        return NULL;
+
+    if (if_indextoname(index, name) == NULL) {
+        PyErr_SetFromErrno(socket_error);
         return NULL;
     }
 
-    return PyBytes_FromString(name);
+    return PyUnicode_DecodeFSDefault(name);
 }
 
 PyDoc_STRVAR(if_indextoname_doc,
@@ -4436,7 +4435,7 @@
     {"if_nameindex", socket_if_nameindex,
      METH_NOARGS, if_nameindex_doc},
     {"if_nametoindex", socket_if_nametoindex,
-     METH_O, if_nametoindex_doc},
+     METH_VARARGS, if_nametoindex_doc},
     {"if_indextoname", socket_if_indextoname,
      METH_O, if_indextoname_doc},
 #endif

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list