File: test_bytes.py

package info (click to toggle)
python3.2 3.2.3-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 62,476 kB
  • sloc: python: 344,518; ansic: 315,782; sh: 11,910; asm: 10,846; makefile: 3,564; objc: 775; cpp: 432; exp: 416; xml: 73
file content (50 lines) | stat: -rw-r--r-- 1,190 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Test where byte objects are accepted"""
import unittest
import sys
from ctypes import *

class BytesTest(unittest.TestCase):
    def test_c_char(self):
        x = c_char(b"x")
        x.value = b"y"
        c_char.from_param(b"x")
        (c_char * 3)(b"a", b"b", b"c")

    def test_c_wchar(self):
        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(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")
        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.assertEqual(x.a, "abc")
        self.assertEqual(type(x.a), str)

    if sys.platform == "win32":
        def test_BSTR(self):
            from _ctypes import _SimpleCData
            class BSTR(_SimpleCData):
                _type_ = "X"

            BSTR("abc")

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