[Python-3000-checkins] r60278 - 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 11:53:34 CET 2008
Author: thomas.heller
Date: Fri Jan 25 11:53:33 2008
New Revision: 60278
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:
Implement pep3118 format strings for ctypes.Structure and ctypes.Union.
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 11:53:33 2008
@@ -135,7 +135,7 @@
indicator set. If called with a suffix of NULL the error indicator must
already be set.
*/
-static char *
+char *
alloc_format_string(const char *prefix, const char *suffix)
{
size_t len;
@@ -169,6 +169,14 @@
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
@@ -1059,6 +1067,7 @@
return NULL;
}
+ assert(itemdict->format);
if (itemdict->format[0] == '(') {
sprintf(buf, "(%d,", length);
stgdict->format = alloc_format_string(buf, itemdict->format+1);
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 11:53:33 2008
@@ -343,6 +343,8 @@
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);
+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 11:53:33 2008
@@ -2,6 +2,7 @@
#include <ffi.h>
#ifdef MS_WIN32
#include <windows.h>
+#include <malloc.h>
#endif
#include "ctypes.h"
@@ -55,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;
@@ -69,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);
@@ -346,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);
@@ -384,6 +408,10 @@
ffi_ofs = 0;
}
+ stgdict->format = alloc_format_string(NULL, "}");
+ if (stgdict->format == NULL)
+ return -1;
+
#define realdict ((PyObject *)&stgdict->dict)
for (i = 0; i < len; ++i) {
PyObject *name = NULL, *desc = NULL;
@@ -442,6 +470,19 @@
}
} 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);
+ if (stgdict->format == NULL) {
+ Py_DECREF(pair);
+ return -1;
+ }
+ }
if (isStruct) {
prop = CField_FromDesc(desc, i,
&field_size, bitsize, &bitofs,
@@ -472,6 +513,16 @@
Py_DECREF(prop);
}
#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);
+ }
+ if (stgdict->format == NULL)
+ return -1;
+
if (!isStruct)
size = union_size;
More information about the Python-3000-checkins
mailing list