Author: thomas.heller
Date: Thu Jan 24 22:01:29 2008
New Revision: 60261

Modified:
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/callproc.c
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/ctypes.h
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/stgdict.c
Log:
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.


Modified: python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c       
(original)
+++ python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c       Thu Jan 
24 22:01:29 2008
@@ -1019,6 +1019,7 @@
        long length;
        int overflow;
        Py_ssize_t itemsize, itemalign;
+       char buf[32];
 
        typedict = PyTuple_GetItem(args, 2);
        if (!typedict)
@@ -1058,6 +1059,27 @@
                return NULL;
        }
 
+       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,

Modified: python/branches/py3k-ctypes-pep3118/Modules/_ctypes/callproc.c
==============================================================================
--- python/branches/py3k-ctypes-pep3118/Modules/_ctypes/callproc.c      
(original)
+++ python/branches/py3k-ctypes-pep3118/Modules/_ctypes/callproc.c      Thu Jan 
24 22:01:29 2008
@@ -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},

Modified: python/branches/py3k-ctypes-pep3118/Modules/_ctypes/ctypes.h
==============================================================================
--- python/branches/py3k-ctypes-pep3118/Modules/_ctypes/ctypes.h        
(original)
+++ python/branches/py3k-ctypes-pep3118/Modules/_ctypes/ctypes.h        Thu Jan 
24 22:01:29 2008
@@ -200,7 +200,14 @@
        PyObject *restype;      /* CDataObject or NULL */
        PyObject *checker;
        int flags;              /* calling convention and such */
-       char *format;           /* pep3118 format string, needs PyMem_Free */
+
+       /* 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;
 
 /****************************************************************

Modified: python/branches/py3k-ctypes-pep3118/Modules/_ctypes/stgdict.c
==============================================================================
--- python/branches/py3k-ctypes-pep3118/Modules/_ctypes/stgdict.c       
(original)
+++ python/branches/py3k-ctypes-pep3118/Modules/_ctypes/stgdict.c       Thu Jan 
24 22:01:29 2008
@@ -21,6 +21,8 @@
        if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
                return -1;
        self->format = NULL;
+       self->ndim = 0;
+       self->shape = NULL;
        return 0;
 }
 
@@ -40,6 +42,7 @@
 {
        StgDict_clear(self);
        PyMem_Free(self->format);
+       PyMem_Free(self->shape);
        PyMem_Free(self->ffi_type_pointer.elements);
        PyDict_Type.tp_dealloc((PyObject *)self);
 }
_______________________________________________
Python-3000-checkins mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000-checkins

Reply via email to