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@380 | 11 | try: |
paul@383 | 12 | iso = "???" |
paul@383 | 13 | print iso # ??? |
paul@383 | 14 | utf = to_utf8.convert(iso) |
paul@383 | 15 | print utf # ?????? |
paul@383 | 16 | print from_utf8.convert(utf) # ??? |
paul@383 | 17 | utf = to_utf16.convert(iso) |
paul@383 | 18 | print utf # ... |
paul@380 | 19 | print from_utf16.convert(utf) # ??? |
paul@380 | 20 | except OSError, exc: |
paul@380 | 21 | if exc.value == EINVAL: |
paul@380 | 22 | print "Incomplete input", exc.arg |
paul@380 | 23 | elif exc.value == EILSEQ: |
paul@380 | 24 | print "Invalid input", exc.arg |
paul@380 | 25 | else: |
paul@380 | 26 | print exc.value, exc.arg |
paul@380 | 27 | finally: |
paul@380 | 28 | to_utf8.close() |
paul@380 | 29 | to_utf16.close() |
paul@380 | 30 | from_utf8.close() |
paul@380 | 31 | from_utf16.close() |
paul@380 | 32 | |
paul@380 | 33 | try: |
paul@380 | 34 | Converter("horses", "giraffes") |
paul@380 | 35 | except OSError, exc: |
paul@380 | 36 | print 'Converter("horses", "giraffes"): not valid encodings; error is', exc.value |