Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 1 | """ |
| 2 | Code page 65001: Windows UTF-8 (CP_UTF8). |
| 3 | """ |
| 4 | |
| 5 | import codecs |
| 6 | import functools |
| 7 | |
| 8 | if not hasattr(codecs, 'code_page_encode'): |
| 9 | raise LookupError("cp65001 encoding is only available on Windows") |
| 10 | |
| 11 | ### Codec APIs |
| 12 | |
| 13 | encode = functools.partial(codecs.code_page_encode, 65001) |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 14 | _decode = functools.partial(codecs.code_page_decode, 65001) |
| 15 | |
| 16 | def decode(input, errors='strict'): |
| 17 | return codecs.code_page_decode(65001, input, errors, True) |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 18 | |
| 19 | class IncrementalEncoder(codecs.IncrementalEncoder): |
| 20 | def encode(self, input, final=False): |
| 21 | return encode(input, self.errors)[0] |
| 22 | |
| 23 | class IncrementalDecoder(codecs.BufferedIncrementalDecoder): |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 24 | _buffer_decode = _decode |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 25 | |
| 26 | class StreamWriter(codecs.StreamWriter): |
| 27 | encode = encode |
| 28 | |
| 29 | class StreamReader(codecs.StreamReader): |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 30 | decode = _decode |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 31 | |
| 32 | ### encodings module API |
| 33 | |
| 34 | def getregentry(): |
| 35 | return codecs.CodecInfo( |
| 36 | name='cp65001', |
| 37 | encode=encode, |
| 38 | decode=decode, |
| 39 | incrementalencoder=IncrementalEncoder, |
| 40 | incrementaldecoder=IncrementalDecoder, |
| 41 | streamreader=StreamReader, |
| 42 | streamwriter=StreamWriter, |
| 43 | ) |