New submission from Meador Inge <mead...@gmail.com>:

Currently it is possible to somewhat easily get false positives for reference 
leaks when running the ctypes regression tests with -R.  See issue13250 for an 
example where I got tripped up.  The reason is that the ctypes caches are not 
cleared in between test runs like they are with some of the other modules that 
cache objects.

The attached patch adds an internal function for reseting the ctypes caches.  
regrtest.py is then fixed up to use it.  Finally, two tests
that previously relied on the caching being present to work in back-to-back 
runs have been fixed.  In particular, the tests were written to do something 
like:

# Global
dll = CDLL(_ctypes_test.__file__)

# Local to test
f = dll._testfunc_callback_i_if
f.restype = c_int

MyCallback = CFUNCTYPE(c_int, c_int)

def callback(value)
    return value

cb = MyCallback(callback)
result = f(-10, cb)

f.argtypes = [c_int, MyCallback]
cb = MyCallback(callback)
result = f(-10, cb)

Thus when run in back-to-back runs where caching is cleared in between you 
effectively get:

# Global
dll = CDLL(_ctypes_test.__file__)

# Local to test
f = dll._testfunc_callback_i_if
f.restype = c_int

MyCallback = CFUNCTYPE(c_int, c_int)

def callback(value):
    return value

cb = MyCallback(callback)
result = f(-10, cb)

f.argtypes = [c_int, MyCallback]
cb = MyCallback(callback)
result = f(-10, cb)

_reset_cache()

f = dll._testfunc_callback_i_if
f.restype = c_int

MyCallback = CFUNCTYPE(c_int, c_int)

cb = MyCallback(callback)
result = f(-10, cb)

which causes:

types.ArgumentError: argument 2: <class 'TypeError'>: expected CFunctionType 
instance instead of CFunctionType

because the final MyCallback instance passed to f is not of the same type as 
the MyCallback type saved in f.argtypes.  The fix is to set f.argtypes to None 
at the beginning of each test.

I would also like to commit this to 2.7 and 3.2.  It will make fixing true 
reference leaks in those branches easier.

OK?

----------
assignee: meador.inge
components: ctypes
files: ctypes-reset-cache.patch
keywords: patch
messages: 147394
nosy: amaury.forgeotdarc, belopolsky, meador.inge
priority: normal
severity: normal
stage: patch review
status: open
title: ctypes: add an internal function for reseting the ctypes caches
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3
Added file: http://bugs.python.org/file23651/ctypes-reset-cache.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13380>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to