Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11e3813

Browse files
committed
Issue #13380: add an internal function for resetting the ctypes caches
1 parent 4558bad commit 11e3813

4 files changed

Lines changed: 25 additions & 9 deletions

File tree

Lib/ctypes/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,21 @@ class c_wchar_p(_SimpleCData):
265265
class c_wchar(_SimpleCData):
266266
_type_ = "u"
267267

268-
POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
268+
def _reset_cache():
269+
_pointer_type_cache.clear()
270+
_c_functype_cache.clear()
271+
if _os.name in ("nt", "ce"):
272+
_win_functype_cache.clear()
273+
# _SimpleCData.c_wchar_p_from_param
274+
POINTER(c_wchar).from_param = c_wchar_p.from_param
275+
# _SimpleCData.c_char_p_from_param
276+
POINTER(c_char).from_param = c_char_p.from_param
277+
_pointer_type_cache[None] = c_void_p
278+
# XXX for whatever reasons, creating the first instance of a callback
279+
# function is needed for the unittests on Win64 to succeed. This MAY
280+
# be a compiler bug, since the problem occurs only when _ctypes is
281+
# compiled with the MS SDK compiler. Or an uninitialized variable?
282+
CFUNCTYPE(c_int)(lambda: None)
269283

270284
def create_unicode_buffer(init, size=None):
271285
"""create_unicode_buffer(aString) -> character array
@@ -285,7 +299,6 @@ def create_unicode_buffer(init, size=None):
285299
return buf
286300
raise TypeError(init)
287301

288-
POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param
289302

290303
# XXX Deprecated
291304
def SetPointerType(pointer, cls):
@@ -445,8 +458,6 @@ def WinError(code=None, descr=None):
445458
descr = FormatError(code).strip()
446459
return WindowsError(code, descr)
447460

448-
_pointer_type_cache[None] = c_void_p
449-
450461
if sizeof(c_uint) == sizeof(c_void_p):
451462
c_size_t = c_uint
452463
c_ssize_t = c_int
@@ -529,8 +540,4 @@ def DllCanUnloadNow():
529540
elif sizeof(kind) == 8: c_uint64 = kind
530541
del(kind)
531542

532-
# XXX for whatever reasons, creating the first instance of a callback
533-
# function is needed for the unittests on Win64 to succeed. This MAY
534-
# be a compiler bug, since the problem occurs only when _ctypes is
535-
# compiled with the MS SDK compiler. Or an uninitialized variable?
536-
CFUNCTYPE(c_int)(lambda: None)
543+
_reset_cache()

Lib/ctypes/test/test_as_parameter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def callback(v):
7474
def test_callbacks(self):
7575
f = dll._testfunc_callback_i_if
7676
f.restype = c_int
77+
f.argtypes = None
7778

7879
MyCallback = CFUNCTYPE(c_int, c_int)
7980

Lib/ctypes/test/test_functions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ def callback(v):
250250
def test_callbacks(self):
251251
f = dll._testfunc_callback_i_if
252252
f.restype = c_int
253+
f.argtypes = None
253254

254255
MyCallback = CFUNCTYPE(c_int, c_int)
255256

Lib/test/regrtest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,13 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
12551255
filecmp._cache.clear()
12561256
struct._clearcache()
12571257
doctest.master = None
1258+
try:
1259+
import ctypes
1260+
except ImportError:
1261+
# Don't worry about resetting the cache if ctypes is not supported
1262+
pass
1263+
else:
1264+
ctypes._reset_cache()
12581265

12591266
# Collect cyclic trash.
12601267
gc.collect()

0 commit comments

Comments
 (0)