summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2017-08-28 12:08:49 (GMT)
committerAntoine Pitrou <pitrou@free.fr>2017-08-28 12:08:49 (GMT)
commit07f1658aa09f6798793c473c72b2951b7fefe220 (patch)
tree1a3c4c0603a5e3fac9b8d2ec1a96a9445291d392 /Modules
parenta30f6d45ac3e72761b96a8df0527182029eaee24 (diff)
downloadcpython-07f1658aa09f6798793c473c72b2951b7fefe220.zip
cpython-07f1658aa09f6798793c473c72b2951b7fefe220.tar.gz
cpython-07f1658aa09f6798793c473c72b2951b7fefe220.tar.bz2
bpo-10746: Fix ctypes PEP 3118 type codes for c_long, c_bool, c_int (#31)
Ctypes currently produces wrong pep3118 type codes for several types. E.g. memoryview(ctypes.c_long()).format gives "<l" on 64-bit platforms, but it should be "<q" instead for sizeof(c_long) == 8 The problem is that the '<>' endian specification in the struct syntax also turns on the "standard size" mode, which makes type characters have a platform-independent meaning, which does not match with the codes used internally in ctypes. The struct module format syntax also does not allow specifying native-size non-native-endian items. This commit adds a converter function that maps the internal ctypes codes to appropriate struct module standard-size codes in the pep3118 format strings. The tests are modified to check for this.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_ctypes/_ctypes.c69
1 files changed, 67 insertions, 2 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 0bcfc13..0255f76 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -249,6 +249,71 @@ PyDict_GetItemProxy(PyObject *dict, PyObject *key)
}
/******************************************************************/
+
+/*
+ Allocate a memory block for a pep3118 format string, filled with
+ a suitable PEP 3118 type code corresponding to the given ctypes
+ type. Returns NULL on failure, with the error indicator set.
+
+ This produces type codes in the standard size mode (cf. struct module),
+ since the endianness may need to be swapped to a non-native one
+ later on.
+ */
+static char *
+_ctypes_alloc_format_string_for_type(char code, int big_endian)
+{
+ char *result;
+ char pep_code = '\0';
+
+ switch (code) {
+#if SIZEOF_INT == 2
+ case 'i': pep_code = 'h'; break;
+ case 'I': pep_code = 'H'; break;
+#elif SIZEOF_INT == 4
+ case 'i': pep_code = 'i'; break;
+ case 'I': pep_code = 'I'; break;
+#elif SIZEOF_INT == 8
+ case 'i': pep_code = 'q'; break;
+ case 'I': pep_code = 'Q'; break;
+#else
+# error SIZEOF_INT has an unexpected value
+#endif /* SIZEOF_INT */
+#if SIZEOF_LONG == 4
+ case 'l': pep_code = 'l'; break;
+ case 'L': pep_code = 'L'; break;
+#elif SIZEOF_LONG == 8
+ case 'l': pep_code = 'q'; break;
+ case 'L': pep_code = 'Q'; break;
+#else
+# error SIZEOF_LONG has an unexpected value
+#endif /* SIZEOF_LONG */
+#if SIZEOF__BOOL == 1
+ case '?': pep_code = '?'; break;
+#elif SIZEOF__BOOL == 2
+ case '?': pep_code = 'H'; break;
+#elif SIZEOF__BOOL == 4
+ case '?': pep_code = 'L'; break;
+#elif SIZEOF__BOOL == 8
+ case '?': pep_code = 'Q'; break;
+#else
+# error SIZEOF__BOOL has an unexpected value
+#endif /* SIZEOF__BOOL */
+ default:
+ /* The standard-size code is the same as the ctypes one */
+ pep_code = code;
+ break;
+ }
+
+ result = PyMem_Malloc(3);
+ if (result == NULL)
+ return NULL;
+
+ result[0] = big_endian ? '>' : '<';
+ result[1] = pep_code;
+ result[2] = '\0';
+ return result;
+}
+
/*
Allocate a memory block for a pep3118 format string, copy prefix (if
non-null) and suffix into it. Returns NULL on failure, with the error
@@ -1930,9 +1995,9 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
stgdict->setfunc = fmt->setfunc;
stgdict->getfunc = fmt->getfunc;
#ifdef WORDS_BIGENDIAN
- stgdict->format = _ctypes_alloc_format_string(">", proto_str);
+ stgdict->format = _ctypes_alloc_format_string_for_type(proto_str[0], 1);
#else
- stgdict->format = _ctypes_alloc_format_string("<", proto_str);
+ stgdict->format = _ctypes_alloc_format_string_for_type(proto_str[0], 0);
#endif
if (stgdict->format == NULL) {
Py_DECREF(result);