Guido van Rossum | 0229bf6 | 2000-03-10 23:17:24 +0000 | [diff] [blame] | 1 | """ Python 'utf-16' Codec |
| 2 | |
| 3 | |
| 4 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 5 | |
| 6 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 7 | |
| 8 | """ |
Marc-André Lemburg | 92b550c | 2001-06-19 20:07:51 +0000 | [diff] [blame] | 9 | import codecs, sys |
Guido van Rossum | 0229bf6 | 2000-03-10 23:17:24 +0000 | [diff] [blame] | 10 | |
| 11 | ### Codec APIs |
| 12 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 13 | encode = codecs.utf_16_encode |
Guido van Rossum | 0229bf6 | 2000-03-10 23:17:24 +0000 | [diff] [blame] | 14 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 15 | def decode(input, errors='strict'): |
| 16 | return codecs.utf_16_decode(input, errors, True) |
Guido van Rossum | 0229bf6 | 2000-03-10 23:17:24 +0000 | [diff] [blame] | 17 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 18 | class StreamWriter(codecs.StreamWriter): |
Marc-André Lemburg | 92b550c | 2001-06-19 20:07:51 +0000 | [diff] [blame] | 19 | def __init__(self, stream, errors='strict'): |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 20 | self.bom_written = False |
Marc-André Lemburg | 92b550c | 2001-06-19 20:07:51 +0000 | [diff] [blame] | 21 | codecs.StreamWriter.__init__(self, stream, errors) |
| 22 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 23 | def encode(self, input, errors='strict'): |
| 24 | self.bom_written = True |
| 25 | result = codecs.utf_16_encode(input, errors) |
| 26 | if sys.byteorder == 'little': |
| 27 | self.encode = codecs.utf_16_le_encode |
| 28 | else: |
| 29 | self.encode = codecs.utf_16_be_encode |
Marc-André Lemburg | 92b550c | 2001-06-19 20:07:51 +0000 | [diff] [blame] | 30 | return result |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 31 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 32 | class StreamReader(codecs.StreamReader): |
Marc-André Lemburg | 92b550c | 2001-06-19 20:07:51 +0000 | [diff] [blame] | 33 | |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 34 | def reset(self): |
| 35 | codecs.StreamReader.reset(self) |
| 36 | try: |
| 37 | del self.decode |
| 38 | except AttributeError: |
| 39 | pass |
| 40 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 41 | def decode(self, input, errors='strict'): |
| 42 | (object, consumed, byteorder) = \ |
| 43 | codecs.utf_16_ex_decode(input, errors, 0, False) |
| 44 | if byteorder == -1: |
| 45 | self.decode = codecs.utf_16_le_decode |
| 46 | elif byteorder == 1: |
| 47 | self.decode = codecs.utf_16_be_decode |
| 48 | elif consumed>=2: |
| 49 | raise UnicodeError,"UTF-16 stream does not start with BOM" |
| 50 | return (object, consumed) |
Marc-André Lemburg | 3ccb09c | 2002-04-05 12:12:00 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 0229bf6 | 2000-03-10 23:17:24 +0000 | [diff] [blame] | 52 | ### encodings module API |
| 53 | |
| 54 | def getregentry(): |
| 55 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 56 | return (encode,decode,StreamReader,StreamWriter) |