From: Thomas Heller Date: Thu, 7 Sep 2006 19:09:54 +0000 (+0000) Subject: The cast function did not accept c_char_p or c_wchar_p instances X-Git-Tag: v2.6a1~2680 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2244af596a56baaab3b85028189434f0c8a1d1e8;p=python The cast function did not accept c_char_p or c_wchar_p instances as first argument, and failed with a 'bad argument to internal function' error message. --- diff --git a/Lib/ctypes/test/test_cast.py b/Lib/ctypes/test/test_cast.py index 09e928fdb4..7371b0fe24 100644 --- a/Lib/ctypes/test/test_cast.py +++ b/Lib/ctypes/test/test_cast.py @@ -57,5 +57,21 @@ class Test(unittest.TestCase): c_int() self.failUnlessEqual(p[:4], [1, 2, 96, 4]) + def test_char_p(self): + # This didn't work: bad argument to internal function + s = c_char_p("hiho") + self.failUnlessEqual(cast(cast(s, c_void_p), c_char_p).value, + "hiho") + + try: + c_wchar_p + except NameError: + pass + else: + def test_wchar_p(self): + s = c_wchar_p("hiho") + self.failUnlessEqual(cast(cast(s, c_void_p), c_wchar_p).value, + "hiho") + if __name__ == "__main__": unittest.main() diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 6234c2b2e5..8889038d86 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4597,11 +4597,11 @@ cast(void *ptr, PyObject *src, PyObject *ctype) if (obj->b_objects == NULL) goto failed; } + Py_XINCREF(obj->b_objects); result->b_objects = obj->b_objects; - if (result->b_objects) { + if (result->b_objects && PyDict_Check(result->b_objects)) { PyObject *index; int rc; - Py_INCREF(obj->b_objects); index = PyLong_FromVoidPtr((void *)src); if (index == NULL) goto failed;