Skip to content

Commit 8fe84d6

Browse files
committed
add : OSError_format_errno method added for finer printing.
1 parent 36c3512 commit 8fe84d6

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Objects/exceptions.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,33 @@ OSError_traverse(PyObject *op, visitproc visit, void *arg)
23382338
return BaseException_traverse(op, visit, arg);
23392339
}
23402340

2341+
static PyObject *
2342+
OSError_format_errno(PyObject *myerrno)
2343+
{
2344+
if (myerrno == NULL) {
2345+
return Py_NewRef(Py_None);
2346+
}
2347+
if (!PyLong_Check(myerrno)) {
2348+
return PyObject_Str(myerrno);
2349+
}
2350+
long err = PyLong_AsLong(myerrno);
2351+
if (err == -1 && PyErr_Occurred()) {
2352+
PyErr_Clear();
2353+
return PyObject_Str(myerrno);
2354+
}
2355+
2356+
const char *name = NULL;
2357+
#define add_errcode(n, code, comment) \
2358+
if ((code) == err) { name = (n); } // search over the errornames
2359+
#include "errnonames.h"
2360+
#undef add_errcode
2361+
2362+
if (name != NULL) {
2363+
return PyUnicode_FromFormat("%S (%s)", myerrno, name);
2364+
}
2365+
return PyObject_Str(myerrno);
2366+
}
2367+
23412368
static PyObject *
23422369
OSError_str(PyObject *op)
23432370
{

0 commit comments

Comments
 (0)