|
11 | 11 |
|
12 | 12 | class CAPITest(unittest.TestCase):
|
13 | 13 |
|
| 14 | + @support.cpython_only |
| 15 | + @unittest.skipIf(_testcapi is None, 'need _testcapi module') |
| 16 | + def test_decodeutf8(self): |
| 17 | + """Test PyUnicode_DecodeUTF8()""" |
| 18 | + from _testcapi import unicode_decodeutf8 as decodeutf8 |
| 19 | + |
| 20 | + for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']: |
| 21 | + b = s.encode('utf-8') |
| 22 | + self.assertEqual(decodeutf8(b), s) |
| 23 | + self.assertEqual(decodeutf8(b, 'strict'), s) |
| 24 | + |
| 25 | + self.assertRaises(UnicodeDecodeError, decodeutf8, b'\x80') |
| 26 | + self.assertRaises(UnicodeDecodeError, decodeutf8, b'\xc0') |
| 27 | + self.assertRaises(UnicodeDecodeError, decodeutf8, b'\xff') |
| 28 | + self.assertRaises(UnicodeDecodeError, decodeutf8, b'a\xf0\x9f') |
| 29 | + self.assertEqual(decodeutf8(b'a\xf0\x9f', 'replace'), 'a\ufffd') |
| 30 | + self.assertEqual(decodeutf8(b'a\xf0\x9fb', 'replace'), 'a\ufffdb') |
| 31 | + |
| 32 | + self.assertRaises(LookupError, decodeutf8, b'a\x80', 'foo') |
| 33 | + # TODO: Test PyUnicode_DecodeUTF8() with NULL as data and |
| 34 | + # negative size. |
| 35 | + |
| 36 | + @support.cpython_only |
| 37 | + @unittest.skipIf(_testcapi is None, 'need _testcapi module') |
| 38 | + def test_decodeutf8stateful(self): |
| 39 | + """Test PyUnicode_DecodeUTF8Stateful()""" |
| 40 | + from _testcapi import unicode_decodeutf8stateful as decodeutf8stateful |
| 41 | + |
| 42 | + for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']: |
| 43 | + b = s.encode('utf-8') |
| 44 | + self.assertEqual(decodeutf8stateful(b), (s, len(b))) |
| 45 | + self.assertEqual(decodeutf8stateful(b, 'strict'), (s, len(b))) |
| 46 | + |
| 47 | + self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'\x80') |
| 48 | + self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'\xc0') |
| 49 | + self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'\xff') |
| 50 | + self.assertEqual(decodeutf8stateful(b'a\xf0\x9f'), ('a', 1)) |
| 51 | + self.assertEqual(decodeutf8stateful(b'a\xf0\x9f', 'replace'), ('a', 1)) |
| 52 | + self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'a\xf0\x9fb') |
| 53 | + self.assertEqual(decodeutf8stateful(b'a\xf0\x9fb', 'replace'), ('a\ufffdb', 4)) |
| 54 | + |
| 55 | + self.assertRaises(LookupError, decodeutf8stateful, b'a\x80', 'foo') |
| 56 | + # TODO: Test PyUnicode_DecodeUTF8Stateful() with NULL as data and |
| 57 | + # negative size. |
| 58 | + # TODO: Test PyUnicode_DecodeUTF8Stateful() with NULL as the address of |
| 59 | + # "consumed". |
| 60 | + |
14 | 61 | # Test PyUnicode_FromFormat()
|
15 | 62 | def test_from_format(self):
|
16 | 63 | import_helper.import_module('ctypes')
|
|
0 commit comments