[Python-ideas] Provide additional debug info for OSError and WindowsError

Giampaolo Rodola' g.rodola at gmail.com
Fri Apr 12 10:25:53 EDT 2019


When dealing with C extensions from Python there are circumstances
where a function is called and we get an OSError(errno) exception
without knowing what exactly went wrong internally. This is especially
not obvious on Windows, where multiple MSDN APIs may be invoked within
the same C function and we're not sure which one of them failed.

There are other times where the underlying C syscall is obvious, but
it would still be useful to append some additional information. One
example is socket.bind():
https://github.com/python/cpython/blob/56065d4c8ac03042cb7e29ffda9b1ac544a37b4d/Lib/asyncio/base_events.py#L940-L949

In order to work around that in psutil (on Windows) I stored the debug
msg string in OSError.filename attribute:
https://github.com/giampaolo/psutil/pull/1428/
As such I was thinking that perhaps it would be nice to provide 2 new
cPython APIs:

PyErr_SetFromErrnoWithMsg(PyObject *type, const char *msg)
PyErr_SetFromWindowsErrWithMsg(int ierr, const char *msg)
PyErr_SetExcFromWindowsErrWithMsg(PyObject *type, int ierr, const char *msg)

With this in place also OSError and WindowsError would probably have
to host a new "extramsg" attribute or something (but not necessarily).

Thoughts?

-- 
Giampaolo - http://grodola.blogspot.com


More information about the Python-ideas mailing list