Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6a4716

Browse files
committed
Add strerror() interface.
1 parent a2f626f commit b6a4716

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

Modules/posixmodule.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ static char posix_putenv__doc__[] =
20062006
Change or add an environment variable.";
20072007

20082008
static PyObject *
2009-
posix_putenv(self,args)
2009+
posix_putenv(self, args)
20102010
PyObject *self;
20112011
PyObject *args;
20122012
{
@@ -2026,7 +2026,32 @@ posix_putenv(self,args)
20262026
Py_INCREF(Py_None);
20272027
return Py_None;
20282028
}
2029-
#endif
2029+
#endif /* putenv */
2030+
2031+
#ifdef HAVE_STRERROR
2032+
static char posix_strerror__doc__[] =
2033+
"strerror(code) -> string\n\
2034+
Translate an error code to a message string.";
2035+
2036+
PyObject *
2037+
posix_strerror(self, args)
2038+
PyObject *self;
2039+
PyObject *args;
2040+
{
2041+
int code;
2042+
char *message;
2043+
if (!PyArg_ParseTuple(args, "i", &code))
2044+
return NULL;
2045+
message = strerror(code);
2046+
if (message == NULL) {
2047+
PyErr_SetString(PyExc_ValueError,
2048+
"strerror code out of range");
2049+
return NULL;
2050+
}
2051+
return PyString_FromString(message);
2052+
}
2053+
#endif /* strerror */
2054+
20302055

20312056
static PyMethodDef posix_methods[] = {
20322057
{"chdir", posix_chdir, 0, posix_chdir__doc__},
@@ -2151,6 +2176,9 @@ static PyMethodDef posix_methods[] = {
21512176
#endif
21522177
#ifdef HAVE_PUTENV
21532178
{"putenv", posix_putenv, 1, posix_putenv__doc__},
2179+
#endif
2180+
#ifdef HAVE_STRERROR
2181+
{"strerror", posix_strerror, 1, posix_strerror__doc__},
21542182
#endif
21552183
{NULL, NULL} /* Sentinel */
21562184
};

0 commit comments

Comments
 (0)