diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -265,7 +265,21 @@ class c_wchar_p(_SimpleCData): class c_wchar(_SimpleCData): _type_ = "u" -POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param +def _reset_cache(): + _pointer_type_cache.clear() + _c_functype_cache.clear() + if _os.name in ("nt", "ce"): + _win_functype_cache.clear() + # _SimpleCData.c_wchar_p_from_param + POINTER(c_wchar).from_param = c_wchar_p.from_param + # _SimpleCData.c_char_p_from_param + POINTER(c_char).from_param = c_char_p.from_param + _pointer_type_cache[None] = c_void_p + # XXX for whatever reasons, creating the first instance of a callback + # function is needed for the unittests on Win64 to succeed. This MAY + # be a compiler bug, since the problem occurs only when _ctypes is + # compiled with the MS SDK compiler. Or an uninitialized variable? + CFUNCTYPE(c_int)(lambda: None) def create_unicode_buffer(init, size=None): """create_unicode_buffer(aString) -> character array @@ -285,7 +299,6 @@ def create_unicode_buffer(init, size=Non return buf raise TypeError(init) -POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param # XXX Deprecated def SetPointerType(pointer, cls): @@ -445,8 +458,6 @@ if _os.name in ("nt", "ce"): descr = FormatError(code).strip() return WindowsError(code, descr) -_pointer_type_cache[None] = c_void_p - if sizeof(c_uint) == sizeof(c_void_p): c_size_t = c_uint c_ssize_t = c_int @@ -534,3 +545,5 @@ del(kind) # be a compiler bug, since the problem occurs only when _ctypes is # compiled with the MS SDK compiler. Or an uninitialized variable? CFUNCTYPE(c_int)(lambda: None) + +_reset_cache() diff --git a/Lib/ctypes/test/test_as_parameter.py b/Lib/ctypes/test/test_as_parameter.py --- a/Lib/ctypes/test/test_as_parameter.py +++ b/Lib/ctypes/test/test_as_parameter.py @@ -74,6 +74,7 @@ class BasicWrapTestCase(unittest.TestCas def test_callbacks(self): f = dll._testfunc_callback_i_if f.restype = c_int + f.argtypes = None MyCallback = CFUNCTYPE(c_int, c_int) diff --git a/Lib/ctypes/test/test_functions.py b/Lib/ctypes/test/test_functions.py --- a/Lib/ctypes/test/test_functions.py +++ b/Lib/ctypes/test/test_functions.py @@ -250,6 +250,7 @@ class FunctionTestCase(unittest.TestCase def test_callbacks(self): f = dll._testfunc_callback_i_if f.restype = c_int + f.argtypes = None MyCallback = CFUNCTYPE(c_int, c_int) diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -1350,6 +1350,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abc import _strptime, linecache import urllib.parse, urllib.request, mimetypes, doctest import struct, filecmp, collections.abc + import ctypes from distutils.dir_util import _path_created from weakref import WeakSet @@ -1401,6 +1402,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abc filecmp._cache.clear() struct._clearcache() doctest.master = None + ctypes._reset_cache() # Collect cyclic trash. gc.collect()