This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: _ctypes.dlopen does not include errno in OSError
Type: behavior Stage:
Components: ctypes Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: anacrolix, pitrou, santoso.wijaya, vladris
Priority: normal Keywords:

Created on 2011-06-29 00:07 by anacrolix, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
dlopen_raise.py anacrolix, 2011-06-29 00:07 test case
Messages (8)
msg139382 - (view) Author: Matt Joiner (anacrolix) Date: 2011-06-29 00:07
_ctypes.dlopen is not including the errno when it raises OSError.

This occurs when attempting to load a library that doesn't exist, the error string given is clearly generated from an ENOENT.

joiner@dbssyd800:~$ python3 dlopen_raise.py
None
somelib.so: cannot open shared object file: No such file or directory
msg139387 - (view) Author: Santoso Wijaya (santoso.wijaya) * Date: 2011-06-29 01:09
On Windows:

>>> try:
...     ctypes.CDLL('somelib')
... except OSError as exc:
...     print repr(exc)
...     print exc.errno
...
WindowsError(126, 'The specified module could not be found')
22
msg141592 - (view) Author: Matt Joiner (anacrolix) Date: 2011-08-03 01:47
Should I just submit a patch for this myself? Can someone confirm the behaviour is incorrect so I don't waste time fixing it?
msg141617 - (view) Author: Santoso Wijaya (santoso.wijaya) * Date: 2011-08-04 00:27
At least in Windows, the exception object has its `winerror` attribute correctly set to 126, which is also translated to POSIX `errno` with `winerror_to_errno()`; the latter gives us EINVAL (22).
msg141618 - (view) Author: Santoso Wijaya (santoso.wijaya) * Date: 2011-08-04 00:37
From what I gather from the code, when dlopen fails in POSIX platforms, ctypes raises an PyExc_OSError instantiated with a simple string (via PyErr_SetString()). I suppose this could be changed to raise a more complex tuple, instead (like its WindowsError equivalent when LoadLibrary fails in Windows platforms), but that might break existing code that relies on this behavior. 3.3 only?
msg141754 - (view) Author: Vlad Riscutia (vladris) Date: 2011-08-07 22:45
Not sure how this can be fixed actually. I don't think PyErr_SetFromErrno instead of PyErr_SetString would work because as far as I can tell, standard does not mention dlopen making use of errno. Unlike Windows, where LoadLibrary will change last-error, if dlopen fails, only way to retrieve error is to dlerror which returns a string, no error code.
msg141759 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-08-08 07:25
Indeed, it looks more like a bug in the POSIX standard unfortunately.
I'm suggesting to close as "won't fix".
msg141760 - (view) Author: Matt Joiner (anacrolix) Date: 2011-08-08 07:42
I didn't notice there was no use of errno. It's quite possible that dlopen might be used without the C library present, so perhaps this is why it wasn't included. The error strings however are very C-like, which made me think of this in the first place. Thanks all.
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56646
2011-08-08 07:49:21pitrousetstatus: open -> closed
2011-08-08 07:42:33anacrolixsetstatus: pending -> open

messages: + msg141760
2011-08-08 07:25:03pitrousetstatus: open -> pending

nosy: + pitrou
messages: + msg141759

resolution: wont fix
2011-08-07 22:45:49vladrissetnosy: + vladris
messages: + msg141754
2011-08-04 00:37:58santoso.wijayasetmessages: + msg141618
2011-08-04 00:27:38santoso.wijayasetmessages: + msg141617
versions: + Python 3.3
2011-08-03 01:47:45anacrolixsetmessages: + msg141592
2011-06-29 01:09:40santoso.wijayasetnosy: + santoso.wijaya
messages: + msg139387
2011-06-29 00:07:06anacrolixcreate