Skip to content

ctypes.CDLL on Linux creates a "main program" object when passed an empty string #99874

Closed

Description

Bug report

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:

>>> import ctypes
>>> ctypes.CDLL('')
<CDLL '', handle 7fb30d2d3190 at 0x7fb30c2caa60>

I don't think this is correct, since that dummy object will break things when trying to use it later on.

Your environment

  • CPython versions tested on: 3.8.10, 3.10.8
  • Operating system and architecture: Ubuntu 20.04.5 LTS for x86_64

This doesn't happen on Windows with Python 3.10.7; there I see an exception being thrown for the empty string.

Activity

  1. ghost added
    type-bugAn unexpected behavior, bug, or error
    on Nov 29, 2022
  2. eryksun commented on Nov 29, 2022

    @eryksun
    Contributor

    _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:

    >>> ctypes._dlopen('') == ctypes._dlopen(None)
    True

    This doesn't happen on Windows with Python 3.10.7; there I see an exception being thrown for the empty string.

    WinAPI LoadLibraryExW() requires a library name to load, which cannot be an empty string or NULL.

  3. eryksun commented on Nov 30, 2022

    @eryksun
    Contributor

    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.

  4. added
    docsDocumentation in the Doc dir
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Feb 21, 2025
  5. 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
  6. ZeroIntensity commented on Oct 21, 2026

    @ZeroIntensity
    Member

    I think this was done by the rewrite in #145313.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions