You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ctypes.CDLL on Linux creates a "main program" object when passed an empty string #99874
ctypes.CDLL throws an exception when it can't find a library in the path pointed at by its name argument; however, if name is an empty string a dummy object is created:
_ctypes.dlopen() converts the string via PyUnicode_FSConverter() and passes it to to the system dlopen() function. The interpretation of an empty string depends on the system. For dlopen(), POSIX does not specify the behavior when file is an empty string. On Linux, it's the same as passing NULL. For example:
that's a leaky abstraction, since I don't see any parts of the documentation indicating that this behaves like the underlying dlopen nor that the behavior may differ between OSs.
The documentation does contain a general statement to this effect:
All these classes can be instantiated by calling them with at least one argument, the pathname of the shared library. If you have an existing handle to an already loaded shared library, it can be passed as the handle named parameter, otherwise the underlying platforms dlopen or LoadLibrary function is used to load the library into the process, and to get a handle to it.
However, it's not explicit about how the pathname is used with respect to the platform library loading function. On POSIX, it's just passed along to dlopen(). On Windows, if winmode isn't used, any name that includes a path separator is first resolved as an absolute path, and the loader flags value is modified to include LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR. On AIX, certain pathnames can modify the mode parameter.
Note that ctypes.pythonapi is implemented as PyDLL(None) on POSIX, which depends on the documented behavior of dlopen(). But this fact is not explicitly mentioned in the ctypes documentation.
changed the title [-]ctypes.CDLL creates a dummy object when passed an empty string[/-][+]ctypes.CDLL on Linux creates a "main program" object when passed an empty string[/+]on Feb 21, 2025
Activity
eryksun commentedon Nov 29, 2022
_ctypes.dlopen()converts the string viaPyUnicode_FSConverter()and passes it to to the systemdlopen()function. The interpretation of an empty string depends on the system. Fordlopen(), POSIX does not specify the behavior whenfileis an empty string. On Linux, it's the same as passingNULL. For example:WinAPI
LoadLibraryExW()requires a library name to load, which cannot be an empty string orNULL.eryksun commentedon Nov 30, 2022
The documentation does contain a general statement to this effect:
However, it's not explicit about how the pathname is used with respect to the platform library loading function. On POSIX, it's just passed along to
dlopen(). On Windows, ifwinmodeisn't used, any name that includes a path separator is first resolved as an absolute path, and the loader flags value is modified to includeLOAD_LIBRARY_SEARCH_DLL_LOAD_DIR. On AIX, certain pathnames can modify themodeparameter.Note that
ctypes.pythonapiis implemented asPyDLL(None)on POSIX, which depends on the documented behavior ofdlopen(). But this fact is not explicitly mentioned in the ctypes documentation.[-]ctypes.CDLL creates a dummy object when passed an empty string[/-][+]ctypes.CDLL on Linux creates a "main program" object when passed an empty string[/+]ZeroIntensity commentedon Oct 21, 2026
I think this was done by the rewrite in #145313.