test where byte objects are accepted import sys import unittest from ctypes import simplecdata from ctypes import structure cchar ccharp cwchar cwcharp class bytestestunittest testcase def testccharself x ccharbx self assertraisestypeerror cchar x x value by with self assertraisestypeerror x value y cchar fromparambx self assertraisestypeerror cchar fromparam x self assertin xbd reprcchar fromparambxbd cchar 3ba bb bc self assertraisestypeerror cchar 3 a b c def testcwcharself x cwcharx self assertraisestypeerror cwchar bx x value y with self assertraisestypeerror x value by cwchar fromparamx self assertraisestypeerror cwchar fromparam bx cwchar 3a b c self assertraisestypeerror cwchar 3 ba bb bc def testccharpself ccharpbfoo bar self assertraisestypeerror ccharp foo bar def testcwcharpself cwcharpfoo bar self assertraisestypeerror cwcharp bfoo bar def teststructself class xstructure fields a cchar 3 x xbabc self assertraisestypeerror x abc self assertequalx a babc self assertequaltypex a bytes def teststructwself class xstructure fields a cwchar 3 x xabc self assertraisestypeerror x babc self assertequalx a abc self assertequaltypex a str unittest skipunlesssys platform win32 windowsspecific test def testbstrself class bstrsimplecdata type x bstrabc if name main unittest main
import sys
import unittest
from _ctypes import _SimpleCData
from ctypes import Structure, c_char, c_char_p, c_wchar, c_wchar_p


class BytesTest(unittest.TestCase):
    def test_c_char(self):
        x = c_char(b"x")
        self.assertRaises(TypeError, c_char, "x")
        x.value = b"y"
        with self.assertRaises(TypeError):
            x.value = "y"
        c_char.from_param(b"x")
        self.assertRaises(TypeError, c_char.from_param, "x")
        self.assertIn('xbd', repr(c_char.from_param(b"\xbd")))
        (c_char * 3)(b"a", b"b", b"c")
        self.assertRaises(TypeError, c_char * 3, "a", "b", "c")

    def test_c_wchar(self):
        x = c_wchar("x")
        self.assertRaises(TypeError, c_wchar, b"x")
        x.value = "y"
        with self.assertRaises(TypeError):
            x.value = b"y"
        c_wchar.from_param("x")
        self.assertRaises(TypeError, c_wchar.from_param, b"x")
        (c_wchar * 3)("a", "b", "c")
        self.assertRaises(TypeError, c_wchar * 3, b"a", b"b", b"c")

    def test_c_char_p(self):
        c_char_p(b"foo bar")
        self.assertRaises(TypeError, c_char_p, "foo bar")

    def test_c_wchar_p(self):
        c_wchar_p("foo bar")
        self.assertRaises(TypeError, c_wchar_p, b"foo bar")

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

        x = X(b"abc")
        self.assertRaises(TypeError, X, "abc")
        self.assertEqual(x.a, b"abc")
        self.assertEqual(type(x.a), bytes)

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

        x = X("abc")
        self.assertRaises(TypeError, X, b"abc")
        self.assertEqual(x.a, "abc")
        self.assertEqual(type(x.a), str)

    @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
    def test_BSTR(self):
        class BSTR(_SimpleCData):
            _type_ = "X"

        BSTR("abc")


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