Handle socket protocol and service names according to PEP 383 diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3316,13 +3316,20 @@ for a host. The host argument is a stri static PyObject * socket_getservbyname(PyObject *self, PyObject *args) { + PyObject *nameobj, *protoobj = NULL; char *name, *proto=NULL; struct servent *sp; - if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) + if (!PyArg_ParseTuple(args, "O&|O&:getservbyname", + PyUnicode_FSConverter, &nameobj, + PyUnicode_FSConverter, &protoobj)) return NULL; + name = PyBytes_AS_STRING(nameobj); + proto = protoobj ? PyBytes_AS_STRING(protoobj) : NULL; Py_BEGIN_ALLOW_THREADS sp = getservbyname(name, proto); Py_END_ALLOW_THREADS + Py_DECREF(nameobj); + Py_XDECREF(protoobj); if (sp == NULL) { PyErr_SetString(socket_error, "service/proto not found"); return NULL; @@ -3347,10 +3354,13 @@ static PyObject * socket_getservbyport(PyObject *self, PyObject *args) { int port; + PyObject *protoobj = NULL; char *proto=NULL; struct servent *sp; - if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) + if (!PyArg_ParseTuple(args, "i|O&:getservbyport", + &port, PyUnicode_FSConverter, &protoobj)) return NULL; + proto = protoobj ? PyBytes_AS_STRING(protoobj) : NULL; if (port < 0 || port > 0xffff) { PyErr_SetString( PyExc_OverflowError, @@ -3360,11 +3370,12 @@ socket_getservbyport(PyObject *self, PyO Py_BEGIN_ALLOW_THREADS sp = getservbyport(htons((short)port), proto); Py_END_ALLOW_THREADS + Py_XDECREF(protoobj); if (sp == NULL) { PyErr_SetString(socket_error, "port/proto not found"); return NULL; } - return PyUnicode_FromString(sp->s_name); + return PyUnicode_DecodeFSDefault(sp->s_name); } PyDoc_STRVAR(getservbyport_doc, @@ -3382,13 +3393,17 @@ otherwise any protocol will match."); static PyObject * socket_getprotobyname(PyObject *self, PyObject *args) { + PyObject *nameobj; char *name; struct protoent *sp; - if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) + if (!PyArg_ParseTuple(args, "O&:getprotobyname", + PyUnicode_FSConverter, &nameobj)) return NULL; + name = PyBytes_AS_STRING(nameobj); Py_BEGIN_ALLOW_THREADS sp = getprotobyname(name); Py_END_ALLOW_THREADS + Py_DECREF(nameobj); if (sp == NULL) { PyErr_SetString(socket_error, "protocol not found"); return NULL; @@ -4002,7 +4017,7 @@ socket_getnameinfo(PyObject *self, PyObj set_gaierror(error); goto fail; } - ret = Py_BuildValue("ss", hbuf, pbuf); + ret = Py_BuildValue("sN", hbuf, PyUnicode_DecodeFSDefault(pbuf)); fail: if (res)