| 271 | |
| 272 | class IncrementalEncoder(codecs.BufferedIncrementalEncoder): |
| 273 | def _buffer_encode(self, input, errors, final): |
| 274 | if errors != 'strict': |
| 275 | # IDNA is quite clear that implementations must be strict |
| 276 | raise UnicodeError(f"Unsupported error handling: {errors}") |
| 277 | |
| 278 | if not input: |
| 279 | return (b'', 0) |
| 280 | |
| 281 | labels = dots.split(input) |
| 282 | trailing_dot = b'' |
| 283 | if labels: |
| 284 | if not labels[-1]: |
| 285 | trailing_dot = b'.' |
| 286 | del labels[-1] |
| 287 | elif not final: |
| 288 | # Keep potentially unfinished label until the next call |
| 289 | del labels[-1] |
| 290 | if labels: |
| 291 | trailing_dot = b'.' |
| 292 | |
| 293 | result = bytearray() |
| 294 | size = 0 |
| 295 | for label in labels: |
| 296 | if size: |
| 297 | # Join with U+002E |
| 298 | result.extend(b'.') |
| 299 | size += 1 |
| 300 | try: |
| 301 | result.extend(ToASCII(label)) |
| 302 | except (UnicodeEncodeError, UnicodeDecodeError) as exc: |
| 303 | raise UnicodeEncodeError( |
| 304 | "idna", |
| 305 | input, |
| 306 | size + exc.start, |
| 307 | size + exc.end, |
| 308 | exc.reason, |
| 309 | ) |
| 310 | size += len(label) |
| 311 | |
| 312 | result += trailing_dot |
| 313 | size += len(trailing_dot) |
| 314 | return (bytes(result), size) |
| 315 | |
| 316 | class IncrementalDecoder(codecs.BufferedIncrementalDecoder): |
| 317 | def _buffer_decode(self, input, errors, final): |