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

Method _handle_multipart

Lib/email/generator.py:269–325  ·  view source on GitHub ↗
(self, msg)

Source from the content-addressed store, hash-verified

267 _writeBody = _handle_text
268
269 def _handle_multipart(self, msg):
270 # The trick here is to write out each part separately, merge them all
271 # together, and then make sure that the boundary we've chosen isn't
272 # present in the payload.
273 msgtexts = []
274 subparts = msg.get_payload()
275 if subparts is None:
276 subparts = []
277 elif isinstance(subparts, str):
278 # e.g. a non-strict parse of a message with no starting boundary.
279 self.write(subparts)
280 return
281 elif not isinstance(subparts, list):
282 # Scalar payload
283 subparts = [subparts]
284 for part in subparts:
285 s = self._new_buffer()
286 g = self.clone(s)
287 g.flatten(part, unixfrom=False, linesep=self._NL)
288 msgtexts.append(s.getvalue())
289 # BAW: What about boundaries that are wrapped in double-quotes?
290 boundary = msg.get_boundary()
291 if not boundary:
292 # Create a boundary that doesn't appear in any of the
293 # message texts.
294 alltext = self._encoded_NL.join(msgtexts)
295 boundary = self._make_boundary(alltext)
296 msg.set_boundary(boundary)
297 # If there's a preamble, write it out, with a trailing CRLF
298 if msg.preamble is not None:
299 if self._mangle_from_:
300 preamble = fcre.sub('>From ', msg.preamble)
301 else:
302 preamble = msg.preamble
303 self._write_lines(preamble)
304 self.write(self._NL)
305 # dash-boundary transport-padding CRLF
306 self.write('--' + boundary + self._NL)
307 # body-part
308 if msgtexts:
309 self._fp.write(msgtexts.pop(0))
310 # *encapsulation
311 # --> delimiter transport-padding
312 # --> CRLF body-part
313 for body_part in msgtexts:
314 # delimiter transport-padding CRLF
315 self.write(self._NL + '--' + boundary + self._NL)
316 # body-part
317 self._fp.write(body_part)
318 # close-delimiter transport-padding
319 self.write(self._NL + '--' + boundary + '--' + self._NL)
320 if msg.epilogue is not None:
321 if self._mangle_from_:
322 epilogue = fcre.sub('>From ', msg.epilogue)
323 else:
324 epilogue = msg.epilogue
325 self._write_lines(epilogue)
326

Callers 1

Calls 14

writeMethod · 0.95
_new_bufferMethod · 0.95
cloneMethod · 0.95
_make_boundaryMethod · 0.95
_write_linesMethod · 0.95
get_payloadMethod · 0.80
get_boundaryMethod · 0.80
set_boundaryMethod · 0.80
flattenMethod · 0.45
appendMethod · 0.45
getvalueMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected