mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-20 22:31:33 +02:00
Implemented encoding for strings
* Packer by default uses `utf-8` encoding by default * Unpacker uses `None` by default, so no decoding is done * Both pack and unpack has `encoding` and `unicode_errors` arguments, if `encoding` is `None` no encoding/decoding is done, otherwise it is python codec. `unicode_errors` is supplied as `errors` parameter to codec
This commit is contained in:
@@ -26,7 +26,7 @@ def test_decode_hook():
|
||||
unpacked = unpacks(packed, object_hook=_decode_complex)
|
||||
eq_(unpacked[1], 1+2j)
|
||||
|
||||
@raises(TypeError)
|
||||
@raises(ValueError)
|
||||
def test_bad_hook():
|
||||
packed = packs([3, 1+2j], default=lambda o: o)
|
||||
unpacked = unpacks(packed)
|
||||
|
@@ -17,12 +17,61 @@ def testPack():
|
||||
1.0,
|
||||
b"", b"a", b"a"*31, b"a"*32,
|
||||
None, True, False,
|
||||
(), ((),), ((), None,),
|
||||
{None: 0},
|
||||
(1<<23),
|
||||
(), ((),), ((), None,),
|
||||
{None: 0},
|
||||
(1<<23),
|
||||
]
|
||||
for td in test_data:
|
||||
check(td)
|
||||
|
||||
def testPackUnicode():
|
||||
test_data = [
|
||||
"", "abcd", ("defgh",), "Русский текст",
|
||||
]
|
||||
for td in test_data:
|
||||
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
|
||||
assert_equal(re, td)
|
||||
|
||||
def testPackUTF32():
|
||||
test_data = [
|
||||
"", "abcd", ("defgh",), "Русский текст",
|
||||
]
|
||||
for td in test_data:
|
||||
print(packs(td, encoding='utf-32'))
|
||||
re = unpacks(packs(td, encoding='utf-32'), encoding='utf-32')
|
||||
assert_equal(re, td)
|
||||
|
||||
def testPackBytes():
|
||||
test_data = [
|
||||
b"", b"abcd", (b"defgh",),
|
||||
]
|
||||
for td in test_data:
|
||||
check(td)
|
||||
|
||||
def testIgnoreUnicodeErrors():
|
||||
re = unpacks(packs(b'abc\xeddef'),
|
||||
encoding='utf-8', unicode_errors='ignore')
|
||||
assert_equal(re, "abcdef")
|
||||
|
||||
@raises(UnicodeDecodeError)
|
||||
def testStrictUnicodeUnpack():
|
||||
unpacks(packs(b'abc\xeddef'), encoding='utf-8')
|
||||
|
||||
@raises(UnicodeEncodeError)
|
||||
def testStrictUnicodePack():
|
||||
packs("abc\xeddef", encoding='ascii', unicode_errors='strict')
|
||||
|
||||
def testIgnoreErrorsPack():
|
||||
re = unpacks(packs("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8')
|
||||
assert_equal(re, "abcdef")
|
||||
|
||||
@raises(TypeError)
|
||||
def testNoEncoding():
|
||||
packs("abc", encoding=None)
|
||||
|
||||
def testDecodeBinary():
|
||||
re = unpacks(packs("abc"), encoding=None)
|
||||
assert_equal(re, b"abc")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Reference in New Issue
Block a user