Skip to content

ctypes.util.test() calls cdll.load, which LibraryLoader does not have #157763

Description

@tony-CloseAI

Bug report

ctypes.util.test() calls cdll.load(...), which LibraryLoader does not have, so the self-test raises on Windows.

Where: Lib/ctypes/util.py, test() (line 535 on main), the Windows branch:

if os.name == "nt":
    print(cdll.msvcrt)
    print(cdll.load("msvcrt"))

LibraryLoader defines LoadLibrary, not load, and its __getattr__ turns the unknown name into an AttributeError.

Reproduce (Windows, 3.13.5; the line is the same on main, whose util.py already needs 3.15 syntax to import):

> python -c "import ctypes.util; ctypes.util.test()"
<CDLL 'msvcrt', handle 7ffd0a0b0000 at 0x...>
AttributeError: load

Proposed fix:

-        print(cdll.load("msvcrt"))
+        print(cdll.LoadLibrary("msvcrt"))

Checked by running test() on Windows against 3.13.5's util.py: the current code raises AttributeError: load; the fixed code completes and prints the loaded CDLL.

Environment: Windows 11, CPython 3.13.5.


Found while auditing installed code with a verification tool I am building; the reproduction above was run on the version named, and the proposed fix was applied to a local copy and run as well. Report drafted with AI assistance.

Linked PRs

Activity

  1. ZeroIntensity commented on Sep 18, 2026

    @ZeroIntensity
    Member

    I think it would be better to just soft-deprecate this rather than try to support it as a public API.

  2. tony-CloseAI commented on Sep 19, 2026

    @tony-CloseAI
    Author

    Soft-deprecating it sounds right to me: test() is a leftover self-test rather than something worth supporting as a public API.

    If it should keep working until it goes, cdll.LoadLibrary("msvcrt") is the whole change - I ran test() on Windows with it and the function completes. Happy to send either patch, the one-liner or the deprecation, whichever you prefer; equally happy to leave this here as a record if you would rather not touch the function before it is removed.

  3. kumaraditya303 commented on Sep 19, 2026

    @kumaraditya303
    Contributor

    Why keep it at all? There is no value in keeping broken stuff, it can be removed directly.

  4. ZeroIntensity commented on Sep 19, 2026

    @ZeroIntensity
    Member

    I don't think it can be safely removed without a deprecation period (it works on some platforms; that's enough for it to exist somewhere in production). We could do one, but that would arguably be more of a burden than just soft-deprecating it.

  5. added a commit that references this issue on Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytopic-ctypes

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions