| 1 | n/a | """ Python 'utf-8-sig' Codec |
|---|
| 2 | n/a | This work similar to UTF-8 with the following changes: |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | * On encoding/writing a UTF-8 encoded BOM will be prepended/written as the |
|---|
| 5 | n/a | first three bytes. |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | * On decoding/reading if the first three bytes are a UTF-8 encoded BOM, these |
|---|
| 8 | n/a | bytes will be skipped. |
|---|
| 9 | n/a | """ |
|---|
| 10 | n/a | import codecs |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | ### Codec APIs |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | def encode(input, errors='strict'): |
|---|
| 15 | n/a | return (codecs.BOM_UTF8 + codecs.utf_8_encode(input, errors)[0], |
|---|
| 16 | n/a | len(input)) |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | def decode(input, errors='strict'): |
|---|
| 19 | n/a | prefix = 0 |
|---|
| 20 | n/a | if input[:3] == codecs.BOM_UTF8: |
|---|
| 21 | n/a | input = input[3:] |
|---|
| 22 | n/a | prefix = 3 |
|---|
| 23 | n/a | (output, consumed) = codecs.utf_8_decode(input, errors, True) |
|---|
| 24 | n/a | return (output, consumed+prefix) |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | class IncrementalEncoder(codecs.IncrementalEncoder): |
|---|
| 27 | n/a | def __init__(self, errors='strict'): |
|---|
| 28 | n/a | codecs.IncrementalEncoder.__init__(self, errors) |
|---|
| 29 | n/a | self.first = 1 |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def encode(self, input, final=False): |
|---|
| 32 | n/a | if self.first: |
|---|
| 33 | n/a | self.first = 0 |
|---|
| 34 | n/a | return codecs.BOM_UTF8 + \ |
|---|
| 35 | n/a | codecs.utf_8_encode(input, self.errors)[0] |
|---|
| 36 | n/a | else: |
|---|
| 37 | n/a | return codecs.utf_8_encode(input, self.errors)[0] |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def reset(self): |
|---|
| 40 | n/a | codecs.IncrementalEncoder.reset(self) |
|---|
| 41 | n/a | self.first = 1 |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def getstate(self): |
|---|
| 44 | n/a | return self.first |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | def setstate(self, state): |
|---|
| 47 | n/a | self.first = state |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | class IncrementalDecoder(codecs.BufferedIncrementalDecoder): |
|---|
| 50 | n/a | def __init__(self, errors='strict'): |
|---|
| 51 | n/a | codecs.BufferedIncrementalDecoder.__init__(self, errors) |
|---|
| 52 | n/a | self.first = 1 |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def _buffer_decode(self, input, errors, final): |
|---|
| 55 | n/a | if self.first: |
|---|
| 56 | n/a | if len(input) < 3: |
|---|
| 57 | n/a | if codecs.BOM_UTF8.startswith(input): |
|---|
| 58 | n/a | # not enough data to decide if this really is a BOM |
|---|
| 59 | n/a | # => try again on the next call |
|---|
| 60 | n/a | return ("", 0) |
|---|
| 61 | n/a | else: |
|---|
| 62 | n/a | self.first = 0 |
|---|
| 63 | n/a | else: |
|---|
| 64 | n/a | self.first = 0 |
|---|
| 65 | n/a | if input[:3] == codecs.BOM_UTF8: |
|---|
| 66 | n/a | (output, consumed) = \ |
|---|
| 67 | n/a | codecs.utf_8_decode(input[3:], errors, final) |
|---|
| 68 | n/a | return (output, consumed+3) |
|---|
| 69 | n/a | return codecs.utf_8_decode(input, errors, final) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def reset(self): |
|---|
| 72 | n/a | codecs.BufferedIncrementalDecoder.reset(self) |
|---|
| 73 | n/a | self.first = 1 |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | def getstate(self): |
|---|
| 76 | n/a | state = codecs.BufferedIncrementalDecoder.getstate(self) |
|---|
| 77 | n/a | # state[1] must be 0 here, as it isn't passed along to the caller |
|---|
| 78 | n/a | return (state[0], self.first) |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | def setstate(self, state): |
|---|
| 81 | n/a | # state[1] will be ignored by BufferedIncrementalDecoder.setstate() |
|---|
| 82 | n/a | codecs.BufferedIncrementalDecoder.setstate(self, state) |
|---|
| 83 | n/a | self.first = state[1] |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | class StreamWriter(codecs.StreamWriter): |
|---|
| 86 | n/a | def reset(self): |
|---|
| 87 | n/a | codecs.StreamWriter.reset(self) |
|---|
| 88 | n/a | try: |
|---|
| 89 | n/a | del self.encode |
|---|
| 90 | n/a | except AttributeError: |
|---|
| 91 | n/a | pass |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | def encode(self, input, errors='strict'): |
|---|
| 94 | n/a | self.encode = codecs.utf_8_encode |
|---|
| 95 | n/a | return encode(input, errors) |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | class StreamReader(codecs.StreamReader): |
|---|
| 98 | n/a | def reset(self): |
|---|
| 99 | n/a | codecs.StreamReader.reset(self) |
|---|
| 100 | n/a | try: |
|---|
| 101 | n/a | del self.decode |
|---|
| 102 | n/a | except AttributeError: |
|---|
| 103 | n/a | pass |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | def decode(self, input, errors='strict'): |
|---|
| 106 | n/a | if len(input) < 3: |
|---|
| 107 | n/a | if codecs.BOM_UTF8.startswith(input): |
|---|
| 108 | n/a | # not enough data to decide if this is a BOM |
|---|
| 109 | n/a | # => try again on the next call |
|---|
| 110 | n/a | return ("", 0) |
|---|
| 111 | n/a | elif input[:3] == codecs.BOM_UTF8: |
|---|
| 112 | n/a | self.decode = codecs.utf_8_decode |
|---|
| 113 | n/a | (output, consumed) = codecs.utf_8_decode(input[3:],errors) |
|---|
| 114 | n/a | return (output, consumed+3) |
|---|
| 115 | n/a | # (else) no BOM present |
|---|
| 116 | n/a | self.decode = codecs.utf_8_decode |
|---|
| 117 | n/a | return codecs.utf_8_decode(input, errors) |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | ### encodings module API |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def getregentry(): |
|---|
| 122 | n/a | return codecs.CodecInfo( |
|---|
| 123 | n/a | name='utf-8-sig', |
|---|
| 124 | n/a | encode=encode, |
|---|
| 125 | n/a | decode=decode, |
|---|
| 126 | n/a | incrementalencoder=IncrementalEncoder, |
|---|
| 127 | n/a | incrementaldecoder=IncrementalDecoder, |
|---|
| 128 | n/a | streamreader=StreamReader, |
|---|
| 129 | n/a | streamwriter=StreamWriter, |
|---|
| 130 | n/a | ) |
|---|