blob: 899adbc7af33dc72a266aa89cbac25492239c902 [file] [log] [blame]
Benjamin Peterson46a99002010-01-09 18:45:30 +00001# Copyright (C) 2001-2010 Python Software Foundation
Guido van Rossum8b3febe2007-08-30 01:15:14 +00002# Author: Barry Warsaw
3# Contact: email-sig@python.org
4
5"""Classes to generate plain text from a message object tree."""
6
R David Murray1b6c7242012-03-16 22:43:05 -04007__all__ = ['Generator', 'DecodedGenerator', 'BytesGenerator']
Guido van Rossum8b3febe2007-08-30 01:15:14 +00008
9import re
10import sys
11import time
12import random
13import warnings
14
R. David Murray96fd54e2010-10-08 15:55:28 +000015from io import StringIO, BytesIO
R David Murrayc27e5222012-05-25 15:01:48 -040016from email._policybase import compat32
Guido van Rossum8b3febe2007-08-30 01:15:14 +000017from email.header import Header
R David Murrayc27e5222012-05-25 15:01:48 -040018from email.utils import _has_surrogates
R David Murray3edd22a2011-04-18 13:59:37 -040019import email.charset as _charset
Guido van Rossum8b3febe2007-08-30 01:15:14 +000020
21UNDERSCORE = '_'
R. David Murray8451c4b2010-10-23 22:19:56 +000022NL = '\n' # XXX: no longer used by the code below.
Guido van Rossum8b3febe2007-08-30 01:15:14 +000023
24fcre = re.compile(r'^From ', re.MULTILINE)
25
26
27
28class Generator:
29 """Generates output from a Message object tree.
30
31 This basic generator writes the message to the given file object as plain
32 text.
33 """
34 #
35 # Public interface
36 #
37
R David Murray3edd22a2011-04-18 13:59:37 -040038 def __init__(self, outfp, mangle_from_=True, maxheaderlen=None, *,
R David Murrayc27e5222012-05-25 15:01:48 -040039 policy=None):
Guido van Rossum8b3febe2007-08-30 01:15:14 +000040 """Create the generator for message flattening.
41
42 outfp is the output file-like object for writing the message to. It
43 must have a write() method.
44
45 Optional mangle_from_ is a flag that, when True (the default), escapes
46 From_ lines in the body of the message by putting a `>' in front of
47 them.
48
49 Optional maxheaderlen specifies the longest length for a non-continued
50 header. When a header line is longer (in characters, with tabs
51 expanded to 8 spaces) than maxheaderlen, the header will split as
52 defined in the Header class. Set maxheaderlen to zero to disable
53 header wrapping. The default is 78, as recommended (but not required)
54 by RFC 2822.
R David Murray3edd22a2011-04-18 13:59:37 -040055
56 The policy keyword specifies a policy object that controls a number of
57 aspects of the generator's operation. The default policy maintains
58 backward compatibility.
59
Guido van Rossum8b3febe2007-08-30 01:15:14 +000060 """
61 self._fp = outfp
62 self._mangle_from_ = mangle_from_
R David Murrayc27e5222012-05-25 15:01:48 -040063 self.maxheaderlen = maxheaderlen
R David Murray3edd22a2011-04-18 13:59:37 -040064 self.policy = policy
Guido van Rossum8b3febe2007-08-30 01:15:14 +000065
66 def write(self, s):
67 # Just delegate to the file object
68 self._fp.write(s)
69
R David Murray3edd22a2011-04-18 13:59:37 -040070 def flatten(self, msg, unixfrom=False, linesep=None):
R David Murraycd37dfc2011-03-14 18:35:56 -040071 r"""Print the message object tree rooted at msg to the output file
Guido van Rossum8b3febe2007-08-30 01:15:14 +000072 specified when the Generator instance was created.
73
74 unixfrom is a flag that forces the printing of a Unix From_ delimiter
75 before the first object in the message tree. If the original message
76 has no From_ delimiter, a `standard' one is crafted. By default, this
77 is False to inhibit the printing of any From_ delimiter.
78
79 Note that for subobjects, no From_ line is printed.
R. David Murray8451c4b2010-10-23 22:19:56 +000080
81 linesep specifies the characters used to indicate a new line in
R David Murray3edd22a2011-04-18 13:59:37 -040082 the output. The default value is determined by the policy.
R David Murraycd37dfc2011-03-14 18:35:56 -040083
Guido van Rossum8b3febe2007-08-30 01:15:14 +000084 """
R. David Murray8451c4b2010-10-23 22:19:56 +000085 # We use the _XXX constants for operating on data that comes directly
86 # from the msg, and _encoded_XXX constants for operating on data that
87 # has already been converted (to bytes in the BytesGenerator) and
88 # inserted into a temporary buffer.
R David Murrayc27e5222012-05-25 15:01:48 -040089 policy = msg.policy if self.policy is None else self.policy
90 if linesep is not None:
91 policy = policy.clone(linesep=linesep)
92 if self.maxheaderlen is not None:
93 policy = policy.clone(max_line_length=self.maxheaderlen)
94 self._NL = policy.linesep
R David Murray3edd22a2011-04-18 13:59:37 -040095 self._encoded_NL = self._encode(self._NL)
R. David Murray8451c4b2010-10-23 22:19:56 +000096 self._EMPTY = ''
97 self._encoded_EMTPY = self._encode('')
R David Murray0b6f6c82012-05-25 18:42:14 -040098 # Because we use clone (below) when we recursively process message
99 # subparts, and because clone uses the computed policy (not None),
100 # submessages will automatically get set to the computed policy when
101 # they are processed by this code.
102 old_gen_policy = self.policy
103 old_msg_policy = msg.policy
R David Murrayc27e5222012-05-25 15:01:48 -0400104 try:
105 self.policy = policy
R David Murray0b6f6c82012-05-25 18:42:14 -0400106 msg.policy = policy
R David Murrayc27e5222012-05-25 15:01:48 -0400107 if unixfrom:
108 ufrom = msg.get_unixfrom()
109 if not ufrom:
110 ufrom = 'From nobody ' + time.ctime(time.time())
111 self.write(ufrom + self._NL)
112 self._write(msg)
113 finally:
R David Murray0b6f6c82012-05-25 18:42:14 -0400114 self.policy = old_gen_policy
115 msg.policy = old_msg_policy
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000116
117 def clone(self, fp):
118 """Clone this generator with the exact same options."""
R David Murrayc27e5222012-05-25 15:01:48 -0400119 return self.__class__(fp,
120 self._mangle_from_,
121 None, # Use policy setting, which we've adjusted
122 policy=self.policy)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000123
124 #
125 # Protected interface - undocumented ;/
126 #
127
R. David Murray96fd54e2010-10-08 15:55:28 +0000128 # Note that we use 'self.write' when what we are writing is coming from
129 # the source, and self._fp.write when what we are writing is coming from a
130 # buffer (because the Bytes subclass has already had a chance to transform
131 # the data in its write method in that case). This is an entirely
132 # pragmatic split determined by experiment; we could be more general by
133 # always using write and having the Bytes subclass write method detect when
134 # it has already transformed the input; but, since this whole thing is a
135 # hack anyway this seems good enough.
136
R. David Murray8451c4b2010-10-23 22:19:56 +0000137 # Similarly, we have _XXX and _encoded_XXX attributes that are used on
138 # source and buffer data, respectively.
139 _encoded_EMPTY = ''
R. David Murray96fd54e2010-10-08 15:55:28 +0000140
141 def _new_buffer(self):
142 # BytesGenerator overrides this to return BytesIO.
143 return StringIO()
144
R. David Murray8451c4b2010-10-23 22:19:56 +0000145 def _encode(self, s):
146 # BytesGenerator overrides this to encode strings to bytes.
147 return s
148
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000149 def _write(self, msg):
150 # We can't write the headers yet because of the following scenario:
151 # say a multipart message includes the boundary string somewhere in
152 # its body. We'd have to calculate the new boundary /before/ we write
153 # the headers so that we can write the correct Content-Type:
154 # parameter.
155 #
156 # The way we do this, so as to make the _handle_*() methods simpler,
R. David Murray96fd54e2010-10-08 15:55:28 +0000157 # is to cache any subpart writes into a buffer. The we write the
158 # headers and the buffer contents. That way, subpart handlers can
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000159 # Do The Right Thing, and can still modify the Content-Type: header if
160 # necessary.
161 oldfp = self._fp
162 try:
R. David Murray96fd54e2010-10-08 15:55:28 +0000163 self._fp = sfp = self._new_buffer()
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000164 self._dispatch(msg)
165 finally:
166 self._fp = oldfp
167 # Write the headers. First we see if the message object wants to
168 # handle that itself. If not, we'll do it generically.
169 meth = getattr(msg, '_write_headers', None)
170 if meth is None:
171 self._write_headers(msg)
172 else:
173 meth(self)
174 self._fp.write(sfp.getvalue())
175
176 def _dispatch(self, msg):
177 # Get the Content-Type: for the message, then try to dispatch to
178 # self._handle_<maintype>_<subtype>(). If there's no handler for the
179 # full MIME type, then dispatch to self._handle_<maintype>(). If
180 # that's missing too, then dispatch to self._writeBody().
181 main = msg.get_content_maintype()
182 sub = msg.get_content_subtype()
183 specific = UNDERSCORE.join((main, sub)).replace('-', '_')
184 meth = getattr(self, '_handle_' + specific, None)
185 if meth is None:
186 generic = main.replace('-', '_')
187 meth = getattr(self, '_handle_' + generic, None)
188 if meth is None:
189 meth = self._writeBody
190 meth(msg)
191
192 #
193 # Default handlers
194 #
195
196 def _write_headers(self, msg):
R David Murrayc27e5222012-05-25 15:01:48 -0400197 for h, v in msg.raw_items():
198 self.write(self.policy.fold(h, v))
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000199 # A blank line always separates headers from body
R. David Murray8451c4b2010-10-23 22:19:56 +0000200 self.write(self._NL)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000201
202 #
203 # Handlers for writing types and subtypes
204 #
205
206 def _handle_text(self, msg):
207 payload = msg.get_payload()
208 if payload is None:
209 return
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000210 if not isinstance(payload, str):
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000211 raise TypeError('string payload expected: %s' % type(payload))
R. David Murray96fd54e2010-10-08 15:55:28 +0000212 if _has_surrogates(msg._payload):
213 charset = msg.get_param('charset')
214 if charset is not None:
215 del msg['content-transfer-encoding']
216 msg.set_payload(payload, charset)
217 payload = msg.get_payload()
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000218 if self._mangle_from_:
219 payload = fcre.sub('>From ', payload)
R. David Murray96fd54e2010-10-08 15:55:28 +0000220 self.write(payload)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000221
222 # Default body handler
223 _writeBody = _handle_text
224
225 def _handle_multipart(self, msg):
226 # The trick here is to write out each part separately, merge them all
227 # together, and then make sure that the boundary we've chosen isn't
228 # present in the payload.
229 msgtexts = []
230 subparts = msg.get_payload()
231 if subparts is None:
232 subparts = []
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000233 elif isinstance(subparts, str):
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000234 # e.g. a non-strict parse of a message with no starting boundary.
R. David Murray96fd54e2010-10-08 15:55:28 +0000235 self.write(subparts)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000236 return
237 elif not isinstance(subparts, list):
238 # Scalar payload
239 subparts = [subparts]
240 for part in subparts:
R. David Murray96fd54e2010-10-08 15:55:28 +0000241 s = self._new_buffer()
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000242 g = self.clone(s)
R. David Murray8451c4b2010-10-23 22:19:56 +0000243 g.flatten(part, unixfrom=False, linesep=self._NL)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000244 msgtexts.append(s.getvalue())
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000245 # BAW: What about boundaries that are wrapped in double-quotes?
R. David Murray5260a9b2010-12-12 20:06:19 +0000246 boundary = msg.get_boundary()
247 if not boundary:
248 # Create a boundary that doesn't appear in any of the
249 # message texts.
250 alltext = self._encoded_NL.join(msgtexts)
R. David Murray73a559d2010-12-21 18:07:59 +0000251 boundary = self._make_boundary(alltext)
252 msg.set_boundary(boundary)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000253 # If there's a preamble, write it out, with a trailing CRLF
254 if msg.preamble is not None:
R David Murray6a31bc62012-07-22 21:47:53 -0400255 if self._mangle_from_:
256 preamble = fcre.sub('>From ', msg.preamble)
257 else:
258 preamble = msg.preamble
259 self.write(preamble + self._NL)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000260 # dash-boundary transport-padding CRLF
R. David Murray8451c4b2010-10-23 22:19:56 +0000261 self.write('--' + boundary + self._NL)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000262 # body-part
263 if msgtexts:
264 self._fp.write(msgtexts.pop(0))
265 # *encapsulation
266 # --> delimiter transport-padding
267 # --> CRLF body-part
268 for body_part in msgtexts:
269 # delimiter transport-padding CRLF
R. David Murray8451c4b2010-10-23 22:19:56 +0000270 self.write(self._NL + '--' + boundary + self._NL)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000271 # body-part
272 self._fp.write(body_part)
273 # close-delimiter transport-padding
R. David Murray8451c4b2010-10-23 22:19:56 +0000274 self.write(self._NL + '--' + boundary + '--')
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000275 if msg.epilogue is not None:
R. David Murray8451c4b2010-10-23 22:19:56 +0000276 self.write(self._NL)
R David Murray6a31bc62012-07-22 21:47:53 -0400277 if self._mangle_from_:
278 epilogue = fcre.sub('>From ', msg.epilogue)
279 else:
280 epilogue = msg.epilogue
281 self.write(epilogue)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000282
R. David Murraya8f480f2010-01-16 18:30:03 +0000283 def _handle_multipart_signed(self, msg):
284 # The contents of signed parts has to stay unmodified in order to keep
285 # the signature intact per RFC1847 2.1, so we disable header wrapping.
286 # RDM: This isn't enough to completely preserve the part, but it helps.
R David Murrayc27e5222012-05-25 15:01:48 -0400287 p = self.policy
288 self.policy = p.clone(max_line_length=0)
R. David Murraya8f480f2010-01-16 18:30:03 +0000289 try:
R. David Murraya8f480f2010-01-16 18:30:03 +0000290 self._handle_multipart(msg)
291 finally:
R David Murrayc27e5222012-05-25 15:01:48 -0400292 self.policy = p
R. David Murraya8f480f2010-01-16 18:30:03 +0000293
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000294 def _handle_message_delivery_status(self, msg):
295 # We can't just write the headers directly to self's file object
296 # because this will leave an extra newline between the last header
297 # block and the boundary. Sigh.
298 blocks = []
299 for part in msg.get_payload():
R. David Murray96fd54e2010-10-08 15:55:28 +0000300 s = self._new_buffer()
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000301 g = self.clone(s)
R. David Murray719a4492010-11-21 16:53:48 +0000302 g.flatten(part, unixfrom=False, linesep=self._NL)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000303 text = s.getvalue()
R. David Murray8451c4b2010-10-23 22:19:56 +0000304 lines = text.split(self._encoded_NL)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000305 # Strip off the unnecessary trailing empty line
R. David Murray8451c4b2010-10-23 22:19:56 +0000306 if lines and lines[-1] == self._encoded_EMPTY:
307 blocks.append(self._encoded_NL.join(lines[:-1]))
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000308 else:
309 blocks.append(text)
310 # Now join all the blocks with an empty line. This has the lovely
311 # effect of separating each block with an empty line, but not adding
312 # an extra one after the last one.
R. David Murray8451c4b2010-10-23 22:19:56 +0000313 self._fp.write(self._encoded_NL.join(blocks))
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000314
315 def _handle_message(self, msg):
R. David Murray96fd54e2010-10-08 15:55:28 +0000316 s = self._new_buffer()
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000317 g = self.clone(s)
318 # The payload of a message/rfc822 part should be a multipart sequence
319 # of length 1. The zeroth element of the list should be the Message
320 # object for the subpart. Extract that object, stringify it, and
321 # write it out.
R. David Murray57c45ac2010-02-21 04:39:40 +0000322 # Except, it turns out, when it's a string instead, which happens when
323 # and only when HeaderParser is used on a message of mime type
324 # message/rfc822. Such messages are generated by, for example,
325 # Groupwise when forwarding unadorned messages. (Issue 7970.) So
326 # in that case we just emit the string body.
R David Murrayb35c8502011-04-13 16:46:05 -0400327 payload = msg._payload
R. David Murray57c45ac2010-02-21 04:39:40 +0000328 if isinstance(payload, list):
R. David Murray719a4492010-11-21 16:53:48 +0000329 g.flatten(msg.get_payload(0), unixfrom=False, linesep=self._NL)
R. David Murray57c45ac2010-02-21 04:39:40 +0000330 payload = s.getvalue()
R David Murrayb35c8502011-04-13 16:46:05 -0400331 else:
332 payload = self._encode(payload)
R. David Murray57c45ac2010-02-21 04:39:40 +0000333 self._fp.write(payload)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000334
R. David Murray96fd54e2010-10-08 15:55:28 +0000335 # This used to be a module level function; we use a classmethod for this
336 # and _compile_re so we can continue to provide the module level function
337 # for backward compatibility by doing
338 # _make_boudary = Generator._make_boundary
339 # at the end of the module. It *is* internal, so we could drop that...
340 @classmethod
341 def _make_boundary(cls, text=None):
342 # Craft a random boundary. If text is given, ensure that the chosen
343 # boundary doesn't appear in the text.
344 token = random.randrange(sys.maxsize)
345 boundary = ('=' * 15) + (_fmt % token) + '=='
346 if text is None:
347 return boundary
348 b = boundary
349 counter = 0
350 while True:
351 cre = cls._compile_re('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
352 if not cre.search(text):
353 break
354 b = boundary + '.' + str(counter)
355 counter += 1
356 return b
357
358 @classmethod
359 def _compile_re(cls, s, flags):
360 return re.compile(s, flags)
361
362
363class BytesGenerator(Generator):
364 """Generates a bytes version of a Message object tree.
365
366 Functionally identical to the base Generator except that the output is
367 bytes and not string. When surrogates were used in the input to encode
R David Murray3edd22a2011-04-18 13:59:37 -0400368 bytes, these are decoded back to bytes for output. If the policy has
R David Murrayc27e5222012-05-25 15:01:48 -0400369 cte_type set to 7bit, then the message is transformed such that the
370 non-ASCII bytes are properly content transfer encoded, using the charset
371 unknown-8bit.
R. David Murray96fd54e2010-10-08 15:55:28 +0000372
373 The outfp object must accept bytes in its write method.
374 """
375
R. David Murray8451c4b2010-10-23 22:19:56 +0000376 # Bytes versions of this constant for use in manipulating data from
R. David Murray96fd54e2010-10-08 15:55:28 +0000377 # the BytesIO buffer.
R. David Murray8451c4b2010-10-23 22:19:56 +0000378 _encoded_EMPTY = b''
R. David Murray96fd54e2010-10-08 15:55:28 +0000379
380 def write(self, s):
381 self._fp.write(s.encode('ascii', 'surrogateescape'))
382
383 def _new_buffer(self):
384 return BytesIO()
385
R. David Murray8451c4b2010-10-23 22:19:56 +0000386 def _encode(self, s):
387 return s.encode('ascii')
388
R. David Murray96fd54e2010-10-08 15:55:28 +0000389 def _write_headers(self, msg):
390 # This is almost the same as the string version, except for handling
391 # strings with 8bit bytes.
R David Murrayc27e5222012-05-25 15:01:48 -0400392 for h, v in msg.raw_items():
393 self._fp.write(self.policy.fold_binary(h, v))
R. David Murray96fd54e2010-10-08 15:55:28 +0000394 # A blank line always separates headers from body
R. David Murray8451c4b2010-10-23 22:19:56 +0000395 self.write(self._NL)
R. David Murray96fd54e2010-10-08 15:55:28 +0000396
397 def _handle_text(self, msg):
398 # If the string has surrogates the original source was bytes, so
399 # just write it back out.
R. David Murray7372a072011-01-26 21:21:32 +0000400 if msg._payload is None:
401 return
R David Murrayc27e5222012-05-25 15:01:48 -0400402 if _has_surrogates(msg._payload) and not self.policy.cte_type=='7bit':
R David Murray638d40b2012-08-24 11:14:13 -0400403 if self._mangle_from_:
404 msg._payload = fcre.sub(">From ", msg._payload)
R. David Murraybdd2d932011-01-26 02:31:37 +0000405 self.write(msg._payload)
R. David Murray96fd54e2010-10-08 15:55:28 +0000406 else:
407 super(BytesGenerator,self)._handle_text(msg)
408
409 @classmethod
410 def _compile_re(cls, s, flags):
411 return re.compile(s.encode('ascii'), flags)
412
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000413
414
415_FMT = '[Non-text (%(type)s) part of message omitted, filename %(filename)s]'
416
417class DecodedGenerator(Generator):
R. David Murray70a99932010-10-01 20:38:33 +0000418 """Generates a text representation of a message.
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000419
420 Like the Generator base class, except that non-text parts are substituted
421 with a format string representing the part.
422 """
423 def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None):
424 """Like Generator.__init__() except that an additional optional
425 argument is allowed.
426
427 Walks through all subparts of a message. If the subpart is of main
428 type `text', then it prints the decoded payload of the subpart.
429
430 Otherwise, fmt is a format string that is used instead of the message
431 payload. fmt is expanded with the following keywords (in
432 %(keyword)s format):
433
434 type : Full MIME type of the non-text part
435 maintype : Main MIME type of the non-text part
436 subtype : Sub-MIME type of the non-text part
437 filename : Filename of the non-text part
438 description: Description associated with the non-text part
439 encoding : Content transfer encoding of the non-text part
440
441 The default value for fmt is None, meaning
442
443 [Non-text (%(type)s) part of message omitted, filename %(filename)s]
444 """
445 Generator.__init__(self, outfp, mangle_from_, maxheaderlen)
446 if fmt is None:
447 self._fmt = _FMT
448 else:
449 self._fmt = fmt
450
451 def _dispatch(self, msg):
452 for part in msg.walk():
453 maintype = part.get_content_maintype()
454 if maintype == 'text':
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000455 print(part.get_payload(decode=False), file=self)
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000456 elif maintype == 'multipart':
457 # Just skip this
458 pass
459 else:
460 print(self._fmt % {
461 'type' : part.get_content_type(),
462 'maintype' : part.get_content_maintype(),
463 'subtype' : part.get_content_subtype(),
464 'filename' : part.get_filename('[no filename]'),
465 'description': part.get('Content-Description',
466 '[no description]'),
467 'encoding' : part.get('Content-Transfer-Encoding',
468 '[no encoding]'),
469 }, file=self)
470
471
472
R. David Murray96fd54e2010-10-08 15:55:28 +0000473# Helper used by Generator._make_boundary
Christian Heimesa37d4c62007-12-04 23:02:19 +0000474_width = len(repr(sys.maxsize-1))
Guido van Rossum8b3febe2007-08-30 01:15:14 +0000475_fmt = '%%0%dd' % _width
476
R. David Murray96fd54e2010-10-08 15:55:28 +0000477# Backward compatibility
478_make_boundary = Generator._make_boundary