Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
email.test.test_email.Test8BitBytesHandling Class Reference
Collaboration diagram for email.test.test_email.Test8BitBytesHandling:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_known_8bit_CTE
def test_unknown_8bit_CTE
def test_8bit_in_quopri_body
def test_invalid_8bit_in_non_8bit_cte_uses_replace
def test_8bit_in_base64_body
def test_8bit_in_uuencode_body
def test_get_8bit_header
def test_print_8bit_headers
def test_values_with_8bit_headers
def test_items_with_8bit_headers
def test_get_all_with_8bit_headers
def test_get_content_type_with_8bit
def test_get_params_with_8bit
def test_get_rfc2231_params_with_8bit
def test_set_rfc2231_params_with_8bit
def test_del_rfc2231_params_with_8bit
def test_get_payload_with_8bit_cte_header
def test_bytes_generator
def test_bytes_generator_handles_None_body
def test_generator_handles_8bit
def test_bytes_generator_with_unix_from
def test_message_from_binary_file
def test_string_generator_reencodes_to_quopri_when_appropriate
def test_decoded_generator_emits_unicode_body
def test_bytes_feedparser
def test_crlf_flatten
def test_8bit_multipart

Static Public Attributes

tuple bodytest_msg
tuple headertest_headers
tuple headertest_msg
tuple non_latin_bin_msg
tuple non_latin_bin_msg_as7bit_wrapped
tuple non_latin_bin_msg_as7bit = non_latin_bin_msg_as7bit_wrapped.split('\n')
string non_latin_bin_msg_as7bit = '\n'
tuple latin_bin_msg
tuple latin_bin_msg_as7bit
 maxDiff = None

Detailed Description

Definition at line 3056 of file test_email.py.


Member Function Documentation

Definition at line 3126 of file test_email.py.

03126 
03127     def test_8bit_in_base64_body(self):
03128         # Sticking an 8bit byte in a base64 block makes it undecodable by
03129         # normal means, so the block is returned undecoded, but as bytes.
03130         m = self.bodytest_msg.format(charset='utf-8',
03131                                      cte='base64',
03132                                      bodyline='cMO2c3RhbAá=').encode('utf-8')
03133         msg = email.message_from_bytes(m)
03134         self.assertEqual(msg.get_payload(decode=True),
03135                          'cMO2c3RhbAá=\n'.encode('utf-8'))

Here is the call graph for this function:

Definition at line 3093 of file test_email.py.

03093 
03094     def test_8bit_in_quopri_body(self):
03095         # This is non-RFC compliant data...without 'decode' the library code
03096         # decodes the body using the charset from the headers, and because the
03097         # source byte really is utf-8 this works.  This is likely to fail
03098         # against real dirty data (ie: produce mojibake), but the data is
03099         # invalid anyway so it is as good a guess as any.  But this means that
03100         # this test just confirms the current behavior; that behavior is not
03101         # necessarily the best possible behavior.  With 'decode' it is
03102         # returning the raw bytes, so that test should be of correct behavior,
03103         # or at least produce the same result that email4 did.
03104         m = self.bodytest_msg.format(charset='utf-8',
03105                                      cte='quoted-printable',
03106                                      bodyline='p=C3=B6stál').encode('utf-8')
03107         msg = email.message_from_bytes(m)
03108         self.assertEqual(msg.get_payload(), 'p=C3=B6stál\n')
03109         self.assertEqual(msg.get_payload(decode=True),
03110                          'pöstál\n'.encode('utf-8'))

Here is the call graph for this function:

Definition at line 3136 of file test_email.py.

03136 
03137     def test_8bit_in_uuencode_body(self):
03138         # Sticking an 8bit byte in a uuencode block makes it undecodable by
03139         # normal means, so the block is returned undecoded, but as bytes.
03140         m = self.bodytest_msg.format(charset='utf-8',
03141                                      cte='uuencode',
03142                                      bodyline='<,.V<W1A; á ').encode('utf-8')
03143         msg = email.message_from_bytes(m)
03144         self.assertEqual(msg.get_payload(decode=True),
03145                          '<,.V<W1A; á \n'.encode('utf-8'))
03146 

Here is the call graph for this function:

Definition at line 3381 of file test_email.py.

03381 
03382     def test_8bit_multipart(self):
03383         # Issue 11605
03384         source = textwrap.dedent("""\
03385             Date: Fri, 18 Mar 2011 17:15:43 +0100
03386             To: foo@example.com
03387             From: foodwatch-Newsletter <bar@example.com>
03388             Subject: Aktuelles zu Japan, Klonfleisch und Smiley-System
03389             Message-ID: <76a486bee62b0d200f33dc2ca08220ad@localhost.localdomain>
03390             MIME-Version: 1.0
03391             Content-Type: multipart/alternative;
03392                     boundary="b1_76a486bee62b0d200f33dc2ca08220ad"
03393 
03394             --b1_76a486bee62b0d200f33dc2ca08220ad
03395             Content-Type: text/plain; charset="utf-8"
03396             Content-Transfer-Encoding: 8bit
03397 
03398             Guten Tag, ,
03399 
03400             mit großer Betroffenheit verfolgen auch wir im foodwatch-Team die
03401             Nachrichten aus Japan.
03402 
03403 
03404             --b1_76a486bee62b0d200f33dc2ca08220ad
03405             Content-Type: text/html; charset="utf-8"
03406             Content-Transfer-Encoding: 8bit
03407 
03408             <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03409                 "http://www.w3.org/TR/html4/loose.dtd">
03410             <html lang="de">
03411             <head>
03412                     <title>foodwatch - Newsletter</title>
03413             </head>
03414             <body>
03415               <p>mit gro&szlig;er Betroffenheit verfolgen auch wir im foodwatch-Team
03416                  die Nachrichten aus Japan.</p>
03417             </body>
03418             </html>
03419             --b1_76a486bee62b0d200f33dc2ca08220ad--
03420 
03421             """).encode('utf-8')
03422         msg = email.message_from_bytes(source)
03423         s = BytesIO()
03424         g = email.generator.BytesGenerator(s)
03425         g.flatten(msg)
03426         self.assertEqual(s.getvalue(), source)

Here is the call graph for this function:

Definition at line 3365 of file test_email.py.

03365 
03366     def test_bytes_feedparser(self):
03367         bfp = email.feedparser.BytesFeedParser()
03368         for i in range(0, len(self.latin_bin_msg), 10):
03369             bfp.feed(self.latin_bin_msg[i:i+10])
03370         m = bfp.close()
03371         self.assertEqual(str(m), self.latin_bin_msg_as7bit)

Here is the call graph for this function:

Definition at line 3271 of file test_email.py.

03271 
03272     def test_bytes_generator(self):
03273         msg = email.message_from_bytes(self.non_latin_bin_msg)
03274         out = BytesIO()
03275         email.generator.BytesGenerator(out).flatten(msg)
03276         self.assertEqual(out.getvalue(), self.non_latin_bin_msg)

Here is the call graph for this function:

Definition at line 3277 of file test_email.py.

03277 
03278     def test_bytes_generator_handles_None_body(self):
03279         #Issue 11019
03280         msg = email.message.Message()
03281         out = BytesIO()
03282         email.generator.BytesGenerator(out).flatten(msg)
03283         self.assertEqual(out.getvalue(), b"\n")

Here is the call graph for this function:

Definition at line 3303 of file test_email.py.

03303 
03304     def test_bytes_generator_with_unix_from(self):
03305         # The unixfrom contains a current date, so we can't check it
03306         # literally.  Just make sure the first word is 'From' and the
03307         # rest of the message matches the input.
03308         msg = email.message_from_bytes(self.non_latin_bin_msg)
03309         out = BytesIO()
03310         email.generator.BytesGenerator(out).flatten(msg, unixfrom=True)
03311         lines = out.getvalue().split(b'\n')
03312         self.assertEqual(lines[0].split()[0], b'From')
03313         self.assertEqual(b'\n'.join(lines[1:]), self.non_latin_bin_msg)

Here is the call graph for this function:

Definition at line 3372 of file test_email.py.

03372 
03373     def test_crlf_flatten(self):
03374         with openfile('msg_26.txt', 'rb') as fp:
03375             text = fp.read()
03376         msg = email.message_from_bytes(text)
03377         s = BytesIO()
03378         g = email.generator.BytesGenerator(s)
03379         g.flatten(msg, linesep='\r\n')
03380         self.assertEqual(s.getvalue(), text)

Here is the call graph for this function:

Definition at line 3355 of file test_email.py.

03355 
03356     def test_decoded_generator_emits_unicode_body(self):
03357         m = email.message_from_bytes(self.latin_bin_msg)
03358         out = StringIO()
03359         email.generator.DecodedGenerator(out).flatten(m)
03360         #DecodedHeader output contains an extra blank line compared
03361         #to the input message.  RDM: not sure if this is a bug or not,
03362         #but it is not specific to the 8bit->7bit conversion.
03363         self.assertEqual(out.getvalue(),
03364             self.latin_bin_msg.decode('latin-1')+'\n')

Here is the call graph for this function:

Definition at line 3240 of file test_email.py.

03240 
03241     def test_del_rfc2231_params_with_8bit(self):
03242         msg = email.message_from_bytes(textwrap.dedent("""\
03243             Content-Type: text/plain; charset=us-ascii;
03244              title*=us-ascii'en'This%20is%20not%20f\xa7n"""
03245              ).encode('latin-1'))
03246         msg.del_param('title')
03247         self.assertEqual(msg.get_param('title'), None)
03248         self.assertEqual(msg.get_content_maintype(), 'text')

Here is the call graph for this function:

Definition at line 3297 of file test_email.py.

03297 
03298     def test_generator_handles_8bit(self):
03299         msg = email.message_from_bytes(self.non_latin_bin_msg)
03300         out = StringIO()
03301         email.generator.Generator(out).flatten(msg)
03302         self.assertEqual(out.getvalue(), self.non_latin_bin_msg_as7bit_wrapped)

Here is the call graph for this function:

Definition at line 3160 of file test_email.py.

03160 
03161     def test_get_8bit_header(self):
03162         msg = email.message_from_bytes(self.headertest_msg)
03163         self.assertEqual(str(msg.get('to')), 'b\uFFFD\uFFFDz')
03164         self.assertEqual(str(msg['to']), 'b\uFFFD\uFFFDz')

Here is the call graph for this function:

Definition at line 3201 of file test_email.py.

03201 
03202     def test_get_all_with_8bit_headers(self):
03203         msg = email.message_from_bytes(self.headertest_msg)
03204         self.assertListEqual([str(x) for x in msg.get_all('from')],
03205                               ['foo@bar.com',
03206                                'g\uFFFD\uFFFDst'])

Here is the call graph for this function:

Definition at line 3207 of file test_email.py.

03207 
03208     def test_get_content_type_with_8bit(self):
03209         msg = email.message_from_bytes(textwrap.dedent("""\
03210             Content-Type: text/pl\xA7in; charset=utf-8
03211             """).encode('latin-1'))
03212         self.assertEqual(msg.get_content_type(), "text/pl\uFFFDin")
03213         self.assertEqual(msg.get_content_maintype(), "text")
03214         self.assertEqual(msg.get_content_subtype(), "pl\uFFFDin")

Here is the call graph for this function:

Definition at line 3215 of file test_email.py.

03215 
03216     def test_get_params_with_8bit(self):
03217         msg = email.message_from_bytes(
03218             'X-Header: foo=\xa7ne; b\xa7r=two; baz=three\n'.encode('latin-1'))
03219         self.assertEqual(msg.get_params(header='x-header'),
03220            [('foo', '\uFFFDne'), ('b\uFFFDr', 'two'), ('baz', 'three')])
03221         self.assertEqual(msg.get_param('Foo', header='x-header'), '\uFFFdne')
03222         # XXX: someday you might be able to get 'b\xa7r', for now you can't.
03223         self.assertEqual(msg.get_param('b\xa7r', header='x-header'), None)

Here is the call graph for this function:

Definition at line 3249 of file test_email.py.

03249 
03250     def test_get_payload_with_8bit_cte_header(self):
03251         msg = email.message_from_bytes(textwrap.dedent("""\
03252             Content-Transfer-Encoding: b\xa7se64
03253             Content-Type: text/plain; charset=latin-1
03254 
03255             payload
03256             """).encode('latin-1'))
03257         self.assertEqual(msg.get_payload(), 'payload\n')
03258         self.assertEqual(msg.get_payload(decode=True), b'payload\n')

Here is the call graph for this function:

Definition at line 3224 of file test_email.py.

03224 
03225     def test_get_rfc2231_params_with_8bit(self):
03226         msg = email.message_from_bytes(textwrap.dedent("""\
03227             Content-Type: text/plain; charset=us-ascii;
03228              title*=us-ascii'en'This%20is%20not%20f\xa7n"""
03229              ).encode('latin-1'))
03230         self.assertEqual(msg.get_param('title'),
03231             ('us-ascii', 'en', 'This is not f\uFFFDn'))

Here is the call graph for this function:

Definition at line 3111 of file test_email.py.

03111 
03112     def test_invalid_8bit_in_non_8bit_cte_uses_replace(self):
03113         # This is similar to the previous test, but proves that if the 8bit
03114         # byte is undecodeable in the specified charset, it gets replaced
03115         # by the unicode 'unknown' character.  Again, this may or may not
03116         # be the ideal behavior.  Note that if decode=False none of the
03117         # decoders will get involved, so this is the only test we need
03118         # for this behavior.
03119         m = self.bodytest_msg.format(charset='ascii',
03120                                      cte='quoted-printable',
03121                                      bodyline='p=C3=B6stál').encode('utf-8')
03122         msg = email.message_from_bytes(m)
03123         self.assertEqual(msg.get_payload(), 'p=C3=B6st\uFFFD\uFFFDl\n')
03124         self.assertEqual(msg.get_payload(decode=True),
03125                         'pöstál\n'.encode('utf-8'))

Here is the call graph for this function:

Definition at line 3189 of file test_email.py.

03189 
03190     def test_items_with_8bit_headers(self):
03191         msg = email.message_from_bytes(self.headertest_msg)
03192         self.assertListEqual([(str(x), str(y)) for (x, y) in msg.items()],
03193                               [('From', 'foo@bar.com'),
03194                                ('To', 'b\uFFFD\uFFFDz'),
03195                                ('Subject', 'Maintenant je vous '
03196                                   'pr\uFFFD\uFFFDsente '
03197                                   'mon coll\uFFFD\uFFFDgue, le pouf '
03198                                   'c\uFFFD\uFFFDl\uFFFD\uFFFDbre\n'
03199                                   '\tJean de Baddie'),
03200                                ('From', 'g\uFFFD\uFFFDst')])

Here is the call graph for this function:

Definition at line 3075 of file test_email.py.

03075 
03076     def test_known_8bit_CTE(self):
03077         m = self.bodytest_msg.format(charset='utf-8',
03078                                      cte='8bit',
03079                                      bodyline='pöstal').encode('utf-8')
03080         msg = email.message_from_bytes(m)
03081         self.assertEqual(msg.get_payload(), "pöstal\n")
03082         self.assertEqual(msg.get_payload(decode=True),
03083                          "pöstal\n".encode('utf-8'))

Here is the call graph for this function:

Definition at line 3320 of file test_email.py.

03320 
03321     def test_message_from_binary_file(self):
03322         fn = 'test.msg'
03323         self.addCleanup(unlink, fn)
03324         with open(fn, 'wb') as testfile:
03325             testfile.write(self.non_latin_bin_msg)
03326         with open(fn, 'rb') as testfile:
03327             m = email.parser.BytesParser().parse(testfile)
03328         self.assertEqual(str(m), self.non_latin_bin_msg_as7bit)

Here is the call graph for this function:

Definition at line 3165 of file test_email.py.

03165 
03166     def test_print_8bit_headers(self):
03167         msg = email.message_from_bytes(self.headertest_msg)
03168         self.assertEqual(str(msg),
03169                          textwrap.dedent("""\
03170                             From: {}
03171                             To: {}
03172                             Subject: {}
03173                             From: {}
03174 
03175                             Yes, they are flying.
03176                             """).format(*[expected[1] for (_, expected) in
03177                                         self.headertest_headers]))

Here is the call graph for this function:

Definition at line 3232 of file test_email.py.

03232 
03233     def test_set_rfc2231_params_with_8bit(self):
03234         msg = email.message_from_bytes(textwrap.dedent("""\
03235             Content-Type: text/plain; charset=us-ascii;
03236              title*=us-ascii'en'This%20is%20not%20f\xa7n"""
03237              ).encode('latin-1'))
03238         msg.set_param('title', 'test')
03239         self.assertEqual(msg.get_param('title'), 'test')

Here is the call graph for this function:

Definition at line 3351 of file test_email.py.

Here is the call graph for this function:

Definition at line 3084 of file test_email.py.

03084 
03085     def test_unknown_8bit_CTE(self):
03086         m = self.bodytest_msg.format(charset='notavalidcharset',
03087                                      cte='8bit',
03088                                      bodyline='pöstal').encode('utf-8')
03089         msg = email.message_from_bytes(m)
03090         self.assertEqual(msg.get_payload(), "p\uFFFD\uFFFDstal\n")
03091         self.assertEqual(msg.get_payload(decode=True),
03092                          "pöstal\n".encode('utf-8'))

Here is the call graph for this function:

Definition at line 3178 of file test_email.py.

03178 
03179     def test_values_with_8bit_headers(self):
03180         msg = email.message_from_bytes(self.headertest_msg)
03181         self.assertListEqual([str(x) for x in msg.values()],
03182                               ['foo@bar.com',
03183                                'b\uFFFD\uFFFDz',
03184                                'Maintenant je vous pr\uFFFD\uFFFDsente mon '
03185                                    'coll\uFFFD\uFFFDgue, le pouf '
03186                                    'c\uFFFD\uFFFDl\uFFFD\uFFFDbre\n'
03187                                    '\tJean de Baddie',
03188                                "g\uFFFD\uFFFDst"])

Here is the call graph for this function:


Member Data Documentation

Initial value:
textwrap.dedent("""\
    From: foo@bar.com    To: baz    Mime-Version: 1.0    Content-Type: text/plain; charset={charset}    Content-Transfer-Encoding: {cte}    {bodyline}    """)

Definition at line 3065 of file test_email.py.

Initial value:
(
        ('From: foo@bar.com', ('From', 'foo@bar.com')),
        ('To: báz', ('To', '=?unknown-8bit?q?b=C3=A1z?=')),
        ('Subject: Maintenant je vous présente mon collègue, le pouf célèbre\n'
            '\tJean de Baddie',
            ('Subject', '=?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_'
                'coll=C3=A8gue=2C_le_pouf_c=C3=A9l=C3=A8bre?=\n'
                ' =?unknown-8bit?q?_Jean_de_Baddie?=')),
        ('From: göst', ('From', '=?unknown-8bit?b?Z8O2c3Q=?=')),
        )

Definition at line 3147 of file test_email.py.

Initial value:
('\n'.join([src for (src, _) in headertest_headers]) +
        '\nYes, they are flying.\n')

Definition at line 3157 of file test_email.py.

Initial value:
textwrap.dedent("""\
    From: foo@bar.com    To: Dinsdale    Subject: Nudge nudge, wink, wink    Mime-Version: 1.0    Content-Type: text/plain; charset="latin-1"    Content-Transfer-Encoding: 8bit    oh là là, know what I mean, know what I mean?    """)

Definition at line 3329 of file test_email.py.

Initial value:
textwrap.dedent("""\
    From: foo@bar.com    To: Dinsdale    Subject: Nudge nudge, wink, wink    Mime-Version: 1.0    Content-Type: text/plain; charset="iso-8859-1"    Content-Transfer-Encoding: quoted-printable    oh l=E0 l=E0, know what I mean, know what I mean?    """)

Definition at line 3340 of file test_email.py.

Definition at line 3427 of file test_email.py.

Initial value:
textwrap.dedent("""\
    From: foo@bar.com    To: báz    Subject: Maintenant je vous présente mon collègue, le pouf célèbre    \tJean de Baddie    Mime-Version: 1.0    Content-Type: text/plain; charset="utf-8"    Content-Transfer-Encoding: 8bit    Да, они летят.    """)

Definition at line 3259 of file test_email.py.

Definition at line 3314 of file test_email.py.

Definition at line 3318 of file test_email.py.

Initial value:
textwrap.dedent("""\
    From: foo@bar.com    To: =?unknown-8bit?q?b=C3=A1z?=    Subject: =?unknown-8bit?q?Maintenant_je_vous_pr=C3=A9sente_mon_coll=C3=A8gue?=     =?unknown-8bit?q?=2C_le_pouf_c=C3=A9l=C3=A8bre?=     =?unknown-8bit?q?_Jean_de_Baddie?=    Mime-Version: 1.0    Content-Type: text/plain; charset="utf-8"    Content-Transfer-Encoding: base64    0JTQsCwg0L7QvdC4INC70LXRgtGP0YIuCg==    """)

Definition at line 3284 of file test_email.py.


The documentation for this class was generated from the following file: