1 # -*- coding: ISO-8859-1 -*- 2 3 from posix.iconv import Converter, EILSEQ 4 5 to_utf8 = Converter("ISO-8859-1", "UTF-8") 6 to_utf16 = Converter("ISO-8859-1", "UTF-16") 7 from_utf8 = Converter("UTF-8", "ISO-8859-1") 8 from_utf16 = Converter("UTF-16", "ISO-8859-1") 9 10 try: 11 iso = b"???" 12 print iso # ??? 13 to_utf8.feed(iso) 14 utf8 = str(to_utf8) 15 print utf8 # ?????? 16 from_utf8.feed(utf8) 17 print str(from_utf8) # ??? 18 to_utf16.feed(iso) 19 utf16 = str(to_utf16) 20 print utf16 # ... 21 from_utf16.feed(utf16) 22 print str(from_utf16) # ??? 23 24 # Convert part of a UTF-16 sequence, then convert the remainder, then obtain 25 # the result. 26 27 first = utf16[:3] 28 second = utf16[3:] 29 30 from_utf16.reset() 31 print "first:", first # ... 32 from_utf16.feed(first) # should have handled an incomplete input 33 print "second:", second # ... 34 from_utf16.feed(second) # should have handled the complete input 35 print str(from_utf16) # ??? 36 37 # Convert part of a UTF-8 sequence, then the remainder, then get the result. 38 39 first = utf8[:3] 40 second = utf8[3:] 41 42 from_utf8.reset() 43 print "first:", first # ??? 44 from_utf8.feed(first) # should have handled an incomplete input 45 print "second:", second # ??? 46 from_utf8.feed(second) # should have handled the complete input 47 print str(from_utf8) # ??? 48 49 # Attempt to convert ISO-8859-1 characters as if they were UTF-8. 50 51 from_utf8.reset() 52 53 try: 54 from_utf8.feed(iso) # should raise an exception 55 except OSError, exc: 56 if exc.value == EILSEQ: 57 print "Not UTF-8 input:", exc.arg 58 else: 59 print "OSError:", exc.value 60 61 print str(from_utf8) # 62 63 # Attempt to convert ISO-8859-1 characters following some UTF-8 ones. 64 65 to_utf8.reset() 66 to_utf8.feed("???") 67 utf8_2 = str(to_utf8) 68 69 from_utf8.reset() 70 71 try: 72 from_utf8.feed(utf8_2 + iso) # should raise an exception 73 except OSError, exc: 74 if exc.value == EILSEQ: 75 print "Not UTF-8 input:", exc.arg 76 else: 77 print "OSError:", exc.value 78 79 print str(from_utf8) # 80 81 finally: 82 to_utf8.close() 83 to_utf16.close() 84 from_utf8.close() 85 from_utf16.close() 86 87 try: 88 Converter("horses", "giraffes") 89 except OSError, exc: 90 print 'Converter("horses", "giraffes"): not valid encodings; error is', exc.value