1 # -*- coding: ISO-8859-1 -*- 2 3 from posix.iconv import Converter, EINVAL, 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 = "???" 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 finally: 50 to_utf8.close() 51 to_utf16.close() 52 from_utf8.close() 53 from_utf16.close() 54 55 try: 56 Converter("horses", "giraffes") 57 except OSError, exc: 58 print 'Converter("horses", "giraffes"): not valid encodings; error is', exc.value