Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19b5254

Browse files
author
Thomas Heller
committed
c_char, c_char_p objects and c_char array structure fields return
their value now as str, no longer str8.
1 parent 27384da commit 19b5254

7 files changed

Lines changed: 29 additions & 40 deletions

File tree

Lib/ctypes/test/test_buffers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ def test_buffer(self):
77
b = create_string_buffer(32)
88
self.failUnlessEqual(len(b), 32)
99
self.failUnlessEqual(sizeof(b), 32 * sizeof(c_char))
10-
self.failUnless(type(b[0]) is str8)
10+
self.failUnless(type(b[0]) is str)
1111

1212
b = create_string_buffer("abc")
1313
self.failUnlessEqual(len(b), 4) # trailing nul char
1414
self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
15-
self.failUnless(type(b[0]) is str8)
15+
self.failUnless(type(b[0]) is str)
1616
self.failUnlessEqual(b[0], "a")
1717
self.failUnlessEqual(b[:], "abc\0")
1818

1919
def test_string_conversion(self):
2020
b = create_string_buffer("abc")
2121
self.failUnlessEqual(len(b), 4) # trailing nul char
2222
self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
23-
self.failUnless(type(b[0]) is str8)
23+
self.failUnless(type(b[0]) is str)
2424
self.failUnlessEqual(b[0], "a")
2525
self.failUnlessEqual(b[:], "abc\0")
2626

Lib/ctypes/test/test_bytes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ class X(Structure):
2929
_fields_ = [("a", c_char * 3)]
3030

3131
X("abc")
32-
X(b"abc")
32+
x = X(b"abc")
33+
self.assertEqual(x.a, "abc")
34+
self.assertEqual(type(x.a), str)
3335

3436
def test_struct_W(self):
3537
class X(Structure):
3638
_fields_ = [("a", c_wchar * 3)]
3739

3840
X("abc")
39-
X(b"abc")
41+
x = X(b"abc")
42+
self.assertEqual(x.a, "abc")
43+
self.assertEqual(type(x.a), str)
4044

4145
if sys.platform == "win32":
4246
def test_BSTR(self):

Lib/ctypes/test/test_objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
>>> array._objects
2525
{'4': b'foo bar'}
2626
>>> array[4]
27-
s'foo bar'
27+
'foo bar'
2828
>>>
2929
3030
It gets more complicated when the ctypes instance itself is contained

Lib/ctypes/test/test_random_things.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_TypeErrorDivisionError(self):
6969
out = self.capture_stderr(cb, "spam")
7070
self.failUnlessEqual(out.splitlines()[-1],
7171
"TypeError: "
72-
"unsupported operand type(s) for /: 'int' and 'str8'")
72+
"unsupported operand type(s) for /: 'int' and 'str'")
7373

7474
if __name__ == '__main__':
7575
unittest.main()

Lib/ctypes/test/test_repr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_numbers(self):
2222
self.failUnlessEqual("<X object at", repr(typ(42))[:12])
2323

2424
def test_char(self):
25-
self.failUnlessEqual("c_char(s'x')", repr(c_char('x')))
25+
self.failUnlessEqual("c_char('x')", repr(c_char('x')))
2626
self.failUnlessEqual("<X object at", repr(X('x'))[:12])
2727

2828
if __name__ == "__main__":

Lib/ctypes/test/test_slicing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def errcheck(result, func, args):
7070
dll.my_strdup.errcheck = errcheck
7171
try:
7272
res = dll.my_strdup(s)
73-
self.failUnlessEqual(res, s)
73+
self.failUnlessEqual(res, str(s))
7474
finally:
7575
del dll.my_strdup.errcheck
7676

Modules/_ctypes/cfield.c

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
11371137
return NULL;
11381138
if (PyBytes_GET_SIZE(value) != 1) {
11391139
Py_DECREF(value);
1140-
PyErr_Format(PyExc_TypeError,
1141-
"one character string expected");
1142-
return NULL;
1140+
goto error;
11431141
}
11441142
*(char *)ptr = PyBytes_AsString(value)[0];
11451143
Py_DECREF(value);
@@ -1149,22 +1147,17 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
11491147
*(char *)ptr = PyBytes_AsString(value)[0];
11501148
_RET(value);
11511149
}
1152-
/* XXX struni remove later */
1153-
if (!PyString_Check(value) || (1 != PyString_Size(value))) {
1154-
PyErr_Format(PyExc_TypeError,
1155-
"one character string expected");
1156-
return NULL;
1157-
}
1158-
*(char *)ptr = PyString_AS_STRING(value)[0];
1159-
_RET(value);
1150+
error:
1151+
PyErr_Format(PyExc_TypeError,
1152+
"one character string expected");
1153+
return NULL;
11601154
}
11611155

11621156

11631157
static PyObject *
11641158
c_get(void *ptr, Py_ssize_t size)
11651159
{
1166-
/* XXX struni return PyBytes (or PyUnicode?) later */
1167-
return PyString_FromStringAndSize((char *)ptr, 1);
1160+
return PyUnicode_FromStringAndSize((char *)ptr, 1);
11681161
}
11691162

11701163
#ifdef CTYPES_UNICODE
@@ -1280,24 +1273,16 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
12801273
static PyObject *
12811274
s_get(void *ptr, Py_ssize_t size)
12821275
{
1283-
PyObject *result;
1284-
size_t slen;
1276+
Py_ssize_t i;
1277+
char *p;
12851278

1286-
result = PyString_FromString((char *)ptr);
1287-
if (!result)
1288-
return NULL;
1289-
/* chop off at the first NUL character, if any.
1290-
* On error, result will be deallocated and set to NULL.
1291-
*/
1292-
slen = strlen(PyString_AS_STRING(result));
1293-
size = min(size, (Py_ssize_t)slen);
1294-
if (result->ob_refcnt == 1) {
1295-
/* shorten the result */
1296-
_PyString_Resize(&result, size);
1297-
return result;
1298-
} else
1299-
/* cannot shorten the result */
1300-
return PyString_FromStringAndSize(ptr, size);
1279+
p = (char *)ptr;
1280+
for (i = 0; i < size; ++i) {
1281+
if (*p++ == '\0')
1282+
break;
1283+
}
1284+
1285+
return PyUnicode_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
13011286
}
13021287

13031288
static PyObject *
@@ -1393,7 +1378,7 @@ z_get(void *ptr, Py_ssize_t size)
13931378
return NULL;
13941379
}
13951380
#endif
1396-
return PyString_FromString(*(char **)ptr);
1381+
return PyUnicode_FromString(*(char **)ptr);
13971382
} else {
13981383
Py_INCREF(Py_None);
13991384
return Py_None;

0 commit comments

Comments
 (0)