Title: cdll.LoadLibrary allows None as an argument
Type: Stage: resolved
Components: Versions: Python 3.7, Python 3.6, Python 3.4, Python 3.5, Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Malcolm Smith, superbobry, vstinner
Priority: normal Keywords:

Created on 2018-09-05 21:35 by superbobry, last changed 2020-02-11 12:25 by vstinner. This issue is now closed.

Messages (4)
msg324654 - (view) Author: Sergei Lebedev (superbobry) Date: 2018-09-05 21:35
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/", line 444, in LoadLibrary
    return self._dlltype(name)
  File "[...]/ctypes/", 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>
msg361734 - (view) Author: Malcolm Smith (Malcolm Smith) Date: 2020-02-10 22:25
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" (

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>
msg361735 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-10 22:27
I'm using LoadLibrary(None) commonly to load symbols in Python itself ;-)
msg361807 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-11 12:25
In clear, it's a feature, not a bug.
Date User Action Args
2020-02-11 12:25:18vstinnersetstatus: open -> closed
resolution: not a bug
messages: + msg361807

stage: resolved
2020-02-10 22:27:11vstinnersetnosy: + vstinner
messages: + msg361735
2020-02-10 22:25:02Malcolm Smithsetnosy: + Malcolm Smith
messages: + msg361734
2018-09-05 21:35:41superbobrycreate