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

Skip to content

Commit 4f06d60

Browse files
Issue #22161: Conformed arguments type checks in ctype to actually supported
types. Corrected error messages about bytes arguments.
1 parent e4936b8 commit 4f06d60

6 files changed

Lines changed: 30 additions & 28 deletions

File tree

Lib/ctypes/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def create_string_buffer(init, size=None):
4949
create_string_buffer(anInteger) -> character array
5050
create_string_buffer(aString, anInteger) -> character array
5151
"""
52-
if isinstance(init, (str, bytes)):
52+
if isinstance(init, bytes):
5353
if size is None:
5454
size = len(init)+1
5555
buftype = c_char * size
@@ -284,7 +284,7 @@ def create_unicode_buffer(init, size=None):
284284
create_unicode_buffer(anInteger) -> character array
285285
create_unicode_buffer(aString, anInteger) -> character array
286286
"""
287-
if isinstance(init, (str, bytes)):
287+
if isinstance(init, str):
288288
if size is None:
289289
size = len(init)+1
290290
buftype = c_wchar * size

Lib/ctypes/test/test_buffers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def test_buffer(self):
2121
self.assertEqual(b[::2], b"ac")
2222
self.assertEqual(b[::5], b"a")
2323

24+
self.assertRaises(TypeError, create_string_buffer, "abc")
25+
2426
def test_buffer_interface(self):
2527
self.assertEqual(len(bytearray(create_string_buffer(0))), 0)
2628
self.assertEqual(len(bytearray(create_string_buffer(1))), 1)
@@ -43,6 +45,8 @@ def test_unicode_buffer(self):
4345
self.assertEqual(b[::2], "ac")
4446
self.assertEqual(b[::5], "a")
4547

48+
self.assertRaises(TypeError, create_unicode_buffer, b"abc")
49+
4650
@need_symbol('c_wchar')
4751
def test_unicode_conversion(self):
4852
b = create_unicode_buffer("abc")

Lib/ctypes/test/test_bytes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,40 @@
66
class BytesTest(unittest.TestCase):
77
def test_c_char(self):
88
x = c_char(b"x")
9+
self.assertRaises(TypeError, c_char, "x")
910
x.value = b"y"
11+
with self.assertRaises(TypeError):
12+
x.value = "y"
1013
c_char.from_param(b"x")
14+
self.assertRaises(TypeError, c_char.from_param, "x")
1115
(c_char * 3)(b"a", b"b", b"c")
16+
self.assertRaises(TypeError, c_char * 3, "a", "b", "c")
1217

1318
def test_c_wchar(self):
1419
x = c_wchar("x")
20+
self.assertRaises(TypeError, c_wchar, b"x")
1521
x.value = "y"
22+
with self.assertRaises(TypeError):
23+
x.value = b"y"
1624
c_wchar.from_param("x")
25+
self.assertRaises(TypeError, c_wchar.from_param, b"x")
1726
(c_wchar * 3)("a", "b", "c")
27+
self.assertRaises(TypeError, c_wchar * 3, b"a", b"b", b"c")
1828

1929
def test_c_char_p(self):
2030
c_char_p(b"foo bar")
31+
self.assertRaises(TypeError, c_char_p, "foo bar")
2132

2233
def test_c_wchar_p(self):
2334
c_wchar_p("foo bar")
35+
self.assertRaises(TypeError, c_wchar_p, b"foo bar")
2436

2537
def test_struct(self):
2638
class X(Structure):
2739
_fields_ = [("a", c_char * 3)]
2840

2941
x = X(b"abc")
42+
self.assertRaises(TypeError, X, "abc")
3043
self.assertEqual(x.a, b"abc")
3144
self.assertEqual(type(x.a), bytes)
3245

@@ -35,6 +48,7 @@ class X(Structure):
3548
_fields_ = [("a", c_wchar * 3)]
3649

3750
x = X("abc")
51+
self.assertRaises(TypeError, X, b"abc")
3852
self.assertEqual(x.a, "abc")
3953
self.assertEqual(type(x.a), str)
4054

@@ -46,5 +60,6 @@ class BSTR(_SimpleCData):
4660

4761
BSTR("abc")
4862

63+
4964
if __name__ == '__main__':
5065
unittest.main()

Lib/ctypes/test/test_structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class Person(Structure):
322322
self.assertEqual(cls, RuntimeError)
323323
self.assertEqual(msg,
324324
"(Phone) <class 'TypeError'>: "
325-
"expected string, int found")
325+
"expected bytes, int found")
326326

327327
cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c"))
328328
self.assertEqual(cls, RuntimeError)

Modules/_ctypes/_ctypes.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
10801080
ptr = view.buf;
10811081
if (size > self->b_size) {
10821082
PyErr_SetString(PyExc_ValueError,
1083-
"string too long");
1083+
"byte string too long");
10841084
goto fail;
10851085
}
10861086

@@ -1132,7 +1132,7 @@ CharArray_set_value(CDataObject *self, PyObject *value)
11321132
size = PyBytes_GET_SIZE(value);
11331133
if (size > self->b_size) {
11341134
PyErr_SetString(PyExc_ValueError,
1135-
"string too long");
1135+
"byte string too long");
11361136
Py_DECREF(value);
11371137
return -1;
11381138
}
@@ -1471,7 +1471,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
14711471
Py_INCREF(Py_None);
14721472
return Py_None;
14731473
}
1474-
if (PyUnicode_Check(value) || PyBytes_Check(value)) {
1474+
if (PyUnicode_Check(value)) {
14751475
PyCArgObject *parg;
14761476
struct fielddesc *fd = _ctypes_get_fielddesc("Z");
14771477

@@ -1623,25 +1623,8 @@ c_void_p_from_param(PyObject *type, PyObject *value)
16231623
return (PyObject *)parg;
16241624
}
16251625
/* XXX struni: remove later */
1626-
/* string */
1627-
if (PyBytes_Check(value)) {
1628-
PyCArgObject *parg;
1629-
struct fielddesc *fd = _ctypes_get_fielddesc("z");
1630-
1631-
parg = PyCArgObject_new();
1632-
if (parg == NULL)
1633-
return NULL;
1634-
parg->pffi_type = &ffi_type_pointer;
1635-
parg->tag = 'z';
1636-
parg->obj = fd->setfunc(&parg->value, value, 0);
1637-
if (parg->obj == NULL) {
1638-
Py_DECREF(parg);
1639-
return NULL;
1640-
}
1641-
return (PyObject *)parg;
1642-
}
16431626
/* bytes */
1644-
if (PyByteArray_Check(value)) {
1627+
if (PyBytes_Check(value)) {
16451628
PyCArgObject *parg;
16461629
struct fielddesc *fd = _ctypes_get_fielddesc("z");
16471630

@@ -3218,7 +3201,7 @@ _get_name(PyObject *obj, char **pname)
32183201
return *pname ? 1 : 0;
32193202
}
32203203
PyErr_SetString(PyExc_TypeError,
3221-
"function name must be string or integer");
3204+
"function name must be string, bytes object or integer");
32223205
return 0;
32233206
}
32243207

Modules/_ctypes/cfield.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
11601160
}
11611161
error:
11621162
PyErr_Format(PyExc_TypeError,
1163-
"one character string expected");
1163+
"one character bytes, bytearray or integer expected");
11641164
return NULL;
11651165
}
11661166

@@ -1295,7 +1295,7 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
12951295
Py_INCREF(value);
12961296
} else {
12971297
PyErr_Format(PyExc_TypeError,
1298-
"expected string, %s found",
1298+
"expected bytes, %s found",
12991299
value->ob_type->tp_name);
13001300
return NULL;
13011301
}
@@ -1311,7 +1311,7 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
13111311
++size;
13121312
} else if (size > length) {
13131313
PyErr_Format(PyExc_ValueError,
1314-
"string too long (%zd, maximum length %zd)",
1314+
"bytes too long (%zd, maximum length %zd)",
13151315
size, length);
13161316
Py_DECREF(value);
13171317
return NULL;

0 commit comments

Comments
 (0)