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

Method _dump_message

Lib/mailbox.py:210–262  ·  view source on GitHub ↗

Dump message contents to target file.

(self, message, target, mangle_from_=False)

Source from the content-addressed store, hash-verified

208 _append_newline = False
209
210 def _dump_message(self, message, target, mangle_from_=False):
211 # This assumes the target file is open in binary mode.
212 """Dump message contents to target file."""
213 if isinstance(message, email.message.Message):
214 buffer = io.BytesIO()
215 gen = email.generator.BytesGenerator(buffer, mangle_from_, 0)
216 gen.flatten(message)
217 buffer.seek(0)
218 data = buffer.read()
219 data = data.replace(b'\n', linesep)
220 target.write(data)
221 if self._append_newline and not data.endswith(linesep):
222 # Make sure the message ends with a newline
223 target.write(linesep)
224 elif isinstance(message, (str, bytes, io.StringIO)):
225 if isinstance(message, io.StringIO):
226 warnings.warn("Use of StringIO input is deprecated, "
227 "use BytesIO instead", DeprecationWarning, 3)
228 message = message.getvalue()
229 if isinstance(message, str):
230 message = self._string_to_bytes(message)
231 if mangle_from_:
232 message = message.replace(b'\nFrom ', b'\n>From ')
233 message = message.replace(b'\n', linesep)
234 target.write(message)
235 if self._append_newline and not message.endswith(linesep):
236 # Make sure the message ends with a newline
237 target.write(linesep)
238 elif hasattr(message, 'read'):
239 if hasattr(message, 'buffer'):
240 warnings.warn("Use of text mode files is deprecated, "
241 "use a binary mode file instead", DeprecationWarning, 3)
242 message = message.buffer
243 lastline = None
244 while True:
245 line = message.readline()
246 # Universal newline support.
247 if line.endswith(b'\r\n'):
248 line = line[:-2] + b'\n'
249 elif line.endswith(b'\r'):
250 line = line[:-1] + b'\n'
251 if not line:
252 break
253 if mangle_from_ and line.startswith(b'From '):
254 line = b'>From ' + line[5:]
255 line = line.replace(b'\n', linesep)
256 target.write(line)
257 lastline = line
258 if self._append_newline and lastline and not lastline.endswith(linesep):
259 # Make sure the message ends with a newline
260 target.write(linesep)
261 else:
262 raise TypeError('Invalid message type: %s' % type(message))
263
264 __class_getitem__ = classmethod(GenericAlias)
265

Callers 5

addMethod · 0.80
_install_messageMethod · 0.80
addMethod · 0.80
__setitem__Method · 0.80
test_dump_messageMethod · 0.80

Calls 13

seekMethod · 0.95
readMethod · 0.95
_string_to_bytesMethod · 0.95
isinstanceFunction · 0.85
hasattrFunction · 0.85
flattenMethod · 0.45
replaceMethod · 0.45
writeMethod · 0.45
endswithMethod · 0.45
warnMethod · 0.45
getvalueMethod · 0.45
readlineMethod · 0.45

Tested by 1

test_dump_messageMethod · 0.64