[Python-3000-checkins] r60289 - python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c python/branches/py3k-ctypes-pep3118/Modules/_ctypes/ctypes.h python/branches/py3k-ctypes-pep3118/Modules/_ctypes/stgdict.c

thomas.heller python-3000-checkins at python.org
Fri Jan 25 19:59:45 CET 2008


Author: thomas.heller
Date: Fri Jan 25 19:59:45 2008
New Revision: 60289

Modified:
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/ctypes.h
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/stgdict.c
Log:
Fix format string for structures, and itemsize for arrays.


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	Fri Jan 25 19:59:45 2008
@@ -291,14 +291,6 @@
 	return result;
 }
 
-char *
-replace_format_string(const char *prefix, char *suffix)
-{
-	char *result = alloc_format_string(prefix, suffix);
-	PyMem_Free(suffix);
-	return result;
-}
-
 /*
   StructType_Type - a meta type/class.  Creating a new class using this one as
   __metaclass__ will call the contructor StructUnionType_new.  It replaces the
@@ -2319,6 +2311,7 @@
 {
 	CDataObject *self = (CDataObject *)_self;
 	StgDictObject *dict = PyObject_stgdict(_self);
+	Py_ssize_t i;
 
 	if (view == NULL) return 0;
 	if (((flags & PyBUF_LOCK) == PyBUF_LOCK)) {
@@ -2330,7 +2323,6 @@
 	view->buf = self->b_ptr;
 	view->len = self->b_size;
 	view->readonly = 0;
-	view->itemsize = self->b_size;
 #if 1
 	/* XXX fix later */
 	/* use default format character if not set */
@@ -2340,6 +2332,10 @@
 #endif
 	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;
@@ -4824,7 +4820,7 @@
 	0,					/* tp_str */
 	0,					/* tp_getattro */
 	0,					/* tp_setattro */
-	&Simple_as_buffer,			/* tp_as_buffer */
+	&CData_as_buffer,			/* tp_as_buffer */
 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
 	"XXX to be provided",			/* tp_doc */
 	(traverseproc)CData_traverse,		/* tp_traverse */

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	Fri Jan 25 19:59:45 2008
@@ -344,7 +344,6 @@
 
 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);
-extern char *replace_format_string(const char *prefix, char *suffix);
 
 /* XXX better name needed! */
 extern int IsSimpleSubType(PyObject *obj);

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	Fri Jan 25 19:59:45 2008
@@ -408,9 +408,12 @@
 		ffi_ofs = 0;
 	}
 
-	stgdict->format = alloc_format_string(NULL, "}");
-	if (stgdict->format == NULL)
-		return -1;
+	if (isStruct) {
+		stgdict->format = alloc_format_string(NULL, "T{");
+	} else {
+		/* PEP3118 doesn't support unions. Invent our own character... */
+		stgdict->format = alloc_format_string(NULL, "#{");
+	}
 
 #define realdict ((PyObject *)&stgdict->dict)
 	for (i = 0; i < len; ++i) {
@@ -471,13 +474,17 @@
 		} else
 			bitsize = 0;
 		{
-			int len = PyUnicode_GetSize(name);
-			char *buf = alloca(len);
-			sprintf(buf, ":%s:", PyUnicode_AsString(name));
-/* XXX FIXME */
-/*			assert(dict->format); */
-			strcat(buf, dict->format ? dict->format : "XXX");
-			stgdict->format = replace_format_string(buf, stgdict->format);
+			char *fieldfmt = dict->format ? dict->format : "XXX";
+			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;
@@ -514,12 +521,7 @@
 	}
 #undef realdict
 
-	if (isStruct) {
-		stgdict->format = replace_format_string("T{", stgdict->format);
-	} else {
-		/* PEP3118 doesn't support unions. Invent our own character... */
-		stgdict->format = replace_format_string("#{", stgdict->format);
-	}
+	stgdict->format = alloc_format_string(stgdict->format, "}");
 	if (stgdict->format == NULL)
 		return -1;
 


More information about the Python-3000-checkins mailing list