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.

classification
Title: ctypes: add an internal function for reseting the ctypes caches
Type: behavior Stage: resolved
Components: ctypes Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: meador.inge Nosy List: amaury.forgeotdarc, belopolsky, meador.inge, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2011-11-10 05:22 by meador.inge, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ctypes-reset-cache.patch meador.inge, 2011-11-10 05:22 review
ctypes-reset-cache-2.patch meador.inge, 2011-11-15 03:20 review
Messages (6)
msg147394 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-11-10 05:22
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?
msg147534 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-11-12 23:48
Two things:
- you duplicated the part with "CFUNCTYPE(c_int)(lambda: None)" without removing the original chunk of code
- some platforms can't compile ctypes, you must handle that case in regrtest

Otherwise, good idea.
msg147652 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-11-15 03:20
> - you duplicated the part with "CFUNCTYPE(c_int)(lambda: None)" without 
> removing the original chunk of code
> - some platforms can't compile ctypes, you must handle that case in 
> regrtest

Both fixed.  Thanks for the review.  Second patch attached.
msg147695 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-11-15 18:26
The patch looks good to me.
msg148376 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-11-26 04:38
New changeset 6783aa5c15ae by Meador Inge in branch '2.7':
Issue #13380: add an internal function for resetting the ctypes caches
http://hg.python.org/cpython/rev/6783aa5c15ae

New changeset fa59b3758b14 by Meador Inge in branch '3.2':
Issue #13380: add an internal function for resetting the ctypes caches
http://hg.python.org/cpython/rev/fa59b3758b14

New changeset 963d861d0eb5 by Meador Inge in branch 'default':
Issue #13380: add an internal function for resetting the ctypes caches
http://hg.python.org/cpython/rev/963d861d0eb5
msg148377 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-11-26 04:40
Committed.  Thanks for the review Antoine.
History
Date User Action Args
2022-04-11 14:57:23adminsetgithub: 57589
2011-11-26 04:40:33meador.ingesetstatus: open -> closed
resolution: fixed
messages: + msg148377

stage: patch review -> resolved
2011-11-26 04:38:35python-devsetnosy: + python-dev
messages: + msg148376
2011-11-15 18:26:03pitrousetmessages: + msg147695
2011-11-15 03:20:08meador.ingesetfiles: + ctypes-reset-cache-2.patch

messages: + msg147652
2011-11-12 23:48:34pitrousetnosy: + pitrou
messages: + msg147534
2011-11-10 05:22:40meador.ingecreate