ยปCore Development>Code coverage>Lib/email/encoders.py

Python code coverage for Lib/email/encoders.py

#countcontent
1n/a# Copyright (C) 2001-2006 Python Software Foundation
2n/a# Author: Barry Warsaw
3n/a# Contact: email-sig@python.org
4n/a
5n/a"""Encodings and related functions."""
6n/a
7n/a__all__ = [
8n/a 'encode_7or8bit',
9n/a 'encode_base64',
10n/a 'encode_noop',
11n/a 'encode_quopri',
12n/a ]
13n/a
14n/a
15n/afrom base64 import encodebytes as _bencode
16n/afrom quopri import encodestring as _encodestring
17n/a
18n/a
19n/a
20n/adef _qencode(s):
21n/a enc = _encodestring(s, quotetabs=True)
22n/a # Must encode spaces, which quopri.encodestring() doesn't do
23n/a return enc.replace(b' ', b'=20')
24n/a
25n/a
26n/adef encode_base64(msg):
27n/a """Encode the message's payload in Base64.
28n/a
29n/a Also, add an appropriate Content-Transfer-Encoding header.
30n/a """
31n/a orig = msg.get_payload(decode=True)
32n/a encdata = str(_bencode(orig), 'ascii')
33n/a msg.set_payload(encdata)
34n/a msg['Content-Transfer-Encoding'] = 'base64'
35n/a
36n/a
37n/a
38n/adef encode_quopri(msg):
39n/a """Encode the message's payload in quoted-printable.
40n/a
41n/a Also, add an appropriate Content-Transfer-Encoding header.
42n/a """
43n/a orig = msg.get_payload(decode=True)
44n/a encdata = _qencode(orig)
45n/a msg.set_payload(encdata)
46n/a msg['Content-Transfer-Encoding'] = 'quoted-printable'
47n/a
48n/a
49n/a
50n/adef encode_7or8bit(msg):
51n/a """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
52n/a orig = msg.get_payload(decode=True)
53n/a if orig is None:
54n/a # There's no payload. For backwards compatibility we use 7bit
55n/a msg['Content-Transfer-Encoding'] = '7bit'
56n/a return
57n/a # We play a trick to make this go fast. If decoding from ASCII succeeds,
58n/a # we know the data must be 7bit, otherwise treat it as 8bit.
59n/a try:
60n/a orig.decode('ascii')
61n/a except UnicodeError:
62n/a msg['Content-Transfer-Encoding'] = '8bit'
63n/a else:
64n/a msg['Content-Transfer-Encoding'] = '7bit'
65n/a
66n/a
67n/a
68n/adef encode_noop(msg):
69n/a """Do nothing."""