summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2007-07-12 14:58:32 (GMT)
committerThomas Heller <theller@ctypes.org>2007-07-12 14:58:32 (GMT)
commit3af4266d07c38a136858acd63fe663d24590400c (patch)
tree57a1cfb0191b8d4b392557195cdea0c690c0e9af /Modules
parentf7c6d868325b999c32ec759934eac7a96c4b56fd (diff)
downloadcpython-3af4266d07c38a136858acd63fe663d24590400c.zip
cpython-3af4266d07c38a136858acd63fe663d24590400c.tar.gz
cpython-3af4266d07c38a136858acd63fe663d24590400c.tar.bz2
ctypes.c_char and ctypes.c_wchar now accept initialization from byte objects.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_ctypes/cfield.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 9c49c1e..75b00b6 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1141,6 +1141,27 @@ O_set(void *ptr, PyObject *value, Py_ssize_t size)
static PyObject *
c_set(void *ptr, PyObject *value, Py_ssize_t size)
{
+ if (PyUnicode_Check(value)) {
+ value = PyUnicode_AsEncodedString(value,
+ conversion_mode_encoding,
+ conversion_mode_errors);
+ if (value == NULL)
+ return NULL;
+ if (PyBytes_GET_SIZE(value) != 1) {
+ Py_DECREF(value);
+ PyErr_Format(PyExc_TypeError,
+ "one character string expected");
+ return NULL;
+ }
+ *(char *)ptr = PyBytes_AsString(value)[0];
+ Py_DECREF(value);
+ _RET(value);
+ }
+ if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) {
+ *(char *)ptr = PyBytes_AsString(value)[0];
+ _RET(value);
+ }
+ /* XXX struni remove later */
if (!PyString_Check(value) || (1 != PyString_Size(value))) {
PyErr_Format(PyExc_TypeError,
"one character string expected");
@@ -1154,6 +1175,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
static PyObject *
c_get(void *ptr, Py_ssize_t size)
{
+ /* XXX struni return PyBytes (or PyUnicode?) later */
return PyString_FromStringAndSize((char *)ptr, 1);
}
@@ -1163,8 +1185,7 @@ static PyObject *
u_set(void *ptr, PyObject *value, Py_ssize_t size)
{
Py_ssize_t len;
-
- if (PyString_Check(value)) {
+ if (PyBytes_Check(value)) {
value = PyUnicode_FromEncodedObject(value,
conversion_mode_encoding,
conversion_mode_errors);