Issue #5421: Fix misleading error message when one of socket.sendto()'s
arguments has the wrong type.  Patch by Nikita Vetoshkin.
diff --git a/Misc/ACKS b/Misc/ACKS
index 5d9b198..99554da 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -823,6 +823,7 @@
 Frank Vercruesse
 Mike Verdone
 Jaap Vermeulen
+Nikita Vetoshkin
 Al Vezza
 Jacques A. Vidrine
 John Viega
diff --git a/Misc/NEWS b/Misc/NEWS
index 58eea2d..f18e557 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -40,6 +40,9 @@
 Library
 -------
 
+- Issue #5421: Fix misleading error message when one of socket.sendto()'s
+  arguments has the wrong type.  Patch by Nikita Vetoshkin.
+
 - Issue #11401: fix handling of headers with no value; this fixes a regression
   relative to Python2 and the result is now the same as it was in Python2.
 
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index b04d246..5e911e0 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -2647,17 +2647,28 @@
     Py_buffer pbuf;
     PyObject *addro;
     char *buf;
-    Py_ssize_t len;
+    Py_ssize_t len, arglen;
     sock_addr_t addrbuf;
     int addrlen, n = -1, flags, timeout;
 
     flags = 0;
-    if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
-        PyErr_Clear();
-        if (!PyArg_ParseTuple(args, "y*iO:sendto",
-                              &pbuf, &flags, &addro))
-            return NULL;
+    arglen = PyTuple_Size(args);
+    switch (arglen) {
+        case 2:
+            PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
+            break;
+        case 3:
+            PyArg_ParseTuple(args, "y*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;