MCPcopy Index your code
hub / github.com/RustPython/RustPython / BytesGenerator

Class BytesGenerator

Lib/email/generator.py:406–453  ·  view source on GitHub ↗

Generates a bytes version of a Message object tree. Functionally identical to the base Generator except that the output is bytes and not string. When surrogates were used in the input to encode bytes, these are decoded back to bytes for output. If the policy has cte_type set to 7b

Source from the content-addressed store, hash-verified

404
405
406class BytesGenerator(Generator):
407 """Generates a bytes version of a Message object tree.
408
409 Functionally identical to the base Generator except that the output is
410 bytes and not string. When surrogates were used in the input to encode
411 bytes, these are decoded back to bytes for output. If the policy has
412 cte_type set to 7bit, then the message is transformed such that the
413 non-ASCII bytes are properly content transfer encoded, using the charset
414 unknown-8bit.
415
416 The outfp object must accept bytes in its write method.
417 """
418
419 def write(self, s):
420 self._fp.write(s.encode('ascii', 'surrogateescape'))
421
422 def _new_buffer(self):
423 return BytesIO()
424
425 def _encode(self, s):
426 return s.encode('ascii')
427
428 def _write_headers(self, msg):
429 # This is almost the same as the string version, except for handling
430 # strings with 8bit bytes.
431 for h, v in msg.raw_items():
432 self._fp.write(self.policy.fold_binary(h, v))
433 # A blank line always separates headers from body
434 self.write(self._NL)
435
436 def _handle_text(self, msg):
437 # If the string has surrogates the original source was bytes, so
438 # just write it back out.
439 if msg._payload is None:
440 return
441 if _has_surrogates(msg._payload) and not self.policy.cte_type=='7bit':
442 if self._mangle_from_:
443 msg._payload = fcre.sub(">From ", msg._payload)
444 self._write_lines(msg._payload)
445 else:
446 super(BytesGenerator,self)._handle_text(msg)
447
448 # Default body handler
449 _writeBody = _handle_text
450
451 @classmethod
452 def _compile_re(cls, s, flags):
453 return re.compile(s.encode('ascii'), flags)
454
455
456_FMT = '[Non-text (%(type)s) part of message omitted, filename %(filename)s]'

Calls

no outgoing calls