diff options
author | Thomas Heller <theller@ctypes.org> | 2007-07-13 11:19:35 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2007-07-13 11:19:35 (GMT) |
commit | e5095e187ba80a5265d44807c7371440f903dc31 (patch) | |
tree | 54443135dd55f5bf7a2cc39c370542030ed2ea6c /Modules/_ctypes | |
parent | 745f5e2de709722e1586df597af59477cb3f6980 (diff) | |
download | cpython-e5095e187ba80a5265d44807c7371440f903dc31.zip cpython-e5095e187ba80a5265d44807c7371440f903dc31.tar.gz cpython-e5095e187ba80a5265d44807c7371440f903dc31.tar.bz2 |
Structure fields of type c_char array or c_wchar array accept bytes or
(unicode) string.
Diffstat (limited to 'Modules/_ctypes')
-rw-r--r-- | Modules/_ctypes/cfield.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index a8d0d4b..8a0dfe7 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -1260,7 +1260,7 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length) /* It's easier to calculate in characters than in bytes */ length /= sizeof(wchar_t); - if (PyString_Check(value)) { + if (PyBytes_Check(value)) { value = PyUnicode_FromEncodedObject(value, conversion_mode_encoding, conversion_mode_errors); @@ -1322,7 +1322,23 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length) char *data; Py_ssize_t size; - data = PyString_AsString(value); + if (PyUnicode_Check(value)) { + value = PyUnicode_AsEncodedString(value, + conversion_mode_encoding, + conversion_mode_errors); + if (value == NULL) + return NULL; + assert(PyBytes_Check(value)); + } else if(PyBytes_Check(value)) { + Py_INCREF(value); + } else { + PyErr_Format(PyExc_TypeError, + "expected string, %s found", + value->ob_type->tp_name); + return NULL; + } + + data = PyBytes_AsString(value); if (!data) return NULL; size = strlen(data); @@ -1339,10 +1355,13 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length) "string too long (%zd, maximum length %zd)", #endif size, length); + Py_DECREF(value); return NULL; } /* Also copy the terminating NUL character if there is space */ memcpy((char *)ptr, data, size); + + Py_DECREF(value); _RET(value); } |