paul@380 | 1 | # -*- coding: ISO-8859-1 -*- |
paul@380 | 2 | |
paul@380 | 3 | from posix.iconv import Converter, EINVAL, EILSEQ |
paul@380 | 4 | |
paul@380 | 5 | to_utf8 = Converter("ISO-8859-1", "UTF-8") |
paul@380 | 6 | to_utf16 = Converter("ISO-8859-1", "UTF-16") |
paul@380 | 7 | from_utf8 = Converter("UTF-8", "ISO-8859-1") |
paul@380 | 8 | from_utf16 = Converter("UTF-16", "ISO-8859-1") |
paul@380 | 9 | |
paul@380 | 10 | try: |
paul@386 | 11 | iso = "???" |
paul@386 | 12 | print iso # ??? |
paul@386 | 13 | to_utf8.feed(iso) |
paul@386 | 14 | utf8 = str(to_utf8) |
paul@386 | 15 | print utf8 # ?????? |
paul@386 | 16 | from_utf8.feed(utf8) |
paul@386 | 17 | print str(from_utf8) # ??? |
paul@386 | 18 | to_utf16.feed(iso) |
paul@386 | 19 | utf16 = str(to_utf16) |
paul@386 | 20 | print utf16 # ... |
paul@386 | 21 | from_utf16.feed(utf16) |
paul@386 | 22 | print str(from_utf16) # ??? |
paul@386 | 23 | |
paul@386 | 24 | # Convert part of a UTF-16 sequence, then convert the remainder, then obtain |
paul@386 | 25 | # the result. |
paul@386 | 26 | |
paul@386 | 27 | first = utf16[:3] |
paul@386 | 28 | second = utf16[3:] |
paul@386 | 29 | |
paul@386 | 30 | from_utf16.reset() |
paul@386 | 31 | print "first:", first # ... |
paul@386 | 32 | from_utf16.feed(first) # should have handled an incomplete input |
paul@386 | 33 | print "second:", second # ... |
paul@386 | 34 | from_utf16.feed(second) # should have handled the complete input |
paul@386 | 35 | print str(from_utf16) # ??? |
paul@386 | 36 | |
paul@386 | 37 | # Convert part of a UTF-8 sequence, then the remainder, then get the result. |
paul@386 | 38 | |
paul@386 | 39 | first = utf8[:3] |
paul@386 | 40 | second = utf8[3:] |
paul@386 | 41 | |
paul@386 | 42 | from_utf8.reset() |
paul@386 | 43 | print "first:", first # ??? |
paul@386 | 44 | from_utf8.feed(first) # should have handled an incomplete input |
paul@386 | 45 | print "second:", second # ??? |
paul@386 | 46 | from_utf8.feed(second) # should have handled the complete input |
paul@386 | 47 | print str(from_utf8) # ??? |
paul@386 | 48 | |
paul@380 | 49 | finally: |
paul@380 | 50 | to_utf8.close() |
paul@380 | 51 | to_utf16.close() |
paul@380 | 52 | from_utf8.close() |
paul@380 | 53 | from_utf16.close() |
paul@380 | 54 | |
paul@380 | 55 | try: |
paul@380 | 56 | Converter("horses", "giraffes") |
paul@380 | 57 | except OSError, exc: |
paul@380 | 58 | print 'Converter("horses", "giraffes"): not valid encodings; error is', exc.value |