| 180 | return bytes(result+trailing_dot), len(input) |
| 181 | |
| 182 | def decode(self, input, errors='strict'): |
| 183 | |
| 184 | if errors != 'strict': |
| 185 | raise UnicodeError("Unsupported error handling "+errors) |
| 186 | |
| 187 | if not input: |
| 188 | return "", 0 |
| 189 | |
| 190 | # IDNA allows decoding to operate on Unicode strings, too. |
| 191 | if not isinstance(input, bytes): |
| 192 | # XXX obviously wrong, see #3232 |
| 193 | input = bytes(input) |
| 194 | |
| 195 | if ace_prefix not in input: |
| 196 | # Fast path |
| 197 | try: |
| 198 | return input.decode('ascii'), len(input) |
| 199 | except UnicodeDecodeError: |
| 200 | pass |
| 201 | |
| 202 | labels = input.split(b".") |
| 203 | |
| 204 | if labels and len(labels[-1]) == 0: |
| 205 | trailing_dot = '.' |
| 206 | del labels[-1] |
| 207 | else: |
| 208 | trailing_dot = '' |
| 209 | |
| 210 | result = [] |
| 211 | for label in labels: |
| 212 | result.append(ToUnicode(label)) |
| 213 | |
| 214 | return ".".join(result)+trailing_dot, len(input) |
| 215 | |
| 216 | class IncrementalEncoder(codecs.BufferedIncrementalEncoder): |
| 217 | def _buffer_encode(self, input, errors, final): |