diff Modules/_ctypes/callproc.c @ 47475:bbce18d00d87 legacy-trunk

Backport from py3k: Implement the new buffer interface from pep3118 for ctypes instances. Closes issue #2404.
author Thomas Heller <theller@ctypes.org>
date Thu, 05 Jun 2008 17:51:15 +0000
parents b0d4799b0db8
children 4f5e6aaf5a6a
line wrap: on
line diff
--- a/Modules/_ctypes/callproc.c	Thu Jun 05 17:29:38 2008 +0000
+++ b/Modules/_ctypes/callproc.c	Thu Jun 05 17:51:15 2008 +0000
@@ -1666,10 +1666,37 @@
 	return result;
 }
 
+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[] = {
 	{"POINTER", POINTER, METH_O },
 	{"pointer", pointer, METH_O },
 	{"_unpickle", unpickle, METH_VARARGS },
+	{"_buffer_info", buffer_info, METH_O,
+	 "Return buffer interface information (for testing only)"},
 	{"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},