summaryrefslogtreecommitdiffstats
path: root/Modules/_ctypes
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-11-06 21:34:58 (GMT)
committerGuido van Rossum <guido@python.org>2007-11-06 21:34:58 (GMT)
commit98297ee7815939b124156e438b22bd652d67b5db (patch)
treea9d239ebd87c73af2571ab48003984c4e18e27e5 /Modules/_ctypes
parenta19f80c6df2df5e8a5d0cff37131097835ef971e (diff)
downloadcpython-98297ee7815939b124156e438b22bd652d67b5db.zip
cpython-98297ee7815939b124156e438b22bd652d67b5db.tar.gz
cpython-98297ee7815939b124156e438b22bd652d67b5db.tar.bz2
Merging the py3k-pep3137 branch back into the py3k branch.
No detailed change log; just check out the change log for the py3k-pep3137 branch. The most obvious changes: - str8 renamed to bytes (PyString at the C level); - bytes renamed to buffer (PyBytes at the C level); - PyString and PyUnicode are no longer compatible. I.e. we now have an immutable bytes type and a mutable bytes type. The behavior of PyString was modified quite a bit, to make it more bytes-like. Some changes are still on the to-do list.
Diffstat (limited to 'Modules/_ctypes')
-rw-r--r--Modules/_ctypes/_ctypes.c38
-rw-r--r--Modules/_ctypes/callproc.c4
-rw-r--r--Modules/_ctypes/cfield.c35
3 files changed, 40 insertions, 37 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 81276fa..39dfdef 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -763,7 +763,7 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
static PyObject *
CharArray_get_raw(CDataObject *self)
{
- return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
+ return PyString_FromStringAndSize(self->b_ptr, self->b_size);
}
static PyObject *
@@ -774,7 +774,7 @@ CharArray_get_value(CDataObject *self)
for (i = 0; i < self->b_size; ++i)
if (*ptr++ == '\0')
break;
- return PyBytes_FromStringAndSize(self->b_ptr, i);
+ return PyString_FromStringAndSize(self->b_ptr, i);
}
static int
@@ -789,14 +789,14 @@ CharArray_set_value(CDataObject *self, PyObject *value)
conversion_mode_errors);
if (!value)
return -1;
- } else if (!PyBytes_Check(value)) {
+ } else if (!PyString_Check(value)) {
PyErr_Format(PyExc_TypeError,
"str/bytes expected instead of %s instance",
Py_Type(value)->tp_name);
return -1;
} else
Py_INCREF(value);
- size = PyBytes_GET_SIZE(value);
+ size = PyString_GET_SIZE(value);
if (size > self->b_size) {
PyErr_SetString(PyExc_ValueError,
"string too long");
@@ -804,7 +804,7 @@ CharArray_set_value(CDataObject *self, PyObject *value)
return -1;
}
- ptr = PyBytes_AS_STRING(value);
+ ptr = PyString_AS_STRING(value);
memcpy(self->b_ptr, ptr, size);
if (size < self->b_size)
self->b_ptr[size] = '\0';
@@ -838,7 +838,7 @@ WCharArray_set_value(CDataObject *self, PyObject *value)
{
Py_ssize_t result = 0;
- if (PyBytes_Check(value)) {
+ if (PyString_Check(value)) {
value = PyUnicode_FromEncodedObject(value,
conversion_mode_encoding,
conversion_mode_errors);
@@ -1106,7 +1106,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
Py_INCREF(Py_None);
return Py_None;
}
- if (PyUnicode_Check(value) || PyBytes_Check(value)) {
+ if (PyUnicode_Check(value) || PyString_Check(value)) {
PyCArgObject *parg;
struct fielddesc *fd = getentry("Z");
@@ -1167,7 +1167,7 @@ c_char_p_from_param(PyObject *type, PyObject *value)
Py_INCREF(Py_None);
return Py_None;
}
- if (PyBytes_Check(value) || PyUnicode_Check(value)) {
+ if (PyString_Check(value) || PyUnicode_Check(value)) {
PyCArgObject *parg;
struct fielddesc *fd = getentry("z");
@@ -1251,7 +1251,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
}
/* XXX struni: remove later */
/* string */
- if (PyBytes_Check(value)) {
+ if (PyString_Check(value)) {
PyCArgObject *parg;
struct fielddesc *fd = getentry("z");
@@ -2705,8 +2705,8 @@ _get_name(PyObject *obj, char **pname)
return 1;
}
#endif
- if (PyBytes_Check(obj)) {
- *pname = PyBytes_AS_STRING(obj);
+ if (PyString_Check(obj)) {
+ *pname = PyString_AS_STRING(obj);
return *pname ? 1 : 0;
}
if (PyUnicode_Check(obj)) {
@@ -3734,9 +3734,9 @@ Array_subscript(PyObject *_self, PyObject *item)
char *dest;
if (slicelen <= 0)
- return PyBytes_FromStringAndSize("", 0);
+ return PyString_FromStringAndSize("", 0);
if (step == 1) {
- return PyBytes_FromStringAndSize(ptr + start,
+ return PyString_FromStringAndSize(ptr + start,
slicelen);
}
dest = (char *)PyMem_Malloc(slicelen);
@@ -3749,7 +3749,7 @@ Array_subscript(PyObject *_self, PyObject *item)
dest[i] = ptr[cur];
}
- np = PyBytes_FromStringAndSize(dest, slicelen);
+ np = PyString_FromStringAndSize(dest, slicelen);
PyMem_Free(dest);
return np;
}
@@ -4411,9 +4411,9 @@ Pointer_subscript(PyObject *_self, PyObject *item)
char *dest;
if (len <= 0)
- return PyBytes_FromStringAndSize("", 0);
+ return PyString_FromStringAndSize("", 0);
if (step == 1) {
- return PyBytes_FromStringAndSize(ptr + start,
+ return PyString_FromStringAndSize(ptr + start,
len);
}
dest = (char *)PyMem_Malloc(len);
@@ -4422,7 +4422,7 @@ Pointer_subscript(PyObject *_self, PyObject *item)
for (cur = start, i = 0; i < len; cur += step, i++) {
dest[i] = ptr[cur];
}
- np = PyBytes_FromStringAndSize(dest, len);
+ np = PyString_FromStringAndSize(dest, len);
PyMem_Free(dest);
return np;
}
@@ -4658,8 +4658,8 @@ static PyObject *
string_at(const char *ptr, int size)
{
if (size == -1)
- return PyBytes_FromStringAndSize(ptr, strlen(ptr));
- return PyBytes_FromStringAndSize(ptr, size);
+ return PyString_FromStringAndSize(ptr, strlen(ptr));
+ return PyString_FromStringAndSize(ptr, size);
}
static int
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index bc524f7..69129f7 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -507,9 +507,9 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
return 0;
}
- if (PyBytes_Check(obj)) {
+ if (PyString_Check(obj)) {
pa->ffi_type = &ffi_type_pointer;
- pa->value.p = PyBytes_AsString(obj);
+ pa->value.p = PyString_AsString(obj);
Py_INCREF(obj);
pa->keep = obj;
return 0;
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 910470a..2ec7b3a 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1157,16 +1157,20 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
conversion_mode_errors);
if (value == NULL)
return NULL;
- if (PyBytes_GET_SIZE(value) != 1) {
+ if (PyString_GET_SIZE(value) != 1) {
Py_DECREF(value);
goto error;
}
- *(char *)ptr = PyBytes_AsString(value)[0];
+ *(char *)ptr = PyString_AS_STRING(value)[0];
Py_DECREF(value);
_RET(value);
}
+ if (PyString_Check(value) && PyString_GET_SIZE(value) == 1) {
+ *(char *)ptr = PyString_AS_STRING(value)[0];
+ _RET(value);
+ }
if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) {
- *(char *)ptr = PyBytes_AsString(value)[0];
+ *(char *)ptr = PyBytes_AS_STRING(value)[0];
_RET(value);
}
if (PyInt_Check(value))
@@ -1187,7 +1191,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
static PyObject *
c_get(void *ptr, Py_ssize_t size)
{
- return PyBytes_FromStringAndSize((char *)ptr, 1);
+ return PyString_FromStringAndSize((char *)ptr, 1);
}
#ifdef CTYPES_UNICODE
@@ -1196,7 +1200,7 @@ static PyObject *
u_set(void *ptr, PyObject *value, Py_ssize_t size)
{
Py_ssize_t len;
- if (PyBytes_Check(value)) {
+ if (PyString_Check(value)) {
value = PyUnicode_FromEncodedObject(value,
conversion_mode_encoding,
conversion_mode_errors);
@@ -1271,7 +1275,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 (PyBytes_Check(value)) {
+ if (PyString_Check(value)) {
value = PyUnicode_FromEncodedObject(value,
conversion_mode_encoding,
conversion_mode_errors);
@@ -1327,8 +1331,8 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
conversion_mode_errors);
if (value == NULL)
return NULL;
- assert(PyBytes_Check(value));
- } else if(PyBytes_Check(value)) {
+ assert(PyString_Check(value));
+ } else if(PyString_Check(value)) {
Py_INCREF(value);
} else {
PyErr_Format(PyExc_TypeError,
@@ -1337,10 +1341,10 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
return NULL;
}
- data = PyBytes_AsString(value);
+ data = PyString_AS_STRING(value);
if (!data)
return NULL;
- size = strlen(data);
+ size = strlen(data); /* XXX Why not Py_Size(value)? */
if (size < length) {
/* This will copy the leading NUL character
* if there is space for it.
@@ -1368,8 +1372,8 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size)
Py_INCREF(value);
return value;
}
- if (PyBytes_Check(value)) {
- *(char **)ptr = PyBytes_AsString(value);
+ if (PyString_Check(value)) {
+ *(char **)ptr = PyString_AsString(value);
Py_INCREF(value);
return value;
} else if (PyUnicode_Check(value)) {
@@ -1378,8 +1382,7 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size)
conversion_mode_errors);
if (str == NULL)
return NULL;
- assert(PyBytes_Check(str));
- *(char **)ptr = PyBytes_AS_STRING(str);
+ *(char **)ptr = PyString_AS_STRING(str);
return str;
} else if (PyInt_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
@@ -1433,7 +1436,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
Py_INCREF(Py_None);
return Py_None;
}
- if (PyBytes_Check(value)) {
+ if (PyString_Check(value)) {
value = PyUnicode_FromEncodedObject(value,
conversion_mode_encoding,
conversion_mode_errors);
@@ -1516,7 +1519,7 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)
/* convert value into a PyUnicodeObject or NULL */
if (Py_None == value) {
value = NULL;
- } else if (PyBytes_Check(value)) {
+ } else if (PyString_Check(value)) {
value = PyUnicode_FromEncodedObject(value,
conversion_mode_encoding,
conversion_mode_errors);