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

Function _encode_text

Lib/email/contentmanager.py:145–182  ·  view source on GitHub ↗
(string, charset, cte, policy)

Source from the content-addressed store, hash-verified

143
144
145def _encode_text(string, charset, cte, policy):
146 # If max_line_length is 0 or None, there is no limit.
147 maxlen = policy.max_line_length or sys.maxsize
148 lines = string.encode(charset).splitlines()
149 linesep = policy.linesep.encode('ascii')
150 def embedded_body(lines): return linesep.join(lines) + linesep
151 def normal_body(lines): return b'\n'.join(lines) + b'\n'
152 if cte is None:
153 # Use heuristics to decide on the "best" encoding.
154 if max(map(len, lines), default=0) <= maxlen:
155 try:
156 return '7bit', normal_body(lines).decode('ascii')
157 except UnicodeDecodeError:
158 pass
159 if policy.cte_type == '8bit':
160 return '8bit', normal_body(lines).decode('ascii', 'surrogateescape')
161 sniff = embedded_body(lines[:10])
162 sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'), maxlen)
163 sniff_base64 = binascii.b2a_base64(sniff)
164 # This is a little unfair to qp; it includes lineseps, base64 doesn't.
165 if len(sniff_qp) > len(sniff_base64):
166 cte = 'base64'
167 else:
168 cte = 'quoted-printable'
169 if len(lines) <= 10:
170 return cte, sniff_qp
171 if cte == '7bit':
172 data = normal_body(lines).decode('ascii')
173 elif cte == '8bit':
174 data = normal_body(lines).decode('ascii', 'surrogateescape')
175 elif cte == 'quoted-printable':
176 data = quoprimime.body_encode(normal_body(lines).decode('latin-1'),
177 maxlen)
178 elif cte == 'base64':
179 data = _encode_base64(embedded_body(lines), maxlen)
180 else:
181 raise ValueError("Unknown content transfer encoding {}".format(cte))
182 return cte, data
183
184
185def set_text_content(msg, string, subtype="plain", charset='utf-8', cte=None,

Callers 1

set_text_contentFunction · 0.85

Calls 10

maxFunction · 0.85
normal_bodyFunction · 0.85
embedded_bodyFunction · 0.85
lenFunction · 0.85
body_encodeMethod · 0.80
_encode_base64Function · 0.70
splitlinesMethod · 0.45
encodeMethod · 0.45
decodeMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected