This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author David Heffernan
Recipients David Heffernan, paul.moore, steve.dower, tim.golden, zach.ware
Date 2020-01-07.10:53:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1578394425.89.0.0381750651285.issue39243@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2020-01-07 10:53:45David Heffernansetrecipients: + David Heffernan, paul.moore, tim.golden, zach.ware, steve.dower
2020-01-07 10:53:45David Heffernansetmessageid: <1578394425.89.0.0381750651285.issue39243@roundup.psfhosted.org>
2020-01-07 10:53:45David Heffernanlinkissue39243 messages
2020-01-07 10:53:45David Heffernancreate