Author: Armin Rigo <[email protected]>
Branch: win32-stdcall
Changeset: r2318:3e58f08489fa
Date: 2015-10-06 12:22 +0200
http://bitbucket.org/cffi/cffi/changeset/3e58f08489fa/
Log: Fix remaining tests
diff --git a/testing/cffi0/test_verify.py b/testing/cffi0/test_verify.py
--- a/testing/cffi0/test_verify.py
+++ b/testing/cffi0/test_verify.py
@@ -2250,7 +2250,7 @@
""")
lib = ffi.verify(r"""
#ifndef _MSC_VER
- # define __stdcall FOOBARBAZZZZZ
+ # define __stdcall /* nothing */
#endif
int call1(int(*cb)(int)) {
int i, result = 0;
diff --git a/testing/cffi1/test_recompiler.py b/testing/cffi1/test_recompiler.py
--- a/testing/cffi1/test_recompiler.py
+++ b/testing/cffi1/test_recompiler.py
@@ -1281,24 +1281,79 @@
assert lib.aaa == 42
py.test.raises(AttributeError, "lib.aaa = 43")
-def test_win32_calling_convention_1():
- if sys.platform != 'win32':
- py.test.skip("Windows only")
+def test_win32_calling_convention_0():
ffi = FFI()
- ffi.cdef("int call1(int(*cb)(int)); int cb1(int);", calling_conv="cdecl")
- ffi.cdef("int call2(int(*cb)(int)); int cb2(int);", calling_conv="stdcall")
- lib = verify(ffi, 'test_win32_calling_convention_1', r"""
- int __cdecl cb1(int x) { return x * 2; }
- int __cdecl call1(int(__cdecl *cb)(int)) {
- printf("here1\n");
- printf("cb = %p, cb1 = %p\n", cb, (void *)cb1);
+ ffi.cdef("""
+ int call1(int(__cdecl *cb)(int));
+ int (*const call2)(int(__stdcall *cb)(int));
+ """)
+ lib = verify(ffi, 'test_win32_calling_convention_0', r"""
+ #ifndef _MSC_VER
+ # define __stdcall /* nothing */
+ #endif
+ int call1(int(*cb)(int)) {
int i, result = 0;
+ printf("call1: cb = %p\n", cb);
for (i = 0; i < 1000; i++)
result += cb(i);
printf("result = %d\n", result);
return result;
}
+ int call2(int(__stdcall *cb)(int)) {
+ int i, result = 0;
+ printf("call2: cb = %p\n", cb);
+ for (i = 0; i < 1000; i++)
+ result += cb(-i);
+ printf("result = %d\n", result);
+ return result;
+ }
+ """)
+ @ffi.callback("int(int)")
+ def cb1(x):
+ return x * 2
+ @ffi.callback("int __stdcall(int)")
+ def cb2(x):
+ return x * 3
+ res = lib.call1(cb1)
+ assert res == 500*999*2
+ assert res == ffi.addressof(lib, 'call1')(cb1)
+ res = lib.call2(cb2)
+ assert res == -500*999*3
+ assert res == ffi.addressof(lib, 'call2')(cb2)
+ if sys.platform == 'win32':
+ assert '__stdcall' in str(ffi.typeof(cb2))
+ assert '__stdcall' not in str(ffi.typeof(cb1))
+ py.test.raises(TypeError, lib.call1, cb2)
+ py.test.raises(TypeError, lib.call2, cb1)
+ else:
+ assert '__stdcall' not in str(ffi.typeof(cb2))
+ assert ffi.typeof(cb2) is ffi.typeof(cb1)
+
+def test_win32_calling_convention_1():
+ ffi = FFI()
+ ffi.cdef("""
+ int __cdecl call1(int(__cdecl *cb)(int));
+ int __stdcall call2(int(__stdcall *cb)(int));
+ int (__cdecl *const cb1)(int);
+ int (__stdcall *const cb2)(int);
+ """)
+ lib = verify(ffi, 'test_win32_calling_convention_1', r"""
+ #ifndef _MSC_VER
+ # define __cdecl
+ # define __stdcall
+ #endif
+ int __cdecl cb1(int x) { return x * 2; }
int __stdcall cb2(int x) { return x * 3; }
+
+ int __cdecl call1(int(__cdecl *cb)(int)) {
+ int i, result = 0;
+ printf("here1\n");
+ printf("cb = %p, cb1 = %p\n", cb, (void *)cb1);
+ for (i = 0; i < 1000; i++)
+ result += cb(i);
+ printf("result = %d\n", result);
+ return result;
+ }
int __stdcall call2(int(__stdcall *cb)(int)) {
int i, result = 0;
printf("here1\n");
@@ -1320,55 +1375,68 @@
print '<<< done'
def test_win32_calling_convention_2():
- if sys.platform != 'win32':
- py.test.skip("Windows only")
# any mistake in the declaration of plain function (including the
# precise argument types and, here, the calling convention) are
# automatically corrected. But this does not apply to the 'cb'
# function pointer argument.
ffi = FFI()
- ffi.cdef("int call1(int(*cb)(int)); int cb1(int);", calling_conv="cdecl")
- ffi.cdef("int call2(int(*cb)(int)); int cb2(int);", calling_conv="stdcall")
+ ffi.cdef("""
+ int __stdcall call1(int(__cdecl *cb)(int));
+ int __cdecl call2(int(__stdcall *cb)(int));
+ int (__cdecl *const cb1)(int);
+ int (__stdcall *const cb2)(int);
+ """)
lib = verify(ffi, 'test_win32_calling_convention_2', """
- int __stdcall call1(int(__cdecl *cb)(int)) {
+ #ifndef _MSC_VER
+ # define __cdecl
+ # define __stdcall
+ #endif
+ int __cdecl call1(int(__cdecl *cb)(int)) {
int i, result = 0;
for (i = 0; i < 1000; i++)
result += cb(i);
return result;
}
- int __cdecl call2(int(__stdcall *cb)(int)) {
+ int __stdcall call2(int(__stdcall *cb)(int)) {
int i, result = 0;
for (i = 0; i < 1000; i++)
result += cb(-i);
return result;
}
- int __stdcall cb1(int x) { return x * 2; }
- int __cdecl cb2(int x) { return x * 3; }
+ int __cdecl cb1(int x) { return x * 2; }
+ int __stdcall cb2(int x) { return x * 3; }
""")
ptr_call1 = ffi.addressof(lib, 'call1')
ptr_call2 = ffi.addressof(lib, 'call2')
- py.test.raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2'))
- py.test.raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2'))
- py.test.raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1'))
- py.test.raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1'))
+ if sys.platform == 'win32':
+ py.test.raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2'))
+ py.test.raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2'))
+ py.test.raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1'))
+ py.test.raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1'))
assert lib.call1(ffi.addressof(lib, 'cb1')) == 500*999*2
assert ptr_call1(ffi.addressof(lib, 'cb1')) == 500*999*2
assert lib.call2(ffi.addressof(lib, 'cb2')) == -500*999*3
assert ptr_call2(ffi.addressof(lib, 'cb2')) == -500*999*3
def test_win32_calling_convention_3():
- if sys.platform != 'win32':
- py.test.skip("Windows only")
ffi = FFI()
- ffi.cdef("struct point { int x, y; };")
- ffi.cdef("struct point call1(int(*cb)(struct point)); "
- "int cb1(struct point);", calling_conv="cdecl")
- ffi.cdef("struct point call2(int(*cb)(struct point)); "
- "int cb2(struct point);", calling_conv="stdcall")
+ ffi.cdef("""
+ struct point { int x, y; };
+
+ int (*const cb1)(struct point);
+ int (__stdcall *const cb2)(struct point);
+
+ struct point __stdcall call1(int(*cb)(struct point));
+ struct point call2(int(__stdcall *cb)(struct point));
+ """)
lib = verify(ffi, 'test_win32_calling_convention_3', r"""
+ #ifndef _MSC_VER
+ # define __cdecl
+ # define __stdcall
+ #endif
struct point { int x, y; };
- int __stdcall cb1(struct point pt) { return pt.x + 10 * pt.y; }
- int __cdecl cb2(struct point pt) { return pt.x + 100 * pt.y; }
+ int cb1(struct point pt) { return pt.x + 10 * pt.y; }
+ int __stdcall cb2(struct point pt) { return pt.x + 100 * pt.y; }
struct point __stdcall call1(int(__cdecl *cb)(struct point)) {
int i;
struct point result = { 0, 0 };
@@ -1396,11 +1464,11 @@
""")
ptr_call1 = ffi.addressof(lib, 'call1')
ptr_call2 = ffi.addressof(lib, 'call2')
- py.test.raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2'))
- py.test.raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2'))
- py.test.raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1'))
- py.test.raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1'))
- print '<<< cb1 =', ffi.addressof(lib, 'cb1')
+ if sys.platform == 'win32':
+ py.test.raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2'))
+ py.test.raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2'))
+ py.test.raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1'))
+ py.test.raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1'))
pt = lib.call1(ffi.addressof(lib, 'cb1'))
assert (pt.x, pt.y) == (-9*500*999, 9*500*999)
pt = ptr_call1(ffi.addressof(lib, 'cb1'))
@@ -1409,43 +1477,3 @@
assert (pt.x, pt.y) == (99*500*999, -99*500*999)
pt = ptr_call2(ffi.addressof(lib, 'cb2'))
assert (pt.x, pt.y) == (99*500*999, -99*500*999)
-
-def test_win32_calling_convention_4():
- if sys.platform != 'win32':
- py.test.skip("Windows only")
- ffi = FFI()
- ffi.cdef("int call1(int(*cb)(int));", calling_conv="cdecl")
- ffi.cdef("int call2(int(*cb)(int));", calling_conv="stdcall")
- lib = verify(ffi, 'test_win32_calling_convention_4', """
- int __stdcall call1(int(__cdecl *cb)(int)) {
- int i, result = 0;
- for (i = 0; i < 1000; i++)
- result += cb(i);
- return result;
- }
- int __cdecl call2(int(__stdcall *cb)(int)) {
- int i, result = 0;
- for (i = 0; i < 1000; i++)
- result += cb(-i);
- return result;
- }
- """)
- @ffi.callback("int(int)", calling_conv="cdecl")
- def cb1(x):
- return x * 2
- ...
- @ffi.callback("int(int)", calling_conv="stdcall")
- def cb2(x):
- return x * 2
-
-
- ptr_call1 = ffi.addressof(lib, 'call1')
- ptr_call2 = ffi.addressof(lib, 'call2')
- py.test.raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2'))
- py.test.raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2'))
- py.test.raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1'))
- py.test.raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1'))
- assert lib.call1(ffi.addressof(lib, 'cb1')) == 500*999*2
- assert ptr_call1(ffi.addressof(lib, 'cb1')) == 500*999*2
- assert lib.call2(ffi.addressof(lib, 'cb2')) == -500*999*3
- assert ptr_call2(ffi.addressof(lib, 'cb2')) == -500*999*3
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit