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
CDLL __init__ no longer supports name being passed as None when the handle is not None #83424
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=Noneclosed_at=Nonecreated_at=<Date2020-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=<Date2020-06-07.06:19:59.024>user='https://bugs.python.org/DavidHeffernan'
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.
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
Activity
DavidHeffernan commentedon Jan 7, 2020
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
to
The following program demonstrates the issue:
This runs to completion in Python 3.7 and earlier, but fails in Python 3.8 and later:
zooba commentedon Jan 7, 2020
Good catch.
We should probably make the line 351 check "if name and ('/' in name ..." like the others in the same function.
8 remaining items
gh-83424: Allow empty name if handle is non-null when create ctypes.C…
pythongh-83424: Allow empty name if handle is non-null when create ct…
pythongh-83424: Allow empty name if handle is non-null when create ct…
pythongh-83424: Allow empty name if handle is non-null when create ct…
[3.14] gh-83424: Allow empty name if handle is non-null when create c…
[3.13] gh-83424: Allow empty name if handle is non-null when create c…
ZeroIntensity commentedon Jul 18, 2026
I can no longer reproduce this on 3.14.