From 42746df17bd1f147f1bea90216ed9848efc730c8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 27 Jul 2010 23:36:41 +0000 Subject: [PATCH] Fix ctypes tests to avoid implicit bytes-unicode conversion --- Lib/ctypes/test/test_arrays.py | 4 ++-- Lib/ctypes/test/test_bitfields.py | 4 ++-- Lib/ctypes/test/test_buffers.py | 2 +- Lib/ctypes/test/test_bytes.py | 12 ++++++------ Lib/ctypes/test/test_cast.py | 8 ++++---- Lib/ctypes/test/test_cfuncs.py | 2 +- Lib/ctypes/test/test_errno.py | 4 ++-- Lib/ctypes/test/test_internals.py | 10 ++++------ Lib/ctypes/test/test_keeprefs.py | 8 ++++---- Lib/ctypes/test/test_libc.py | 2 +- Lib/ctypes/test/test_objects.py | 4 ++-- Lib/ctypes/test/test_parameters.py | 4 +--- Lib/ctypes/test/test_prototypes.py | 2 +- Lib/ctypes/test/test_python_api.py | 4 ++-- Lib/ctypes/test/test_random_things.py | 2 +- Lib/ctypes/test/test_repr.py | 4 ++-- Lib/ctypes/test/test_returnfuncptrs.py | 8 ++++---- Lib/ctypes/test/test_stringptr.py | 6 +++--- Lib/ctypes/test/test_strings.py | 10 +++++----- Lib/ctypes/test/test_structures.py | 18 +++++++++--------- 20 files changed, 57 insertions(+), 61 deletions(-) diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py index c0cdfd5b785..1a6f3b2a655 100644 --- a/Lib/ctypes/test/test_arrays.py +++ b/Lib/ctypes/test/test_arrays.py @@ -42,7 +42,7 @@ def test_simple(self): CharArray = ARRAY(c_char, 3) - ca = CharArray("a", "b", "c") + ca = CharArray(b"a", b"b", b"c") # Should this work? It doesn't: # CharArray("abc") @@ -89,7 +89,7 @@ def test_classcache(self): def test_from_address(self): # Failed with 0.9.8, reported by JUrner - p = create_string_buffer("foo") + p = create_string_buffer(b"foo") sz = (c_char * 3).from_address(addressof(p)) self.assertEqual(sz[:], b"foo") self.assertEqual(sz[::], b"foo") diff --git a/Lib/ctypes/test/test_bitfields.py b/Lib/ctypes/test/test_bitfields.py index 4eb9571f650..9e0825c7cac 100644 --- a/Lib/ctypes/test/test_bitfields.py +++ b/Lib/ctypes/test/test_bitfields.py @@ -37,14 +37,14 @@ def test_ints(self): for name in "ABCDEFGHI": b = BITS() setattr(b, name, i) - self.assertEqual((name, i, getattr(b, name)), (name, i, func(byref(b), name))) + self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii'))) def test_shorts(self): for i in range(256): for name in "MNOPQRS": b = BITS() setattr(b, name, i) - self.assertEqual((name, i, getattr(b, name)), (name, i, func(byref(b), name))) + self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii'))) signed_int_types = (c_byte, c_short, c_int, c_long, c_longlong) unsigned_int_types = (c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong) diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py index 3f8a5870905..40889eaad16 100644 --- a/Lib/ctypes/test/test_buffers.py +++ b/Lib/ctypes/test/test_buffers.py @@ -9,7 +9,7 @@ def test_buffer(self): self.assertEqual(sizeof(b), 32 * sizeof(c_char)) self.assertTrue(type(b[0]) is bytes) - b = create_string_buffer("abc") + b = create_string_buffer(b"abc") self.assertEqual(len(b), 4) # trailing nul char self.assertEqual(sizeof(b), 4 * sizeof(c_char)) self.assertTrue(type(b[0]) is bytes) diff --git a/Lib/ctypes/test/test_bytes.py b/Lib/ctypes/test/test_bytes.py index 4a1d0fd71ae..5386302017e 100644 --- a/Lib/ctypes/test/test_bytes.py +++ b/Lib/ctypes/test/test_bytes.py @@ -11,10 +11,10 @@ def test_c_char(self): (c_char * 3)(b"a", b"b", b"c") def test_c_wchar(self): - x = c_wchar(b"x") - x.value = b"y" - c_wchar.from_param(b"x") - (c_wchar * 3)(b"a", b"b", b"c") + x = c_wchar("x") + x.value = "y" + c_wchar.from_param("x") + (c_wchar * 3)("a", "b", "c") def test_c_char_p(self): c_char_p("foo bar") @@ -37,8 +37,8 @@ def test_struct_W(self): class X(Structure): _fields_ = [("a", c_wchar * 3)] - X("abc") - x = X(b"abc") + X(b"abc") + x = X("abc") self.assertEqual(x.a, "abc") self.assertEqual(type(x.a), str) diff --git a/Lib/ctypes/test/test_cast.py b/Lib/ctypes/test/test_cast.py index 4c4a2e85458..702de3cffa7 100644 --- a/Lib/ctypes/test/test_cast.py +++ b/Lib/ctypes/test/test_cast.py @@ -33,17 +33,17 @@ def test_address2pointer(self): def test_p2a_objects(self): array = (c_char_p * 5)() self.assertEqual(array._objects, None) - array[0] = "foo bar" + array[0] = b"foo bar" self.assertEqual(array._objects, {'0': b"foo bar"}) p = cast(array, POINTER(c_char_p)) # array and p share a common _objects attribute self.assertTrue(p._objects is array._objects) self.assertEqual(array._objects, {'0': b"foo bar", id(array): array}) - p[0] = "spam spam" + p[0] = b"spam spam" self.assertEqual(p._objects, {'0': b"spam spam", id(array): array}) self.assertTrue(array._objects is p._objects) - p[1] = "foo bar" + p[1] = b"foo bar" self.assertEqual(p._objects, {'1': b'foo bar', '0': b"spam spam", id(array): array}) self.assertTrue(array._objects is p._objects) @@ -71,7 +71,7 @@ def test_other(self): def test_char_p(self): # This didn't work: bad argument to internal function - s = c_char_p("hiho") + s = c_char_p(b"hiho") self.assertEqual(cast(cast(s, c_void_p), c_char_p).value, b"hiho") diff --git a/Lib/ctypes/test/test_cfuncs.py b/Lib/ctypes/test/test_cfuncs.py index 493cbe979ce..f4bd3b16c37 100644 --- a/Lib/ctypes/test/test_cfuncs.py +++ b/Lib/ctypes/test/test_cfuncs.py @@ -107,7 +107,7 @@ def test_ulong(self): def test_ulong_plus(self): self._dll.tf_bL.restype = c_ulong self._dll.tf_bL.argtypes = (c_char, c_ulong) - self.assertEqual(self._dll.tf_bL(' ', 4294967295), 1431655765) + self.assertEqual(self._dll.tf_bL(b' ', 4294967295), 1431655765) self.assertEqual(self.U(), 4294967295) def test_longlong(self): diff --git a/Lib/ctypes/test/test_errno.py b/Lib/ctypes/test/test_errno.py index 5695c1754c5..4690a0d8717 100644 --- a/Lib/ctypes/test/test_errno.py +++ b/Lib/ctypes/test/test_errno.py @@ -19,7 +19,7 @@ def test_open(self): libc_open.argtypes = c_char_p, c_int - self.assertEqual(libc_open("", 0), -1) + self.assertEqual(libc_open(b"", 0), -1) self.assertEqual(get_errno(), errno.ENOENT) self.assertEqual(set_errno(32), errno.ENOENT) @@ -35,7 +35,7 @@ def _worker(): else: libc_open = libc.open libc_open.argtypes = c_char_p, c_int - self.assertEqual(libc_open("", 0), -1) + self.assertEqual(libc_open(b"", 0), -1) self.assertEqual(get_errno(), 0) t = threading.Thread(target=_worker) diff --git a/Lib/ctypes/test/test_internals.py b/Lib/ctypes/test/test_internals.py index 1c7e4a80975..cbf2e0589a2 100644 --- a/Lib/ctypes/test/test_internals.py +++ b/Lib/ctypes/test/test_internals.py @@ -70,19 +70,17 @@ class X(Structure): class Y(Structure): _fields_ = [("x", X), ("y", X)] - s1 = "Hello, World" - s2 = "Hallo, Welt" + s1 = b"Hello, World" + s2 = b"Hallo, Welt" x = X() x.a = s1 x.b = s2 - self.assertEqual(x._objects, {"0": bytes(s1, "ascii"), - "1": bytes(s2, "ascii")}) + self.assertEqual(x._objects, {"0": s1, "1": s2}) y = Y() y.x = x - self.assertEqual(y._objects, {"0": {"0": bytes(s1, "ascii"), - "1": bytes(s2, "ascii")}}) + self.assertEqual(y._objects, {"0": {"0": s1, "1": s2}}) ## x = y.x ## del y ## print x._b_base_._objects diff --git a/Lib/ctypes/test/test_keeprefs.py b/Lib/ctypes/test/test_keeprefs.py index b90765a2998..ef3e6ebb183 100644 --- a/Lib/ctypes/test/test_keeprefs.py +++ b/Lib/ctypes/test/test_keeprefs.py @@ -13,9 +13,9 @@ def test_cint(self): def test_ccharp(self): x = c_char_p() self.assertEquals(x._objects, None) - x.value = "abc" + x.value = b"abc" self.assertEquals(x._objects, b"abc") - x = c_char_p("spam") + x = c_char_p(b"spam") self.assertEquals(x._objects, b"spam") class StructureTestCase(unittest.TestCase): @@ -37,8 +37,8 @@ class X(Structure): x = X() self.assertEquals(x._objects, None) - x.a = "spam" - x.b = "foo" + x.a = b"spam" + x.b = b"foo" self.assertEquals(x._objects, {"0": b"spam", "1": b"foo"}) def test_struct_struct(self): diff --git a/Lib/ctypes/test/test_libc.py b/Lib/ctypes/test/test_libc.py index cce409fa1be..56285b5ff81 100644 --- a/Lib/ctypes/test/test_libc.py +++ b/Lib/ctypes/test/test_libc.py @@ -25,7 +25,7 @@ def test_qsort(self): def sort(a, b): return three_way_cmp(a[0], b[0]) - chars = create_string_buffer("spam, spam, and spam") + chars = create_string_buffer(b"spam, spam, and spam") lib.my_qsort(chars, len(chars)-1, sizeof(c_char), comparefunc(sort)) self.assertEqual(chars.raw, b" ,,aaaadmmmnpppsss\x00") diff --git a/Lib/ctypes/test/test_objects.py b/Lib/ctypes/test/test_objects.py index 750d9045ceb..f075c208939 100644 --- a/Lib/ctypes/test/test_objects.py +++ b/Lib/ctypes/test/test_objects.py @@ -20,7 +20,7 @@ The memory block stores pointers to strings, and the strings itself assigned from Python must be kept. ->>> array[4] = 'foo bar' +>>> array[4] = b'foo bar' >>> array._objects {'4': b'foo bar'} >>> array[4] @@ -45,7 +45,7 @@ >>> ->>> x.array[0] = 'spam spam spam' +>>> x.array[0] = b'spam spam spam' >>> x._objects {'0:2': b'spam spam spam'} >>> x.array._b_base_._objects diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py index 6a3f33d19c6..468c4495cdd 100644 --- a/Lib/ctypes/test/test_parameters.py +++ b/Lib/ctypes/test/test_parameters.py @@ -19,7 +19,6 @@ def tearDown(self): else: set_conversion_mode(*self.prev_conv_mode) - def test_subclasses(self): from ctypes import c_void_p, c_char_p # ctypes 0.9.5 and before did overwrite from_param in SimpleType_new @@ -60,12 +59,11 @@ def test_cstrings(self): # new in 0.9.1: convert (encode) unicode to ascii self.assertEqual(c_char_p.from_param("123")._obj, b"123") self.assertRaises(UnicodeEncodeError, c_char_p.from_param, "123\377") - self.assertRaises(TypeError, c_char_p.from_param, 42) # calling c_char_p.from_param with a c_char_p instance # returns the argument itself: - a = c_char_p("123") + a = c_char_p(b"123") self.assertTrue(c_char_p.from_param(a) is a) def test_cw_strings(self): diff --git a/Lib/ctypes/test/test_prototypes.py b/Lib/ctypes/test/test_prototypes.py index d2e4c0b19d0..6ef1b1bcaff 100644 --- a/Lib/ctypes/test/test_prototypes.py +++ b/Lib/ctypes/test/test_prototypes.py @@ -127,7 +127,7 @@ def test_c_void_p_arg(self): self.assertEqual(None, func(c_char_p(None))) self.assertEqual(b"123", func(c_buffer(b"123"))) - ca = c_char("a") + ca = c_char(b"a") self.assertEqual(ord(b"a"), func(pointer(ca))[0]) self.assertEqual(ord(b"a"), func(byref(ca))[0]) diff --git a/Lib/ctypes/test/test_python_api.py b/Lib/ctypes/test/test_python_api.py index b74767a4f37..1f4c6039dcb 100644 --- a/Lib/ctypes/test/test_python_api.py +++ b/Lib/ctypes/test/test_python_api.py @@ -72,10 +72,10 @@ def test_PyOS_snprintf(self): PyOS_snprintf.argtypes = POINTER(c_char), c_size_t, c_char_p buf = c_buffer(256) - PyOS_snprintf(buf, sizeof(buf), "Hello from %s", b"ctypes") + PyOS_snprintf(buf, sizeof(buf), b"Hello from %s", b"ctypes") self.assertEqual(buf.value, b"Hello from ctypes") - PyOS_snprintf(buf, sizeof(buf), "Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3) + PyOS_snprintf(buf, sizeof(buf), b"Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3) self.assertEqual(buf.value, b"Hello from ctypes (1, 2, 3)") # not enough arguments diff --git a/Lib/ctypes/test/test_random_things.py b/Lib/ctypes/test/test_random_things.py index 7bb9db80490..4e9f08fb70b 100644 --- a/Lib/ctypes/test/test_random_things.py +++ b/Lib/ctypes/test/test_random_things.py @@ -66,7 +66,7 @@ def test_FloatDivisionError(self): def test_TypeErrorDivisionError(self): cb = CFUNCTYPE(c_int, c_char_p)(callback_func) - out = self.capture_stderr(cb, "spam") + out = self.capture_stderr(cb, b"spam") self.assertEqual(out.splitlines()[-1], "TypeError: " "unsupported operand type(s) for /: 'int' and 'bytes'") diff --git a/Lib/ctypes/test/test_repr.py b/Lib/ctypes/test/test_repr.py index 9a1e2386e65..60a2c803453 100644 --- a/Lib/ctypes/test/test_repr.py +++ b/Lib/ctypes/test/test_repr.py @@ -22,8 +22,8 @@ def test_numbers(self): self.assertEqual(": " "expected string, int found") - cls, msg = self.get_except(Person, "Someone", ("a", "b", "c")) + cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c")) self.assertEqual(cls, RuntimeError) if issubclass(Exception, object): self.assertEqual(msg,