diff options
author | Thomas Heller <theller@ctypes.org> | 2008-01-17 18:46:55 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2008-01-17 18:46:55 (GMT) |
commit | ff72122184556afae962f32b7d7ac47bb2d5de33 (patch) | |
tree | 1a44c3b7fb2179e8ee0cc5559f4cbfc4d532423d /Modules | |
parent | 9893de1b83978249eafaebe575989537c01b484e (diff) | |
download | cpython-ff72122184556afae962f32b7d7ac47bb2d5de33.zip cpython-ff72122184556afae962f32b7d7ac47bb2d5de33.tar.gz cpython-ff72122184556afae962f32b7d7ac47bb2d5de33.tar.bz2 |
Merged revisions 60001,60003-60004,60008 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60001 | thomas.heller | 2008-01-16 20:16:27 +0100 (Mi, 16 Jan 2008) | 3 lines
Convert the internal ctypes array type cache to a WeakValueDict so
that array types do not live longer than needed.
........
r60003 | thomas.heller | 2008-01-16 20:37:33 +0100 (Mi, 16 Jan 2008) | 3 lines
Raise a TypeError if conflicting positional and named arguments are
passed to a Structure or Union constructor.
........
r60004 | thomas.heller | 2008-01-16 20:45:51 +0100 (Mi, 16 Jan 2008) | 3 lines
Raise a TypeError instead of a ValueError when too many initializers
are used in a Structure or Union constructor.
........
r60008 | thomas.heller | 2008-01-16 21:34:37 +0100 (Mi, 16 Jan 2008) | 3 lines
Use 'g' instead of 'D' as the ctypes typecode for c_longdouble, for
compliance with PEP 3118.
........
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 49 | ||||
-rw-r--r-- | Modules/_ctypes/cfield.c | 6 |
2 files changed, 40 insertions, 15 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index f60d494..c6098b9 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -122,6 +122,7 @@ bytes(cdata) PyObject *PyExc_ArgError; static PyTypeObject Simple_Type; +PyObject *array_types_cache; char *conversion_mode_encoding = NULL; char *conversion_mode_errors = NULL; @@ -1112,7 +1113,7 @@ _type_ attribute. */ -static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOvtD"; +static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOvtg"; static PyObject * c_wchar_p_from_param(PyObject *type, PyObject *value) @@ -3535,7 +3536,7 @@ Struct_init(PyObject *self, PyObject *args, PyObject *kwds) if (PyTuple_GET_SIZE(args) > PySequence_Length(fields)) { Py_DECREF(fields); - PyErr_SetString(PyExc_ValueError, + PyErr_SetString(PyExc_TypeError, "too many initializers"); return -1; } @@ -3556,6 +3557,21 @@ Struct_init(PyObject *self, PyObject *args, PyObject *kwds) return IBUG("_fields_[i][0] failed"); } + if (kwds && PyDict_GetItem(kwds, name)) { + char *field = PyString_AsString(name); + if (field == NULL) { + PyErr_Clear(); + field = "???"; + } + PyErr_Format(PyExc_TypeError, + "duplicate values for field %s", + field); + Py_DECREF(pair); + Py_DECREF(name); + Py_DECREF(fields); + return -1; + } + val = PyTuple_GET_ITEM(args, i); if (-1 == PyObject_SetAttr(self, name, val)) { Py_DECREF(pair); @@ -3977,25 +3993,19 @@ PyTypeObject Array_Type = { PyObject * CreateArrayType(PyObject *itemtype, Py_ssize_t length) { - static PyObject *cache; PyObject *key; PyObject *result; char name[256]; - if (cache == NULL) { - cache = PyDict_New(); - if (cache == NULL) - return NULL; - } key = Py_BuildValue("(On)", itemtype, length); if (!key) return NULL; - result = PyDict_GetItem(cache, key); + result = PyObject_GetItem(array_types_cache, key); if (result) { - Py_INCREF(result); Py_DECREF(key); return result; - } + } else + PyErr_Clear(); if (!PyType_Check(itemtype)) { PyErr_SetString(PyExc_TypeError, @@ -4021,7 +4031,11 @@ CreateArrayType(PyObject *itemtype, Py_ssize_t length) ); if (!result) return NULL; - PyDict_SetItem(cache, key, result); + if (-1 == PyObject_SetItem(array_types_cache, key, result)) { + Py_DECREF(key); + Py_DECREF(result); + return NULL; + } Py_DECREF(key); return result; } @@ -4778,6 +4792,7 @@ PyMODINIT_FUNC init_ctypes(void) { PyObject *m; + PyObject *weakref; /* Note: ob_type is the metatype (the 'type'), defaults to PyType_Type, @@ -4790,6 +4805,16 @@ init_ctypes(void) if (!m) return; + weakref = PyImport_ImportModule("weakref"); + if (weakref == NULL) + return; + array_types_cache = PyObject_CallMethod(weakref, + "WeakValueDictionary", + NULL); + if (array_types_cache == NULL) + return; + Py_DECREF(weakref); + if (PyType_Ready(&PyCArg_Type) < 0) return; diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index e7a534e..f4056bf 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -983,7 +983,7 @@ Q_get_sw(void *ptr, Py_ssize_t size) static PyObject * -D_set(void *ptr, PyObject *value, Py_ssize_t size) +g_set(void *ptr, PyObject *value, Py_ssize_t size) { long double x; @@ -999,7 +999,7 @@ D_set(void *ptr, PyObject *value, Py_ssize_t size) } static PyObject * -D_get(void *ptr, Py_ssize_t size) +g_get(void *ptr, Py_ssize_t size) { long double val; memcpy(&val, ptr, sizeof(long double)); @@ -1630,7 +1630,7 @@ static struct fielddesc formattable[] = { { 'B', B_set, B_get, &ffi_type_uchar}, { 'c', c_set, c_get, &ffi_type_schar}, { 'd', d_set, d_get, &ffi_type_double, d_set_sw, d_get_sw}, - { 'D', D_set, D_get, &ffi_type_longdouble}, + { 'g', g_set, g_get, &ffi_type_longdouble}, { 'f', f_set, f_get, &ffi_type_float, f_set_sw, f_get_sw}, { 'h', h_set, h_get, &ffi_type_sshort, h_set_sw, h_get_sw}, { 'H', H_set, H_get, &ffi_type_ushort, H_set_sw, H_get_sw}, |