Skip to content

CDLL __init__ no longer supports name being passed as None when the handle is not None #83424

Closed

Description

@DavidHeffernan
BPO 39243
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba, @jonnyhsu

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:

assignee = None
closed_at = None
created_at = <Date 2020-01-07.10:53:45.831>
labels = ['easy', 'type-bug', '3.8', '3.9', 'ctypes', 'OS-windows']
title = 'CDLL __init__ no longer supports name being passed as None when the handle is not None'
updated_at = <Date 2020-06-07.06:19:59.024>
user = 'https://bugs.python.org/DavidHeffernan'

bugs.python.org fields:

activity = <Date 2020-06-07.06:19:59.024>
actor = 'Jonathan Hsu'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Windows', 'ctypes']
creation = <Date 2020-01-07.10:53:45.831>
creator = 'David Heffernan'
dependencies = []
files = []
hgrepos = []
issue_num = 39243
keywords = ['easy', '3.8regression', 'newcomer friendly']
message_count = 7.0
messages = ['359501', '359536', '359539', '359540', '359543', '359565', '359607']
nosy_count = 7.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'David Heffernan', 'Jonathan Hsu']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'test needed'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39243'
versions = ['Python 3.8', 'Python 3.9']

Linked PRs

Activity

  1. DavidHeffernan commented on Jan 7, 2020

    DavidHeffernanmannequin
    MannequinAuthor

    When creating an instance of CDLL (or indeed WinDLL) for a DLL that is already loaded, you pass the HMODULE in the handle argument to the constructor.

    In older versions of ctypes you could pass None as the name argument when doing so. However, the changes in 2438cdf now mean that such code fails with a NoneType is not iterable error.

    The relevant change is in __init__ for CDLL. The code inside the if _os.name == "nt" block sets up mode, but this is pointless is handle is not None. Because the mode variable is never used, rightly so because the DLL is already loaded.

    The issue could be resolved by changing

    if _os.name == "nt":

    to

    if _os.name == "nt" and handle is not None:

    The following program demonstrates the issue:

    import ctypes
    
    handle = ctypes.windll.kernel32._handle
    print(handle)
    lib = ctypes.WinDLL(name=None, handle=handle)
    print(lib._handle)

    This runs to completion in Python 3.7 and earlier, but fails in Python 3.8 and later:

    Traceback (most recent call last):
      File "test.py", line 5, in <module>
        lib = ctypes.WinDLL(name=None, handle=handle)
      File "C:\Program Files (x86)\Python\38\lib\ctypes\__init__.py", line 359, in __init__
        if '/' in name or '\\' in name:
    TypeError: argument of type 'NoneType' is not iterable
  2. zooba commented on Jan 7, 2020

    @zooba
    Member

    Good catch.

    We should probably make the line 351 check "if name and ('/' in name ..." like the others in the same function.

  3. 8 remaining items

  4. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    and removed on Sep 4, 2025
  5. added a commit that references this issue on Sep 5, 2025
  6. added 2 commits that reference this issue on Sep 5, 2025
  7. added a commit that references this issue on Sep 9, 2025
  8. added 2 commits that reference this issue on Oct 8, 2025
  9. ZeroIntensity commented on Jul 18, 2026

    @ZeroIntensity
    Member

    I can no longer reproduce this on 3.14.

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

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesOS-windowseasytopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions