diff options
author | Thomas Heller <theller@ctypes.org> | 2008-04-30 17:11:46 +0000 |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2008-04-30 17:11:46 +0000 |
commit | b041fdaf941e84513c643ac02febfb64a4c77435 (patch) | |
tree | 4ad2dfb0b140ac234e54c0e8634f097e588ca399 /Modules/_ctypes/stgdict.c | |
parent | 6471f9633bef73e6727bde467bbcd59d0fea4bf1 (diff) | |
download | cpython-git-b041fdaf941e84513c643ac02febfb64a4c77435.tar.gz |
Merged revisions 60056-60071,60073-60127,60129-60261,60263-60284,60286-62589,62591-62594 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k-ctypes-pep3118
........
r60059 | thomas.heller | 2008-01-18 22:17:05 +0100 (Fri, 18 Jan 2008) | 1 line
Implement pep3118 format strings for SimpleCData types.
........
r60108 | thomas.heller | 2008-01-19 22:56:12 +0100 (Sat, 19 Jan 2008) | 3 lines
Always use explicit endian specifiers for simple types, and a bug fix
too. Add unittest.
........
r60112 | thomas.heller | 2008-01-19 23:25:14 +0100 (Sat, 19 Jan 2008) | 2 lines
Fully implement tp_asbuffer for pointer types.
........
r60261 | thomas.heller | 2008-01-24 22:01:29 +0100 (Thu, 24 Jan 2008) | 4 lines
Added shape and ndim field to StgDictObject. Implemented pep3118
format string, ndim, and shape for array types. Added a
buffer_info(type_or_object) for testing.
........
r60278 | thomas.heller | 2008-01-25 11:53:33 +0100 (Fri, 25 Jan 2008) | 2 lines
Implement pep3118 format strings for ctypes.Structure and ctypes.Union.
........
r60288 | thomas.heller | 2008-01-25 17:58:30 +0100 (Fri, 25 Jan 2008) | 2 lines
All ctypes types now use the same CData_GetBuffer function.
........
r60289 | thomas.heller | 2008-01-25 19:59:45 +0100 (Fri, 25 Jan 2008) | 2 lines
Fix format string for structures, and itemsize for arrays.
........
r60290 | thomas.heller | 2008-01-25 20:09:03 +0100 (Fri, 25 Jan 2008) | 2 lines
Implement to format string for function pointers.
........
r60292 | thomas.heller | 2008-01-25 20:32:20 +0100 (Fri, 25 Jan 2008) | 3 lines
Only structures with native packing implement the pep. Unions, or
packed structures do not.
........
r60293 | thomas.heller | 2008-01-25 20:34:31 +0100 (Fri, 25 Jan 2008) | 2 lines
Update the test.
........
r60295 | thomas.heller | 2008-01-25 20:44:41 +0100 (Fri, 25 Jan 2008) | 2 lines
Fixed a few XXX markers.
........
r60298 | thomas.heller | 2008-01-25 21:11:08 +0100 (Fri, 25 Jan 2008) | 1 line
Fix test for 64-bt platform.
........
r60299 | thomas.heller | 2008-01-25 21:34:11 +0100 (Fri, 25 Jan 2008) | 2 lines
Add test for the readonly bit.
........
r60384 | thomas.heller | 2008-01-28 08:45:04 +0100 (Mon, 28 Jan 2008) | 4 lines
Restructure the test so that it contains little endian format strings.
On big endian machines, the format strings are converted by replacing
'<' with '>'.
........
r60385 | thomas.heller | 2008-01-28 08:58:46 +0100 (Mon, 28 Jan 2008) | 1 line
Bugfix and test for explicit big and little endian types.
........
r60428 | thomas.heller | 2008-01-29 22:00:37 +0100 (Tue, 29 Jan 2008) | 1 line
Add comments to clarify the tests.
........
r62589 | thomas.heller | 2008-04-30 13:49:46 +0200 (Wed, 30 Apr 2008) | 1 line
Fix compiler warnings.
........
Diffstat (limited to 'Modules/_ctypes/stgdict.c')
-rw-r--r-- | Modules/_ctypes/stgdict.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index 0ad5840336..085b19f508 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -2,6 +2,7 @@ #include <ffi.h> #ifdef MS_WIN32 #include <windows.h> +#include <malloc.h> #endif #include "ctypes.h" @@ -20,6 +21,9 @@ StgDict_init(StgDictObject *self, PyObject *args, PyObject *kwds) { if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0) return -1; + self->format = NULL; + self->ndim = 0; + self->shape = NULL; return 0; } @@ -38,6 +42,8 @@ static void StgDict_dealloc(StgDictObject *self) { StgDict_clear(self); + PyMem_Free(self->format); + PyMem_Free(self->shape); PyMem_Free(self->ffi_type_pointer.elements); PyDict_Type.tp_dealloc((PyObject *)self); } @@ -50,6 +56,10 @@ StgDict_clone(StgDictObject *dst, StgDictObject *src) StgDict_clear(dst); PyMem_Free(dst->ffi_type_pointer.elements); + PyMem_Free(dst->format); + dst->format = NULL; + PyMem_Free(dst->shape); + dst->shape = NULL; dst->ffi_type_pointer.elements = NULL; d = (char *)dst; @@ -64,6 +74,20 @@ StgDict_clone(StgDictObject *dst, StgDictObject *src) Py_XINCREF(dst->restype); Py_XINCREF(dst->checker); + if (src->format) { + dst->format = PyMem_Malloc(strlen(src->format) + 1); + if (dst->format == NULL) + return -1; + strcpy(dst->format, src->format); + } + if (src->shape) { + dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim); + if (dst->shape == NULL) + return -1; + memcpy(dst->shape, src->shape, + sizeof(Py_ssize_t) * src->ndim); + } + if (src->ffi_type_pointer.elements == NULL) return 0; size = sizeof(ffi_type *) * (src->length + 1); @@ -341,6 +365,11 @@ StructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct) return -1; } + if (stgdict->format) { + PyMem_Free(stgdict->format); + stgdict->format = NULL; + } + if (stgdict->ffi_type_pointer.elements) PyMem_Free(stgdict->ffi_type_pointer.elements); @@ -379,6 +408,15 @@ StructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct) ffi_ofs = 0; } + if (isStruct && !isPacked) { + stgdict->format = alloc_format_string(NULL, "T{"); + } else { + /* PEP3118 doesn't support union, or packed structures (well, + only standard packing, but we dont support the pep for + that). Use 'B' for bytes. */ + stgdict->format = alloc_format_string(NULL, "B"); + } + #define realdict ((PyObject *)&stgdict->dict) for (i = 0; i < len; ++i) { PyObject *name = NULL, *desc = NULL; @@ -439,6 +477,24 @@ StructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct) } } else bitsize = 0; + if (isStruct && !isPacked) { + char *fieldfmt = dict->format ? dict->format : "B"; + char *fieldname = PyUnicode_AsString(name); + char *ptr; + Py_ssize_t len = strlen(fieldname) + strlen(fieldfmt); + char *buf = alloca(len + 2 + 1); + + sprintf(buf, "%s:%s:", fieldfmt, fieldname); + + ptr = stgdict->format; + stgdict->format = alloc_format_string(stgdict->format, buf); + PyMem_Free(ptr); + + if (stgdict->format == NULL) { + Py_DECREF(pair); + return -1; + } + } if (isStruct) { prop = CField_FromDesc(desc, i, &field_size, bitsize, &bitofs, @@ -469,6 +525,13 @@ StructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct) Py_DECREF(prop); } #undef realdict + + if (isStruct && !isPacked) { + stgdict->format = alloc_format_string(stgdict->format, "}"); + if (stgdict->format == NULL) + return -1; + } + if (!isStruct) size = union_size; |