]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 55129-55131 via svnmerge from
authorThomas Heller <theller@ctypes.org>
Fri, 8 Jun 2007 19:39:31 +0000 (19:39 +0000)
committerThomas Heller <theller@ctypes.org>
Fri, 8 Jun 2007 19:39:31 +0000 (19:39 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk/Lib/ctypes

........
  r55129 | thomas.heller | 2007-05-04 21:54:22 +0200 (Fr, 04 Mai 2007) | 3 lines

  Do not truncate 64-bit pointers to 32-bit integers.

  Fixes SF #1703286, will backport to release25-maint.
........
  r55131 | thomas.heller | 2007-05-04 21:56:32 +0200 (Fr, 04 Mai 2007) | 1 line

  Oops, these tests do not run on Windows CE.
........

Lib/ctypes/test/test_loading.py
Misc/NEWS
Modules/_ctypes/callproc.c
Modules/_ctypes/ctypes.h

index 1e7870b79fd1e327662e37ef2d6e7f939cf8630f..18dcdbfc7d70a00644f2a9a96d252ddefcfbfe84 100644 (file)
@@ -74,5 +74,33 @@ class LoaderTest(unittest.TestCase):
 
             self.failUnlessRaises(AttributeError, dll.__getitem__, 1234)
 
+    if os.name == "nt":
+        def test_1703286_A(self):
+            from _ctypes import LoadLibrary, FreeLibrary
+            # On winXP 64-bit, advapi32 loads at an address that does
+            # NOT fit into a 32-bit integer.  FreeLibrary must be able
+            # to accept this address.
+
+            # These are tests for http://www.python.org/sf/1703286
+            handle = LoadLibrary("advapi32")
+            FreeLibrary(handle)
+
+        def test_1703286_B(self):
+            # Since on winXP 64-bit advapi32 loads like described
+            # above, the (arbitrarily selected) CloseEventLog function
+            # also has a high address.  'call_function' should accept
+            # addresses so large.
+            from _ctypes import call_function
+            advapi32 = windll.advapi32
+            # Calling CloseEventLog with a NULL argument should fail,
+            # but the call should not segfault or so.
+            self.failUnlessEqual(0, advapi32.CloseEventLog(None))
+            windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
+            windll.kernel32.GetProcAddress.restype = c_void_p
+            proc = windll.kernel32.GetProcAddress(advapi32._handle, "CloseEventLog")
+            self.failUnless(proc)
+            # This is the real test: call the function via 'call_function'
+            self.failUnlessEqual(0, call_function(proc, (None,)))
+
 if __name__ == "__main__":
     unittest.main()
index 136ab5c3dac8ca2fe85e280fc3dd343b627837c3..4f9d64875090a24c3a741afa400eaa620b01b5dd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,8 @@ Library
 Extension Modules
 -----------------
 
+- Bug #1703286: ctypes no longer truncates 64-bit pointers.
+
 - Bug #1721309: prevent bsddb module from freeing random memory.
 
 - Bug #1726026: Correct the field names of WIN32_FIND_DATAA and
index 0c35f383861af1acd36e1b7febb717745140e83d..58aa9bb623031f80231bf83a5c75e8e982a403b3 100644 (file)
@@ -1111,10 +1111,10 @@ static char free_library_doc[] =
 Free the handle of an executable previously loaded by LoadLibrary.\n";
 static PyObject *free_library(PyObject *self, PyObject *args)
 {
-       HMODULE hMod;
-       if (!PyArg_ParseTuple(args, "i:FreeLibrary", &hMod))
+       void *hMod;
+       if (!PyArg_ParseTuple(args, PY_VOID_P_CODE ":FreeLibrary", &hMod))
                return NULL;
-       if (!FreeLibrary(hMod))
+       if (!FreeLibrary((HMODULE)hMod))
                return PyErr_SetFromWindowsErr(GetLastError());
        Py_INCREF(Py_None);
        return Py_None;
@@ -1233,9 +1233,9 @@ static PyObject *py_dl_open(PyObject *self, PyObject *args)
 
 static PyObject *py_dl_close(PyObject *self, PyObject *args)
 {
-       void * handle;
+       void *handle;
 
-       if (!PyArg_ParseTuple(args, "i:dlclose", &handle))
+       if (!PyArg_ParseTuple(args, PY_VOID_P_CODE ":dlclose", &handle))
                return NULL;
        if (dlclose(handle)) {
                PyErr_SetString(PyExc_OSError,
@@ -1252,7 +1252,7 @@ static PyObject *py_dl_sym(PyObject *self, PyObject *args)
        void *handle;
        void *ptr;
 
-       if (!PyArg_ParseTuple(args, "is:dlsym", &handle, &name))
+       if (!PyArg_ParseTuple(args, PY_VOID_P_CODE "s:dlsym", &handle, &name))
                return NULL;
        ptr = ctypes_dlsym(handle, name);
        if (!ptr) {
@@ -1260,7 +1260,7 @@ static PyObject *py_dl_sym(PyObject *self, PyObject *args)
                                       ctypes_dlerror());
                return NULL;
        }
-       return Py_BuildValue("i", ptr);
+       return PyLong_FromVoidPtr(ptr);
 }
 #endif
 
@@ -1272,12 +1272,12 @@ static PyObject *py_dl_sym(PyObject *self, PyObject *args)
 static PyObject *
 call_function(PyObject *self, PyObject *args)
 {
-       PPROC func;
+       void *func;
        PyObject *arguments;
        PyObject *result;
 
        if (!PyArg_ParseTuple(args,
-                             "iO!",
+                             PY_VOID_P_CODE "O!",
                              &func,
                              &PyTuple_Type, &arguments))
                return NULL;
@@ -1303,12 +1303,12 @@ call_function(PyObject *self, PyObject *args)
 static PyObject *
 call_cdeclfunction(PyObject *self, PyObject *args)
 {
-       PPROC func;
+       void *func;
        PyObject *arguments;
        PyObject *result;
 
        if (!PyArg_ParseTuple(args,
-                             "iO!",
+                             PY_VOID_P_CODE "O!",
                              &func,
                              &PyTuple_Type, &arguments))
                return NULL;
index 0af7851020832bcbfe3c5e440b331be6535a8195..5fb603075d3363606872c1088e79c8469d880b97 100644 (file)
@@ -23,6 +23,12 @@ typedef int Py_ssize_t;
 #define PY_LONG_LONG LONG_LONG
 #endif
 
+#if SIZEOF_VOID_P == SIZEOF_LONG
+#define PY_VOID_P_CODE "k"
+#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P == SIZEOF_LONG_LONG)
+#define PY_VOID_P_CODE "K"
+#endif
+
 typedef struct tagPyCArgObject PyCArgObject;
 typedef struct tagCDataObject CDataObject;
 typedef PyObject *(* GETFUNC)(void *, unsigned size);