Index: Lib/test/test_posix.py =================================================================== --- Lib/test/test_posix.py (revision 61579) +++ Lib/test/test_posix.py (working copy) @@ -9,6 +9,7 @@ import time import os +import sys import pwd import unittest import warnings @@ -190,7 +191,9 @@ def test_strerror(self): if hasattr(posix, 'strerror'): - self.assert_(posix.strerror(0)) + self.assert_(posix.strerror(1)) + if sys.platform in ('darwin',): + self.assertRaises(ValueError, posix.strerror, -1) def test_pipe(self): if hasattr(posix, 'pipe'): Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 61579) +++ Modules/posixmodule.c (working copy) @@ -6717,16 +6717,21 @@ static PyObject * posix_strerror(PyObject *self, PyObject *args) { - int code; + int code, old_errno; char *message; if (!PyArg_ParseTuple(args, "i:strerror", &code)) return NULL; + old_errno = errno; message = strerror(code); - if (message == NULL) { - PyErr_SetString(PyExc_ValueError, - "strerror() argument out of range"); - return NULL; - } + /* ISO C/POSIX do not allow NULL returns from strerror */ + assert(message); + if (errno == EINVAL) { + PyErr_SetString(PyExc_ValueError, + "strerror() argument out of range"); + return NULL; + } + errno = old_errno; + return PyString_FromString(message); }