Commit 0e303178 authored by Meador Inge's avatar Meador Inge

Issue #13380: add an internal function for resetting the ctypes caches

parent a5d5bb99
...@@ -262,6 +262,22 @@ class c_bool(_SimpleCData): ...@@ -262,6 +262,22 @@ class c_bool(_SimpleCData):
from _ctypes import POINTER, pointer, _pointer_type_cache from _ctypes import POINTER, pointer, _pointer_type_cache
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)
try: try:
from _ctypes import set_conversion_mode from _ctypes import set_conversion_mode
except ImportError: except ImportError:
...@@ -278,8 +294,6 @@ else: ...@@ -278,8 +294,6 @@ else:
class c_wchar(_SimpleCData): class c_wchar(_SimpleCData):
_type_ = "u" _type_ = "u"
POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
def create_unicode_buffer(init, size=None): def create_unicode_buffer(init, size=None):
"""create_unicode_buffer(aString) -> character array """create_unicode_buffer(aString) -> character array
create_unicode_buffer(anInteger) -> character array create_unicode_buffer(anInteger) -> character array
...@@ -298,8 +312,6 @@ else: ...@@ -298,8 +312,6 @@ else:
return buf return buf
raise TypeError(init) raise TypeError(init)
POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param
# XXX Deprecated # XXX Deprecated
def SetPointerType(pointer, cls): def SetPointerType(pointer, cls):
if _pointer_type_cache.get(cls, None) is not None: if _pointer_type_cache.get(cls, None) is not None:
...@@ -458,8 +470,6 @@ if _os.name in ("nt", "ce"): ...@@ -458,8 +470,6 @@ if _os.name in ("nt", "ce"):
descr = FormatError(code).strip() descr = FormatError(code).strip()
return WindowsError(code, descr) return WindowsError(code, descr)
_pointer_type_cache[None] = c_void_p
if sizeof(c_uint) == sizeof(c_void_p): if sizeof(c_uint) == sizeof(c_void_p):
c_size_t = c_uint c_size_t = c_uint
c_ssize_t = c_int c_ssize_t = c_int
...@@ -542,8 +552,4 @@ for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]: ...@@ -542,8 +552,4 @@ for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
elif sizeof(kind) == 8: c_uint64 = kind elif sizeof(kind) == 8: c_uint64 = kind
del(kind) del(kind)
# XXX for whatever reasons, creating the first instance of a callback _reset_cache()
# 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)
...@@ -74,6 +74,7 @@ class BasicWrapTestCase(unittest.TestCase): ...@@ -74,6 +74,7 @@ class BasicWrapTestCase(unittest.TestCase):
def test_callbacks(self): def test_callbacks(self):
f = dll._testfunc_callback_i_if f = dll._testfunc_callback_i_if
f.restype = c_int f.restype = c_int
f.argtypes = None
MyCallback = CFUNCTYPE(c_int, c_int) MyCallback = CFUNCTYPE(c_int, c_int)
......
...@@ -250,6 +250,7 @@ class FunctionTestCase(unittest.TestCase): ...@@ -250,6 +250,7 @@ class FunctionTestCase(unittest.TestCase):
def test_callbacks(self): def test_callbacks(self):
f = dll._testfunc_callback_i_if f = dll._testfunc_callback_i_if
f.restype = c_int f.restype = c_int
f.argtypes = None
MyCallback = CFUNCTYPE(c_int, c_int) MyCallback = CFUNCTYPE(c_int, c_int)
......
...@@ -1076,6 +1076,13 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs): ...@@ -1076,6 +1076,13 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
filecmp._cache.clear() filecmp._cache.clear()
struct._clearcache() struct._clearcache()
doctest.master = None doctest.master = None
try:
import ctypes
except ImportError:
# Don't worry about resetting the cache if ctypes is not supported
pass
else:
ctypes._reset_cache()
# Collect cyclic trash. # Collect cyclic trash.
gc.collect() gc.collect()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment