Provide additional debug info for OSError and WindowsError

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/56065d4c8ac03042cb7e29ffda9b1ac544a37... 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

On 4/12/19, Giampaolo Rodola' <g.rodola@gmail.com> wrote:
Existing error handling would benefit from this proposal. win32_error [1], win32_error_object_error, and PyErr_SetFromWindowsErrWithFunction [2] take a function name that's currently ignored. [1]: https://github.com/python/cpython/blob/v3.7.3/Modules/posixmodule.c#L1403 [2]: https://github.com/python/cpython/blob/v3.7.3/PC/winreg.c#L26

On 4/12/19, Giampaolo Rodola' <g.rodola@gmail.com> wrote:
Existing error handling would benefit from this proposal. win32_error [1], win32_error_object_error, and PyErr_SetFromWindowsErrWithFunction [2] take a function name that's currently ignored. [1]: https://github.com/python/cpython/blob/v3.7.3/Modules/posixmodule.c#L1403 [2]: https://github.com/python/cpython/blob/v3.7.3/PC/winreg.c#L26
participants (2)
-
eryk sun
-
Giampaolo Rodola'