MCPcopy Index your code
hub / github.com/python/cpython / _buffer_encode

Method _buffer_encode

Lib/encodings/idna.py:274–315  ·  view source on GitHub ↗
(self, input, errors, final)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

ToASCIIFunction · 0.85
take_bytesMethod · 0.80
splitMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected