# HG changeset patch # User Paul Boddie # Date 1481564609 -3600 # Node ID d11750cc8bf3ff8e16a4f8af467337be9aaf424c # Parent ef11d754296983e796f281c209e5149b88e5d201 Added tests of illegal sequences when converting between encodings. diff -r ef11d7542969 -r d11750cc8bf3 tests/iconv.py --- a/tests/iconv.py Mon Dec 12 18:30:40 2016 +0100 +++ b/tests/iconv.py Mon Dec 12 18:43:29 2016 +0100 @@ -1,6 +1,6 @@ # -*- coding: ISO-8859-1 -*- -from posix.iconv import Converter, EINVAL, EILSEQ +from posix.iconv import Converter, EILSEQ to_utf8 = Converter("ISO-8859-1", "UTF-8") to_utf16 = Converter("ISO-8859-1", "UTF-16") @@ -46,6 +46,38 @@ from_utf8.feed(second) # should have handled the complete input print str(from_utf8) # æøå + # Attempt to convert ISO-8859-1 characters as if they were UTF-8. + + from_utf8.reset() + + try: + from_utf8.feed(iso) # should raise an exception + except OSError, exc: + if exc.value == EILSEQ: + print "Not UTF-8 input:", exc.arg + else: + print "OSError:", exc.value + + print str(from_utf8) # + + # Attempt to convert ISO-8859-1 characters following some UTF-8 ones. + + to_utf8.reset() + to_utf8.feed("ÆØÅ") + utf8_2 = str(to_utf8) + + from_utf8.reset() + + try: + from_utf8.feed(utf8_2 + iso) # should raise an exception + except OSError, exc: + if exc.value == EILSEQ: + print "Not UTF-8 input:", exc.arg + else: + print "OSError:", exc.value + + print str(from_utf8) # + finally: to_utf8.close() to_utf16.close()