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

Skip to content

Commit 0ea622a

Browse files
committed
Issue #8275: Fix passing of callback arguments with ctypes under Win64.
Patch by Stan Mihai. Ok'ed by Georg.
1 parent bd0c897 commit 0ea622a

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lib/ctypes/test/test_callbacks.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,42 @@ def EnumWindowsCallbackFunc(hwnd, lParam):
200200

201201
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
202202

203+
def test_callback_register_int(self):
204+
# Issue #8275: buggy handling of callback args under Win64
205+
# NOTE: should be run on release builds as well
206+
dll = CDLL(_ctypes_test.__file__)
207+
CALLBACK = CFUNCTYPE(c_int, c_int, c_int, c_int, c_int, c_int)
208+
# All this function does is call the callback with its args squared
209+
func = dll._testfunc_cbk_reg_int
210+
func.argtypes = (c_int, c_int, c_int, c_int, c_int, CALLBACK)
211+
func.restype = c_int
212+
213+
def callback(a, b, c, d, e):
214+
return a + b + c + d + e
215+
216+
result = func(2, 3, 4, 5, 6, CALLBACK(callback))
217+
self.assertEqual(result, callback(2*2, 3*3, 4*4, 5*5, 6*6))
218+
219+
def test_callback_register_double(self):
220+
# Issue #8275: buggy handling of callback args under Win64
221+
# NOTE: should be run on release builds as well
222+
dll = CDLL(_ctypes_test.__file__)
223+
CALLBACK = CFUNCTYPE(c_double, c_double, c_double, c_double,
224+
c_double, c_double)
225+
# All this function does is call the callback with its args squared
226+
func = dll._testfunc_cbk_reg_double
227+
func.argtypes = (c_double, c_double, c_double,
228+
c_double, c_double, CALLBACK)
229+
func.restype = c_double
230+
231+
def callback(a, b, c, d, e):
232+
return a + b + c + d + e
233+
234+
result = func(1.1, 2.2, 3.3, 4.4, 5.5, CALLBACK(callback))
235+
self.assertEqual(result,
236+
callback(1.1*1.1, 2.2*2.2, 3.3*3.3, 4.4*4.4, 5.5*5.5))
237+
238+
203239
################################################################
204240

205241
if __name__ == '__main__':

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ Luke Mewburn
574574
Mike Meyer
575575
Steven Miale
576576
Trent Mick
577+
Stan Mihai
577578
Aristotelis Mikropoulos
578579
Damien Miller
579580
Chad Miller

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #8275: Fix passing of callback arguments with ctypes under Win64.
17+
Patch by Stan Mihai.
18+
1619

1720
What's New in Python 3.2 Release Candidate 2?
1821
=============================================

Modules/_ctypes/_ctypes_test.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212

1313
/* some functions handy for testing */
1414

15+
EXPORT(int)
16+
_testfunc_cbk_reg_int(int a, int b, int c, int d, int e,
17+
int (*func)(int, int, int, int, int))
18+
{
19+
return func(a*a, b*b, c*c, d*d, e*e);
20+
}
21+
22+
EXPORT(double)
23+
_testfunc_cbk_reg_double(double a, double b, double c, double d, double e,
24+
double (*func)(double, double, double, double, double))
25+
{
26+
return func(a*a, b*b, c*c, d*d, e*e);
27+
}
28+
1529
EXPORT(void)testfunc_array(int values[4])
1630
{
1731
printf("testfunc_array %d %d %d %d\n",

Modules/_ctypes/libffi_msvc/ffi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ ffi_prep_closure_loc (ffi_closure* closure,
380380
short bytes;
381381
char *tramp;
382382
#ifdef _WIN64
383-
int mask;
383+
int mask = 0;
384384
#endif
385385
FFI_ASSERT (cif->abi == FFI_SYSV);
386386

0 commit comments

Comments
 (0)