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")

+ 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")

+ @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")

def test_c_wchar(self): x = c_wchar("x")

def test_c_char_p(self): c_char_p(b"foo bar")

def test_c_wchar_p(self): c_wchar_p("foo bar")

def test_struct(self): class X(Structure): fields = [("a", c_char * 3)] x = X(b"abc")

@@ -35,6 +48,7 @@ class BytesTest(unittest.TestCase): fields = [("a", c_wchar * 3)] x = X("abc")

@@ -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'>: "

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,

@@ -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 */

-

/* unicode */ if (PyUnicode_Check(value)) { PyCArgObject *parg; @@ -3218,7 +3201,7 @@ static int return *pname ? 1 : 0; } PyErr_SetString(PyExc_TypeError,

--- 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,