Python ctypes.set_conversion_mode() Examples

The following are 30 code examples of ctypes.set_conversion_mode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ctypes , or try the search function .
Example #1
Source File: test_unicode.py    From datafari with Apache License 2.0 6 votes vote down vote up
def test_buffers(self):
        ctypes.set_conversion_mode("ascii", "strict")
        buf = ctypes.create_string_buffer(u"abc")
        self.assertEqual(len(buf), 3+1)

        ctypes.set_conversion_mode("ascii", "replace")
        buf = ctypes.create_string_buffer(u"ab���")
        self.assertEqual(buf[:], "ab???\0")
        self.assertEqual(buf[::], "ab???\0")
        self.assertEqual(buf[::-1], "\0???ba")
        self.assertEqual(buf[::2], "a??")
        self.assertEqual(buf[6:5:-1], "")

        ctypes.set_conversion_mode("ascii", "ignore")
        buf = ctypes.create_string_buffer(u"ab���")
        # is that correct? not sure.  But with 'ignore', you get what you pay for..
        self.assertEqual(buf[:], "ab\0\0\0\0")
        self.assertEqual(buf[::], "ab\0\0\0\0")
        self.assertEqual(buf[::-1], "\0\0\0\0ba") 
Example #2
Source File: test_unicode.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_buffers(self):
            ctypes.set_conversion_mode("ascii", "strict")
            buf = ctypes.create_string_buffer(u"abc")
            self.assertEqual(len(buf), 3+1)

            ctypes.set_conversion_mode("ascii", "replace")
            buf = ctypes.create_string_buffer(u"ab���")
            self.assertEqual(buf[:], "ab???\0")
            self.assertEqual(buf[::], "ab???\0")
            self.assertEqual(buf[::-1], "\0???ba")
            self.assertEqual(buf[::2], "a??")
            self.assertEqual(buf[6:5:-1], "")

            ctypes.set_conversion_mode("ascii", "ignore")
            buf = ctypes.create_string_buffer(u"ab���")
            # is that correct? not sure.  But with 'ignore', you get what you pay for..
            self.assertEqual(buf[:], "ab\0\0\0\0")
            self.assertEqual(buf[::], "ab\0\0\0\0")
            self.assertEqual(buf[::-1], "\0\0\0\0ba") 
Example #3
Source File: test_unicode.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_buffers(self):
        ctypes.set_conversion_mode("ascii", "strict")
        buf = ctypes.create_unicode_buffer("abc")
        self.assertEqual(len(buf), 3+1)

        ctypes.set_conversion_mode("ascii", "replace")
        buf = ctypes.create_unicode_buffer("ab���")
        self.assertEqual(buf[:], u"ab\uFFFD\uFFFD\uFFFD\0")
        self.assertEqual(buf[::], u"ab\uFFFD\uFFFD\uFFFD\0")
        self.assertEqual(buf[::-1], u"\0\uFFFD\uFFFD\uFFFDba")
        self.assertEqual(buf[::2], u"a\uFFFD\uFFFD")
        self.assertEqual(buf[6:5:-1], u"")

        ctypes.set_conversion_mode("ascii", "ignore")
        buf = ctypes.create_unicode_buffer("ab���")
        # is that correct? not sure.  But with 'ignore', you get what you pay for..
        self.assertEqual(buf[:], u"ab\0\0\0\0")
        self.assertEqual(buf[::], u"ab\0\0\0\0")
        self.assertEqual(buf[::-1], u"\0\0\0\0ba")
        self.assertEqual(buf[::2], u"a\0\0")
        self.assertEqual(buf[6:5:-1], u"") 
Example #4
Source File: test_unicode.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_buffers(self):
        ctypes.set_conversion_mode("ascii", "strict")
        buf = ctypes.create_unicode_buffer("abc")
        self.assertEqual(len(buf), 3+1)

        ctypes.set_conversion_mode("ascii", "replace")
        buf = ctypes.create_unicode_buffer("ab���")
        self.assertEqual(buf[:], u"ab\uFFFD\uFFFD\uFFFD\0")
        self.assertEqual(buf[::], u"ab\uFFFD\uFFFD\uFFFD\0")
        self.assertEqual(buf[::-1], u"\0\uFFFD\uFFFD\uFFFDba")
        self.assertEqual(buf[::2], u"a\uFFFD\uFFFD")
        self.assertEqual(buf[6:5:-1], u"")

        ctypes.set_conversion_mode("ascii", "ignore")
        buf = ctypes.create_unicode_buffer("ab���")
        # is that correct? not sure.  But with 'ignore', you get what you pay for..
        self.assertEqual(buf[:], u"ab\0\0\0\0")
        self.assertEqual(buf[::], u"ab\0\0\0\0")
        self.assertEqual(buf[::-1], u"\0\0\0\0ba")
        self.assertEqual(buf[::2], u"a\0\0")
        self.assertEqual(buf[6:5:-1], u"") 
Example #5
Source File: test_unicode.py    From datafari with Apache License 2.0 6 votes vote down vote up
def test_buffers(self):
        ctypes.set_conversion_mode("ascii", "strict")
        buf = ctypes.create_unicode_buffer("abc")
        self.assertEqual(len(buf), 3+1)

        ctypes.set_conversion_mode("ascii", "replace")
        buf = ctypes.create_unicode_buffer("ab���")
        self.assertEqual(buf[:], u"ab\uFFFD\uFFFD\uFFFD\0")
        self.assertEqual(buf[::], u"ab\uFFFD\uFFFD\uFFFD\0")
        self.assertEqual(buf[::-1], u"\0\uFFFD\uFFFD\uFFFDba")
        self.assertEqual(buf[::2], u"a\uFFFD\uFFFD")
        self.assertEqual(buf[6:5:-1], u"")

        ctypes.set_conversion_mode("ascii", "ignore")
        buf = ctypes.create_unicode_buffer("ab���")
        # is that correct? not sure.  But with 'ignore', you get what you pay for..
        self.assertEqual(buf[:], u"ab\0\0\0\0")
        self.assertEqual(buf[::], u"ab\0\0\0\0")
        self.assertEqual(buf[::-1], u"\0\0\0\0ba")
        self.assertEqual(buf[::2], u"a\0\0")
        self.assertEqual(buf[6:5:-1], u"") 
Example #6
Source File: test_unicode.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_buffers(self):
        ctypes.set_conversion_mode("ascii", "strict")
        buf = ctypes.create_string_buffer(u"abc")
        self.assertEqual(len(buf), 3+1)

        ctypes.set_conversion_mode("ascii", "replace")
        buf = ctypes.create_string_buffer(u"ab���")
        self.assertEqual(buf[:], "ab???\0")
        self.assertEqual(buf[::], "ab???\0")
        self.assertEqual(buf[::-1], "\0???ba")
        self.assertEqual(buf[::2], "a??")
        self.assertEqual(buf[6:5:-1], "")

        ctypes.set_conversion_mode("ascii", "ignore")
        buf = ctypes.create_string_buffer(u"ab���")
        # is that correct? not sure.  But with 'ignore', you get what you pay for..
        self.assertEqual(buf[:], "ab\0\0\0\0")
        self.assertEqual(buf[::], "ab\0\0\0\0")
        self.assertEqual(buf[::-1], "\0\0\0\0ba") 
Example #7
Source File: test_unicode.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_buffers(self):
            ctypes.set_conversion_mode("ascii", "strict")
            buf = ctypes.create_unicode_buffer("abc")
            self.assertEqual(len(buf), 3+1)

            ctypes.set_conversion_mode("ascii", "replace")
            buf = ctypes.create_unicode_buffer("ab���")
            self.assertEqual(buf[:], u"ab\uFFFD\uFFFD\uFFFD\0")
            self.assertEqual(buf[::], u"ab\uFFFD\uFFFD\uFFFD\0")
            self.assertEqual(buf[::-1], u"\0\uFFFD\uFFFD\uFFFDba")
            self.assertEqual(buf[::2], u"a\uFFFD\uFFFD")
            self.assertEqual(buf[6:5:-1], u"")

            ctypes.set_conversion_mode("ascii", "ignore")
            buf = ctypes.create_unicode_buffer("ab���")
            # is that correct? not sure.  But with 'ignore', you get what you pay for..
            self.assertEqual(buf[:], u"ab\0\0\0\0")
            self.assertEqual(buf[::], u"ab\0\0\0\0")
            self.assertEqual(buf[::-1], u"\0\0\0\0ba")
            self.assertEqual(buf[::2], u"a\0\0")
            self.assertEqual(buf[6:5:-1], u"") 
Example #8
Source File: test_unicode.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_buffers(self):
        ctypes.set_conversion_mode("ascii", "strict")
        buf = ctypes.create_string_buffer(u"abc")
        self.assertEqual(len(buf), 3+1)

        ctypes.set_conversion_mode("ascii", "replace")
        buf = ctypes.create_string_buffer(u"ab���")
        self.assertEqual(buf[:], "ab???\0")
        self.assertEqual(buf[::], "ab???\0")
        self.assertEqual(buf[::-1], "\0???ba")
        self.assertEqual(buf[::2], "a??")
        self.assertEqual(buf[6:5:-1], "")

        ctypes.set_conversion_mode("ascii", "ignore")
        buf = ctypes.create_string_buffer(u"ab���")
        # is that correct? not sure.  But with 'ignore', you get what you pay for..
        self.assertEqual(buf[:], "ab\0\0\0\0")
        self.assertEqual(buf[::], "ab\0\0\0\0")
        self.assertEqual(buf[::-1], "\0\0\0\0ba") 
Example #9
Source File: test_unicode.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_ascii_replace(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "replace")
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        self.assertEqual(wcslen("abc"), 3)
        self.assertEqual(wcslen("ab�"), 3) 
Example #10
Source File: test_unicode.py    From oss-ftp with MIT License 5 votes vote down vote up
def setUp(self):
        self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict") 
Example #11
Source File: test_unicode.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_ascii_ignore(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "ignore")
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        # ignore error mode skips non-ascii characters
        self.assertEqual(wcslen("abc"), 3)
        self.assertEqual(wcslen("����"), 0) 
Example #12
Source File: test_unicode.py    From oss-ftp with MIT License 5 votes vote down vote up
def setUp(self):
        func = self.func
        self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict")
        func.argtypes = [ctypes.c_char_p]
        func.restype = ctypes.c_char_p 
Example #13
Source File: test_unicode.py    From oss-ftp with MIT License 5 votes vote down vote up
def tearDown(self):
        func = self.func
        ctypes.set_conversion_mode(*self.prev_conv_mode)
        func.argtypes = None
        func.restype = ctypes.c_int 
Example #14
Source File: test_unicode.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_ascii_replace(self):
        func = self.func
        ctypes.set_conversion_mode("ascii", "strict")
        self.assertEqual(func("abc"), "abc")
        self.assertEqual(func(u"abc"), "abc")
        self.assertRaises(ctypes.ArgumentError, func, u"ab�") 
Example #15
Source File: test_unicode.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_ascii_ignore(self):
        func = self.func
        ctypes.set_conversion_mode("ascii", "ignore")
        self.assertEqual(func("abc"), "abc")
        self.assertEqual(func(u"abc"), "abc")
        self.assertEqual(func(u"����"), "") 
Example #16
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict") 
Example #17
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def tearDown(self):
        ctypes.set_conversion_mode(*self.prev_conv_mode) 
Example #18
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_ascii_strict(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "strict")
        # no conversions take place with unicode arguments
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        # string args are converted
        self.assertEqual(wcslen("abc"), 3)
        self.assertRaises(ctypes.ArgumentError, wcslen, "ab�") 
Example #19
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_ascii_replace(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "replace")
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        self.assertEqual(wcslen("abc"), 3)
        self.assertEqual(wcslen("ab�"), 3) 
Example #20
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_ascii_ignore(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "ignore")
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        # ignore error mode skips non-ascii characters
        self.assertEqual(wcslen("abc"), 3)
        self.assertEqual(wcslen("����"), 0) 
Example #21
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        func = self.func
        self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict")
        func.argtypes = [ctypes.c_char_p]
        func.restype = ctypes.c_char_p 
Example #22
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def tearDown(self):
        func = self.func
        ctypes.set_conversion_mode(*self.prev_conv_mode)
        func.argtypes = None
        func.restype = ctypes.c_int 
Example #23
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_ascii_replace(self):
        func = self.func
        ctypes.set_conversion_mode("ascii", "strict")
        self.assertEqual(func("abc"), "abc")
        self.assertEqual(func(u"abc"), "abc")
        self.assertRaises(ctypes.ArgumentError, func, u"ab�") 
Example #24
Source File: test_unicode.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_ascii_ignore(self):
        func = self.func
        ctypes.set_conversion_mode("ascii", "ignore")
        self.assertEqual(func("abc"), "abc")
        self.assertEqual(func(u"abc"), "abc")
        self.assertEqual(func(u"����"), "") 
Example #25
Source File: test_unicode.py    From BinderFilter with MIT License 5 votes vote down vote up
def tearDown(self):
            ctypes.set_conversion_mode(*self.prev_conv_mode) 
Example #26
Source File: test_unicode.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def tearDown(self):
        ctypes.set_conversion_mode(*self.prev_conv_mode) 
Example #27
Source File: test_unicode.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_ascii_strict(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "strict")
        # no conversions take place with unicode arguments
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        # string args are converted
        self.assertEqual(wcslen("abc"), 3)
        self.assertRaises(ctypes.ArgumentError, wcslen, "ab�") 
Example #28
Source File: test_unicode.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_ascii_replace(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "replace")
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        self.assertEqual(wcslen("abc"), 3)
        self.assertEqual(wcslen("ab�"), 3) 
Example #29
Source File: test_unicode.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_ascii_ignore(self):
        wcslen = self.wcslen
        ctypes.set_conversion_mode("ascii", "ignore")
        self.assertEqual(wcslen(u"abc"), 3)
        self.assertEqual(wcslen(u"ab\u2070"), 3)
        # ignore error mode skips non-ascii characters
        self.assertEqual(wcslen("abc"), 3)
        self.assertEqual(wcslen("����"), 0) 
Example #30
Source File: test_unicode.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        func = self.func
        self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict")
        func.argtypes = [ctypes.c_char_p]
        func.restype = ctypes.c_char_p