diff -r 482ec940b413 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Thu Sep 10 19:02:34 2015 -0400 +++ b/Lib/test/test_socket.py Fri Sep 11 19:38:47 2015 +1000 @@ -1282,6 +1282,12 @@ except socket.gaierror: pass + # Issue 24684: str subclasses should not be able to trigger an assertion failure + class BadString(str): + def encode(self, enc): + return -1 + self.assertRaises(TypeError, socket.getaddrinfo, BadString(), 1) + def test_getnameinfo(self): # only IP addresses are allowed self.assertRaises(OSError, socket.getnameinfo, ('mail.python.org',0), 0) diff -r 482ec940b413 Modules/socketmodule.c --- a/Modules/socketmodule.c Thu Sep 10 19:02:34 2015 -0400 +++ b/Modules/socketmodule.c Fri Sep 11 19:38:47 2015 +1000 @@ -5518,7 +5518,12 @@ idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna"); if (!idna) return NULL; - assert(PyBytes_Check(idna)); + if (!PyBytes_Check(idna)) { + Py_DECREF(idna); + PyErr_SetString(PyExc_TypeError, + "getaddrinfo() argument 1 must be encodable string or None"); + return NULL; + } hptr = PyBytes_AS_STRING(idna); } else if (PyBytes_Check(hobj)) { hptr = PyBytes_AsString(hobj);