-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
cdll.LoadLibrary allows None as an argument #78773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
LoadLibrary in Python 2.7 through 3.7 accepts None as an argument. I wonder if this has been allowed for a reason? If not, it should probably be changed to raise a TypeError instead. >>> ctypes.cdll.LoadLibrary(None)
<CDLL 'None', handle fffffffffffffffe at 10eedbe90> Interestingly, on Python 2.7 LoadLibrary explicitly mentions None as allowed in the error message: >>> ctypes.cdll.LoadLibrary(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "[...]/ctypes/__init__.py", line 444, in LoadLibrary
return self._dlltype(name)
File "[...]/ctypes/__init__.py", line 366, in __init__
self._handle = _dlopen(self._name, mode)
TypeError: dlopen() argument 1 must be string or None, not int A side-effect of None being allowed is that chaining find_library and LoadLibrary is error-prone: >>> ctypes.cdll.LoadLibrary(find_library("foobar"))
<CDLL 'None', handle fffffffffffffffe at 10ef35fd0> |
This isn't documented, but CDLL(None) is translated to dlopen(NULL), which "causes a search for a symbol in the main program, followed by all shared libraries loaded at program startup, and then all shared libraries loaded by dlopen() with the flag RTLD_GLOBAL" (https://linux.die.net/man/3/dlopen). This is sometimes useful because it lets you look up a symbol in an already-loaded library without needing to give the library's name. For example: >>> import ctypes
>>> dll = ctypes.CDLL(None)
>>> dll.printf
<_FuncPtr object at 0x7f3427d547c0>
>>> dll.PyObject_Str
<_FuncPtr object at 0x7f3427d54880> |
I'm using LoadLibrary(None) commonly to load symbols in Python itself ;-) |
In clear, it's a feature, not a bug. |
We do need to document this in ctypes.rst. Quite a bit of code depends on ctypes.CDLL(None) and similar to get at symbols linked into the interpreter or already dlopened into the process these days. It works on Linux; I'm assuming it likely does on everything POSIXy. |
Indeed. POSIX says:
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: