In posix.uname(), decode nodename as ASCII/surrogateescape. Also apply PEP 383 decoding to other fields. diff --git a/Doc/library/os.rst b/Doc/library/os.rst --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -486,8 +486,15 @@ process and user. :func:`socket.gethostname` or even ``socket.gethostbyaddr(socket.gethostname())``. + The strings are converted using the file system encoding and the + ``'surrogateescape'`` error handler, except for ``nodename``, which + is converted as ASCII with the ``'surrogateescape'`` error handler + (see the :mod:`socket` module documentation for details). + Availability: recent flavors of Unix. + .. versionchanged:: XXX + Previously, all the strings were converted as UTF-8. .. function:: unsetenv(key) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -3046,12 +3046,13 @@ posix_uname(PyObject *self, PyObject *no Py_END_ALLOW_THREADS if (res < 0) return posix_error(); - return Py_BuildValue("(sssss)", - u.sysname, - u.nodename, - u.release, - u.version, - u.machine); + return Py_BuildValue("(NNNNN)", + PyUnicode_DecodeFSDefault(u.sysname), + PyUnicode_DecodeASCII(u.nodename, strlen(u.nodename), + "surrogateescape"), + PyUnicode_DecodeFSDefault(u.release), + PyUnicode_DecodeFSDefault(u.version), + PyUnicode_DecodeFSDefault(u.machine)); } #endif /* HAVE_UNAME */