|
|
2a6dbc |
diff --git a/Lib/ctypes/test/test_as_parameter.py b/Lib/ctypes/test/test_as_parameter.py
|
|
|
2a6dbc |
index 2a3484b..a264057 100644
|
|
|
2a6dbc |
--- a/Lib/ctypes/test/test_as_parameter.py
|
|
|
2a6dbc |
+++ b/Lib/ctypes/test/test_as_parameter.py
|
|
|
2a6dbc |
@@ -169,6 +169,10 @@ class BasicWrapTestCase(unittest.TestCase):
|
|
|
2a6dbc |
s2h = dll.ret_2h_func(self.wrap(inp))
|
|
|
2a6dbc |
self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
|
|
|
2a6dbc |
|
|
|
2a6dbc |
+ # Test also that the original struct was unmodified (i.e. was passed by
|
|
|
2a6dbc |
+ # value)
|
|
|
2a6dbc |
+ self.assertEqual((inp.x, inp.y), (99, 88))
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
def test_struct_return_8H(self):
|
|
|
2a6dbc |
class S8I(Structure):
|
|
|
2a6dbc |
_fields_ = [("a", c_int),
|
|
|
2a6dbc |
diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py
|
|
|
2a6dbc |
index 3eded77..3265996 100644
|
|
|
2a6dbc |
--- a/Lib/ctypes/test/test_structures.py
|
|
|
2a6dbc |
+++ b/Lib/ctypes/test/test_structures.py
|
|
|
2a6dbc |
@@ -414,6 +414,28 @@ class StructureTestCase(unittest.TestCase):
|
|
|
2a6dbc |
self.assertEqual(s.second, 0xcafebabe)
|
|
|
2a6dbc |
self.assertEqual(s.third, 0x0bad1dea)
|
|
|
2a6dbc |
|
|
|
2a6dbc |
+ def test_pass_by_value_in_register(self):
|
|
|
2a6dbc |
+ class X(Structure):
|
|
|
2a6dbc |
+ _fields_ = [
|
|
|
2a6dbc |
+ ('first', c_uint),
|
|
|
2a6dbc |
+ ('second', c_uint)
|
|
|
2a6dbc |
+ ]
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+ s = X()
|
|
|
2a6dbc |
+ s.first = 0xdeadbeef
|
|
|
2a6dbc |
+ s.second = 0xcafebabe
|
|
|
2a6dbc |
+ dll = CDLL(_ctypes_test.__file__)
|
|
|
2a6dbc |
+ func = dll._testfunc_reg_struct_update_value
|
|
|
2a6dbc |
+ func.argtypes = (X,)
|
|
|
2a6dbc |
+ func.restype = None
|
|
|
2a6dbc |
+ func(s)
|
|
|
2a6dbc |
+ self.assertEqual(s.first, 0xdeadbeef)
|
|
|
2a6dbc |
+ self.assertEqual(s.second, 0xcafebabe)
|
|
|
2a6dbc |
+ got = X.in_dll(dll, "last_tfrsuv_arg")
|
|
|
2a6dbc |
+ self.assertEqual(s.first, got.first)
|
|
|
2a6dbc |
+ self.assertEqual(s.second, got.second)
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
class PointerMemberTestCase(unittest.TestCase):
|
|
|
2a6dbc |
|
|
|
2a6dbc |
def test(self):
|
|
|
2a6dbc |
diff --git a/Modules/_ctypes/_ctypes_test.c b/Modules/_ctypes/_ctypes_test.c
|
|
|
2a6dbc |
index 59d56d0..1963f57 100644
|
|
|
2a6dbc |
--- a/Modules/_ctypes/_ctypes_test.c
|
|
|
2a6dbc |
+++ b/Modules/_ctypes/_ctypes_test.c
|
|
|
2a6dbc |
@@ -57,6 +57,24 @@ _testfunc_large_struct_update_value(Test in)
|
|
|
2a6dbc |
((volatile Test *)&in)->third = 0x0badf00d;
|
|
|
2a6dbc |
}
|
|
|
2a6dbc |
|
|
|
2a6dbc |
+typedef struct {
|
|
|
2a6dbc |
+ unsigned int first;
|
|
|
2a6dbc |
+ unsigned int second;
|
|
|
2a6dbc |
+} TestReg;
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+EXPORT(TestReg) last_tfrsuv_arg;
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+EXPORT(void)
|
|
|
2a6dbc |
+_testfunc_reg_struct_update_value(TestReg in)
|
|
|
2a6dbc |
+{
|
|
|
2a6dbc |
+ last_tfrsuv_arg = in;
|
|
|
2a6dbc |
+ ((volatile TestReg *)&in)->first = 0x0badf00d;
|
|
|
2a6dbc |
+ ((volatile TestReg *)&in)->second = 0x0badf00d;
|
|
|
2a6dbc |
+}
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
EXPORT(void)testfunc_array(int values[4])
|
|
|
2a6dbc |
{
|
|
|
2a6dbc |
printf("testfunc_array %d %d %d %d\n",
|
|
|
2a6dbc |
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
|
|
|
2a6dbc |
index 7d542fb..bc41433 100644
|
|
|
2a6dbc |
--- a/Modules/_ctypes/callproc.c
|
|
|
2a6dbc |
+++ b/Modules/_ctypes/callproc.c
|
|
|
2a6dbc |
@@ -1040,6 +1040,13 @@ GetComError(HRESULT errcode, GUID *riid, IUnknown *pIunk)
|
|
|
2a6dbc |
}
|
|
|
2a6dbc |
#endif
|
|
|
2a6dbc |
|
|
|
2a6dbc |
+#if (defined(__x86_64__) && (defined(__MINGW64__) || defined(__CYGWIN__))) || \
|
|
|
2a6dbc |
+ defined(__aarch64__)
|
|
|
2a6dbc |
+#define CTYPES_PASS_BY_REF_HACK
|
|
|
2a6dbc |
+#define POW2(x) (((x & ~(x - 1)) == x) ? x : 0)
|
|
|
2a6dbc |
+#define IS_PASS_BY_REF(x) (x > 8 || !POW2(x))
|
|
|
2a6dbc |
+#endif
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
/*
|
|
|
2a6dbc |
* Requirements, must be ensured by the caller:
|
|
|
2a6dbc |
* - argtuple is tuple of arguments
|
|
|
2a6dbc |
@@ -1137,8 +1144,20 @@ PyObject *_ctypes_callproc(PPROC pProc,
|
|
|
2a6dbc |
}
|
|
|
2a6dbc |
for (i = 0; i < argcount; ++i) {
|
|
|
2a6dbc |
atypes[i] = args[i].ffi_type;
|
|
|
2a6dbc |
- if (atypes[i]->type == FFI_TYPE_STRUCT
|
|
|
2a6dbc |
- )
|
|
|
2a6dbc |
+#ifdef CTYPES_PASS_BY_REF_HACK
|
|
|
2a6dbc |
+ size_t size = atypes[i]->size;
|
|
|
2a6dbc |
+ if (IS_PASS_BY_REF(size)) {
|
|
|
2a6dbc |
+ void *tmp = alloca(size);
|
|
|
2a6dbc |
+ if (atypes[i]->type == FFI_TYPE_STRUCT)
|
|
|
2a6dbc |
+ memcpy(tmp, args[i].value.p, size);
|
|
|
2a6dbc |
+ else
|
|
|
2a6dbc |
+ memcpy(tmp, (void*)&args[i].value, size);
|
|
|
2a6dbc |
+
|
|
|
2a6dbc |
+ avalues[i] = tmp;
|
|
|
2a6dbc |
+ }
|
|
|
2a6dbc |
+ else
|
|
|
2a6dbc |
+#endif
|
|
|
2a6dbc |
+ if (atypes[i]->type == FFI_TYPE_STRUCT)
|
|
|
2a6dbc |
avalues[i] = (void *)args[i].value.p;
|
|
|
2a6dbc |
else
|
|
|
2a6dbc |
avalues[i] = (void *)&args[i].value;
|