diff Modules/socketmodule.c @ 69915:7c3a20b5943a 2.7

#10169: Fix argument parsing in socket.sendto() to avoid error masking.
author Ezio Melotti <ezio.melotti@gmail.com>
date Sat, 07 May 2011 19:21:22 +0300
parents cd8d3c2d3bce
children 48743ad2d2ef
line wrap: on
line diff
--- a/Modules/socketmodule.c	Sat May 07 17:40:23 2011 +0300
+++ b/Modules/socketmodule.c	Sat May 07 19:21:22 2011 +0300
@@ -2826,14 +2826,24 @@
     Py_ssize_t len;
     sock_addr_t addrbuf;
     int addrlen, n = -1, flags, timeout;
+    int arglen;
 
     flags = 0;
-    if (!PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro)) {
-        PyErr_Clear();
-        if (!PyArg_ParseTuple(args, "s*iO:sendto",
-                              &pbuf, &flags, &addro))
-            return NULL;
+    arglen = PyTuple_Size(args);
+    switch(arglen) {
+        case 2:
+            PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro);
+            break;
+        case 3:
+            PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro);
+            break;
+        default:
+            PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3"
+                         " arguments (%d given)", arglen);
     }
+    if (PyErr_Occurred())
+        return NULL;
+
     buf = pbuf.buf;
     len = pbuf.len;