Index: Lib/ctypes/test/test_pep3118.py =================================================================== --- Lib/ctypes/test/test_pep3118.py (revision 0) +++ Lib/ctypes/test/test_pep3118.py (revision 0) @@ -0,0 +1,170 @@ +import unittest +from ctypes import * +import re, struct, sys + +if sys.byteorder == "little": + THIS_ENDIAN = "<" + OTHER_ENDIAN = ">" +else: + THIS_ENDIAN = ">" + OTHER_ENDIAN = "<" + +def normalize(format): + # Remove current endian specifier and white space from a format + # string + format = format.replace(OTHER_ENDIAN, THIS_ENDIAN) + return re.sub(r"\s", "", format) + +class Test(unittest.TestCase): + + def test_native_types(self): + for tp, fmt, shape, itemtp in native_types: + ob = tp() + v = memoryview(ob) + try: + self.failUnlessEqual(normalize(v.format), normalize(fmt)) + self.failUnlessEqual(v.size, sizeof(ob)) + self.failUnlessEqual(v.itemsize, sizeof(itemtp)) + self.failUnlessEqual(v.shape, shape) + # ctypes object always have a non-strided memory block + self.failUnlessEqual(v.strides, None) + # they are always read/write + self.failIf(v.readonly) + + if v.shape: + n = 1 + for dim in v.shape: + n = n * dim + self.failUnlessEqual(v.itemsize * n, v.size) + except: + # so that we can see the failing type + print(tp) + raise + + def test_endian_types(self): + for tp, fmt, shape, itemtp in endian_types: + ob = tp() + v = memoryview(ob) + try: + self.failUnlessEqual(v.format, fmt) + self.failUnlessEqual(v.size, sizeof(ob)) + self.failUnlessEqual(v.itemsize, sizeof(itemtp)) + self.failUnlessEqual(v.shape, shape) + # ctypes object always have a non-strided memory block + self.failUnlessEqual(v.strides, None) + # they are always read/write + self.failIf(v.readonly) + + if v.shape: + n = 1 + for dim in v.shape: + n = n * dim + self.failUnlessEqual(v.itemsize * n, v.size) + except: + # so that we can see the failing type + print(tp) + raise + +# define some structure classes + +class Point(Structure): + _fields_ = [("x", c_long), ("y", c_long)] + +class PackedPoint(Structure): + _pack_ = 2 + _fields_ = [("x", c_long), ("y", c_long)] + +class Point2(Structure): + pass +Point2._fields_ = [("x", c_long), ("y", c_long)] + +class EmptyStruct(Structure): + _fields_ = [] + +class aUnion(Union): + _fields_ = [("a", c_int)] + +################################################################ +# +# This table contains format strings as they look on little endian +# machines. The test replaces '<' with '>' on big endian machines. +# +native_types = [ + # type format shape calc itemsize + + ## simple types + + (c_char, "l:x:>l:y:}", None, BEPoint), + (LEPoint, "T{l:x:>l:y:}", None, POINTER(BEPoint)), + (POINTER(LEPoint), "&T{format = alloc_format_string("&", itemdict->format); + if (stgdict->format == NULL) { + Py_DECREF((PyObject *)stgdict); + return NULL; + } + } + /* create the new instance (which is a class, since we are a metatype!) */ result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); @@ -1091,6 +1131,7 @@ long length; int overflow; Py_ssize_t itemsize, itemalign; + char buf[32]; typedict = PyTuple_GetItem(args, 2); if (!typedict) @@ -1130,6 +1171,28 @@ return NULL; } + assert(itemdict->format); + if (itemdict->format[0] == '(') { + sprintf(buf, "(%d,", length); + stgdict->format = alloc_format_string(buf, itemdict->format+1); + } else { + sprintf(buf, "(%d)", length); + stgdict->format = alloc_format_string(buf, itemdict->format); + } + if (stgdict->format == NULL) { + Py_DECREF((PyObject *)stgdict); + return NULL; + } + stgdict->ndim = itemdict->ndim + 1; + stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim); + if (stgdict->shape == NULL) { + Py_DECREF((PyObject *)stgdict); + return NULL; + } + stgdict->shape[0] = length; + memmove(&stgdict->shape[1], itemdict->shape, + sizeof(Py_ssize_t) * (stgdict->ndim - 1)); + itemsize = itemdict->size; if (length * itemsize < 0) { PyErr_SetString(PyExc_OverflowError, @@ -1678,6 +1741,16 @@ stgdict->size = fmt->pffi_type->size; stgdict->setfunc = fmt->setfunc; stgdict->getfunc = fmt->getfunc; +#ifdef WORDS_BIGENDIAN + stgdict->format = alloc_format_string(">", proto_str); +#else + stgdict->format = alloc_format_string("<", proto_str); +#endif + if (stgdict->format == NULL) { + Py_DECREF(result); + Py_DECREF((PyObject *)stgdict); + return NULL; + } stgdict->paramfunc = SimpleType_paramfunc; /* @@ -1738,22 +1811,32 @@ if (type == &SimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) { PyObject *swapped = CreateSwappedType(type, args, kwds, proto, fmt); + StgDictObject *sw_dict; if (swapped == NULL) { Py_DECREF(result); return NULL; } + sw_dict = PyType_stgdict(swapped); #ifdef WORDS_BIGENDIAN PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped); PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result); PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result); PyObject_SetAttrString(swapped, "__ctype_le__", swapped); + /* We are creating the type for the OTHER endian */ + sw_dict->format = alloc_format_string("<", stgdict->format+1); #else PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped); PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result); PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result); PyObject_SetAttrString(swapped, "__ctype_be__", swapped); + /* We are creating the type for the OTHER endian */ + sw_dict->format = alloc_format_string(">", stgdict->format+1); #endif Py_DECREF(swapped); + if (PyErr_Occurred()) { + Py_DECREF(result); + return NULL; + } }; return (PyObject *)result; @@ -2003,6 +2086,13 @@ return NULL; stgdict->paramfunc = CFuncPtrType_paramfunc; + /* We do NOT expose the function signature in the format string. It + is impossible, generally, because the only requirement for the + argtypes items is that they have a .from_param method - we do not + know the types of the arguments (although, in practice, most + argtypes would be a ctypes type). + */ + stgdict->format = alloc_format_string(NULL, "X{}"); /* create the new instance (which is a class, since we are a metatype!) */ @@ -2217,7 +2307,31 @@ static int CData_GetBuffer(PyObject *_self, Py_buffer *view, int flags) { CDataObject *self = (CDataObject *)_self; - return PyBuffer_FillInfo(view, self->b_ptr, self->b_size, 0, flags); + StgDictObject *dict = PyObject_stgdict(_self); + Py_ssize_t i; + + if (view == NULL) return 0; + if (((flags & PyBUF_LOCK) == PyBUF_LOCK)) { + PyErr_SetString(PyExc_BufferError, + "Cannot lock this object."); + return -1; + } + + view->buf = self->b_ptr; + view->len = self->b_size; + view->readonly = 0; + /* use default format character if not set */ + view->format = dict->format ? dict->format : "B"; + view->ndim = dict->ndim; + view->shape = dict->shape; + view->itemsize = self->b_size; + for (i = 0; i < view->ndim; ++i) { + view->itemsize /= dict->shape[i]; + } + view->strides = NULL; + view->suboffsets = NULL; + view->internal = NULL; + return 0; } static PyBufferProcs CData_as_buffer = { Index: Modules/_ctypes/callproc.c =================================================================== --- Modules/_ctypes/callproc.c (revision 60428) +++ Modules/_ctypes/callproc.c (working copy) @@ -1543,7 +1543,33 @@ return Py_None; } +static PyObject * +buffer_info(PyObject *self, PyObject *arg) +{ + StgDictObject *dict = PyType_stgdict(arg); + PyObject *shape; + Py_ssize_t i; + + if (dict == NULL) + dict = PyObject_stgdict(arg); + if (dict == NULL) { + PyErr_SetString(PyExc_TypeError, + "not a ctypes type or object"); + return NULL; + } + shape = PyTuple_New(dict->ndim); + for (i = 0; i < (int)dict->ndim; ++i) + PyTuple_SET_ITEM(shape, i, PyLong_FromSsize_t(dict->shape[i])); + + if (PyErr_Occurred()) { + Py_DECREF(shape); + return NULL; + } + return Py_BuildValue("siN", dict->format, dict->ndim, shape); +} + PyMethodDef module_methods[] = { + {"buffer_info", buffer_info, METH_O, "Return buffer interface information"}, {"resize", resize, METH_VARARGS, "Resize the memory buffer of a ctypes instance"}, #ifdef CTYPES_UNICODE {"set_conversion_mode", set_conversion_mode, METH_VARARGS, set_conversion_mode_doc}, Index: Modules/_ctypes/ctypes.h =================================================================== --- Modules/_ctypes/ctypes.h (revision 60428) +++ Modules/_ctypes/ctypes.h (working copy) @@ -200,6 +200,14 @@ PyObject *restype; /* CDataObject or NULL */ PyObject *checker; int flags; /* calling convention and such */ + + /* pep3118 fields, pointers neeed PyMem_Free */ + char *format; + int ndim; + Py_ssize_t *shape; +/* Py_ssize_t *strides; /* unused in ctypes */ +/* Py_ssize_t *suboffsets; /* unused in ctypes */ + } StgDictObject; /**************************************************************** @@ -335,6 +343,7 @@ extern void _AddTraceback(char *, char *, int); extern PyObject *CData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr); +extern char *alloc_format_string(const char *prefix, const char *suffix); /* XXX better name needed! */ extern int IsSimpleSubType(PyObject *obj); Index: Modules/_ctypes/stgdict.c =================================================================== --- Modules/_ctypes/stgdict.c (revision 60428) +++ Modules/_ctypes/stgdict.c (working copy) @@ -2,6 +2,7 @@ #include #ifdef MS_WIN32 #include +#include #endif #include "ctypes.h" @@ -20,6 +21,9 @@ { 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 @@ 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_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 @@ 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 @@ 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 @@ 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; @@ -437,6 +475,24 @@ } } 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, @@ -467,6 +523,13 @@ 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;