[New-bugs-announce] [issue39243] CDLL __init__ no longer supports name being passed as None when the handle is not None

David Heffernan report at bugs.python.org
Tue Jan 7 05:53:45 EST 2020


New submission from David Heffernan <david.heff at gmail.com>:

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 https://github.com/python/cpython/commit/2438cdf0e932a341c7613bf4323d06b91ae9f1f1 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

----------
components: Windows, ctypes
messages: 359501
nosy: David Heffernan, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: CDLL __init__ no longer supports name being passed as None when the handle is not None
type: behavior
versions: Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39243>
_______________________________________


More information about the New-bugs-announce mailing list