cpython: 06cf4044a11a (original) (raw)
Mercurial > cpython
changeset 92049:06cf4044a11a
Issue #22161: Conformed arguments type checks in ctype to actually supported types. Corrected error messages about bytes arguments. [#22161]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Sat, 09 Aug 2014 09:34:25 +0300 |
parents | e205bce4cc0a(current diff)034a5f15561d(diff) |
children | 0fef0afb9d19 |
files | Lib/ctypes/__init__.py |
diffstat | 6 files changed, 30 insertions(+), 28 deletions(-)[+] [-] Lib/ctypes/__init__.py 4 Lib/ctypes/test/test_buffers.py 4 Lib/ctypes/test/test_bytes.py 15 Lib/ctypes/test/test_structures.py 2 Modules/_ctypes/_ctypes.c 27 Modules/_ctypes/cfield.c 6 |
line wrap: on
line diff
--- a/Lib/ctypes/init.py +++ b/Lib/ctypes/init.py @@ -49,7 +49,7 @@ def create_string_buffer(init, size=None create_string_buffer(anInteger) -> character array create_string_buffer(aString, anInteger) -> character array """
@@ -284,7 +284,7 @@ def create_unicode_buffer(init, size=Non create_unicode_buffer(anInteger) -> character array create_unicode_buffer(aString, anInteger) -> character array """
--- a/Lib/ctypes/test/test_buffers.py +++ b/Lib/ctypes/test/test_buffers.py @@ -21,6 +21,8 @@ class StringBufferTestCase(unittest.Test self.assertEqual(b[::2], b"ac") self.assertEqual(b[::5], b"a")
self.assertRaises(TypeError, create_string_buffer, "abc")[](#l2.7)
+ def test_buffer_interface(self): self.assertEqual(len(bytearray(create_string_buffer(0))), 0) self.assertEqual(len(bytearray(create_string_buffer(1))), 1) @@ -43,6 +45,8 @@ class StringBufferTestCase(unittest.Test self.assertEqual(b[::2], "ac") self.assertEqual(b[::5], "a")
self.assertRaises(TypeError, create_unicode_buffer, b"abc")[](#l2.16)
+ @need_symbol('c_wchar') def test_unicode_conversion(self): b = create_unicode_buffer("abc")
--- a/Lib/ctypes/test/test_bytes.py +++ b/Lib/ctypes/test/test_bytes.py @@ -6,27 +6,40 @@ from ctypes import * class BytesTest(unittest.TestCase): def test_c_char(self): x = c_char(b"x")
self.assertRaises(TypeError, c_char, "x")[](#l3.7) x.value = b"y"[](#l3.8)
with self.assertRaises(TypeError):[](#l3.9)
x.value = "y"[](#l3.10) c_char.from_param(b"x")[](#l3.11)
self.assertRaises(TypeError, c_char.from_param, "x")[](#l3.12) (c_char * 3)(b"a", b"b", b"c")[](#l3.13)
self.assertRaises(TypeError, c_char * 3, "a", "b", "c")[](#l3.14)
def test_c_wchar(self): x = c_wchar("x")
self.assertRaises(TypeError, c_wchar, b"x")[](#l3.18) x.value = "y"[](#l3.19)
with self.assertRaises(TypeError):[](#l3.20)
x.value = b"y"[](#l3.21) c_wchar.from_param("x")[](#l3.22)
self.assertRaises(TypeError, c_wchar.from_param, b"x")[](#l3.23) (c_wchar * 3)("a", "b", "c")[](#l3.24)
self.assertRaises(TypeError, c_wchar * 3, b"a", b"b", b"c")[](#l3.25)
def test_c_char_p(self): c_char_p(b"foo bar")
self.assertRaises(TypeError, c_char_p, "foo bar")[](#l3.29)
def test_c_wchar_p(self): c_wchar_p("foo bar")
self.assertRaises(TypeError, c_wchar_p, b"foo bar")[](#l3.33)
def test_struct(self): class X(Structure): fields = [("a", c_char * 3)] x = X(b"abc")
self.assertRaises(TypeError, X, "abc")[](#l3.40) self.assertEqual(x.a, b"abc")[](#l3.41) self.assertEqual(type(x.a), bytes)[](#l3.42)
@@ -35,6 +48,7 @@ class BytesTest(unittest.TestCase): fields = [("a", c_wchar * 3)] x = X("abc")
self.assertRaises(TypeError, X, b"abc")[](#l3.48) self.assertEqual(x.a, "abc")[](#l3.49) self.assertEqual(type(x.a), str)[](#l3.50)
@@ -46,5 +60,6 @@ class BytesTest(unittest.TestCase): BSTR("abc") + if name == 'main': unittest.main()
--- a/Lib/ctypes/test/test_structures.py +++ b/Lib/ctypes/test/test_structures.py @@ -322,7 +322,7 @@ class StructureTestCase(unittest.TestCas self.assertEqual(cls, RuntimeError) self.assertEqual(msg, "(Phone) <class 'TypeError'>: "
"expected string, int found")[](#l4.7)
"expected bytes, int found")[](#l4.8)
cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c")) self.assertEqual(cls, RuntimeError)
--- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -1080,7 +1080,7 @@ CharArray_set_raw(CDataObject *self, PyO ptr = view.buf; if (size > self->b_size) { PyErr_SetString(PyExc_ValueError,
"string too long");[](#l5.7)
} @@ -1132,7 +1132,7 @@ CharArray_set_value(CDataObject *self, P size = PyBytes_GET_SIZE(value); if (size > self->b_size) { PyErr_SetString(PyExc_ValueError,"byte string too long");[](#l5.8) goto fail;[](#l5.9)
"string too long");[](#l5.16)
} @@ -1471,7 +1471,7 @@ c_wchar_p_from_param(PyObject *type, PyO Py_INCREF(Py_None); return Py_None; }"byte string too long");[](#l5.17) Py_DECREF(value);[](#l5.18) return -1;[](#l5.19)
- if (PyUnicode_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = _ctypes_get_fielddesc("Z");
@@ -1623,7 +1623,7 @@ c_void_p_from_param(PyObject *type, PyOb return (PyObject )parg; } / XXX struni: remove later / -/ string / +/ bytes */ if (PyBytes_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = _ctypes_get_fielddesc("z"); @@ -1640,23 +1640,6 @@ c_void_p_from_param(PyObject *type, PyOb } return (PyObject )parg; } -/ bytes */
- if (PyByteArray_Check(value)) {
PyCArgObject *parg;[](#l5.45)
struct fielddesc *fd = _ctypes_get_fielddesc("z");[](#l5.46)
parg = PyCArgObject_new();[](#l5.48)
if (parg == NULL)[](#l5.49)
return NULL;[](#l5.50)
parg->pffi_type = &ffi_type_pointer;[](#l5.51)
parg->tag = 'z';[](#l5.52)
parg->obj = fd->setfunc(&parg->value, value, 0);[](#l5.53)
if (parg->obj == NULL) {[](#l5.54)
Py_DECREF(parg);[](#l5.55)
return NULL;[](#l5.56)
}[](#l5.57)
return (PyObject *)parg;[](#l5.58)
- }
/* unicode */ if (PyUnicode_Check(value)) { PyCArgObject *parg; @@ -3218,7 +3201,7 @@ static int return *pname ? 1 : 0; } PyErr_SetString(PyExc_TypeError,
"function name must be string or integer");[](#l5.67)
--- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -1160,7 +1160,7 @@ c_set(void *ptr, PyObject *value, Py_ssi } error: PyErr_Format(PyExc_TypeError,
"one character string expected");[](#l6.7)
return NULL; } @@ -1295,7 +1295,7 @@ s_set(void *ptr, PyObject *value, Py_ssi Py_INCREF(value); } else { PyErr_Format(PyExc_TypeError,"one character bytes, bytearray or integer expected");[](#l6.8)
"expected string, %s found",[](#l6.16)
} @@ -1311,7 +1311,7 @@ s_set(void *ptr, PyObject *value, Py_ssi ++size; } else if (size > length) { PyErr_Format(PyExc_ValueError,"expected bytes, %s found",[](#l6.17) value->ob_type->tp_name);[](#l6.18) return NULL;[](#l6.19)
"string too long (%zd, maximum length %zd)",[](#l6.25)
"bytes too long (%zd, maximum length %zd)",[](#l6.26) size, length);[](#l6.27) Py_DECREF(value);[](#l6.28) return NULL;[](#l6.29)