Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/future/backports/email/encoders.py: 45%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

47 statements  

1# Copyright (C) 2001-2006 Python Software Foundation 

2# Author: Barry Warsaw 

3# Contact: email-sig@python.org 

4 

5"""Encodings and related functions.""" 

6from __future__ import unicode_literals 

7from __future__ import division 

8from __future__ import absolute_import 

9from future.builtins import str 

10 

11__all__ = [ 

12 'encode_7or8bit', 

13 'encode_base64', 

14 'encode_noop', 

15 'encode_quopri', 

16 ] 

17 

18 

19try: 

20 from base64 import encodebytes as _bencode 

21except ImportError: 

22 # Py2 compatibility. TODO: test this! 

23 from base64 import encodestring as _bencode 

24from quopri import encodestring as _encodestring 

25 

26 

27def _qencode(s): 

28 enc = _encodestring(s, quotetabs=True) 

29 # Must encode spaces, which quopri.encodestring() doesn't do 

30 return enc.replace(' ', '=20') 

31 

32 

33def encode_base64(msg): 

34 """Encode the message's payload in Base64. 

35 

36 Also, add an appropriate Content-Transfer-Encoding header. 

37 """ 

38 orig = msg.get_payload() 

39 encdata = str(_bencode(orig), 'ascii') 

40 msg.set_payload(encdata) 

41 msg['Content-Transfer-Encoding'] = 'base64' 

42 

43 

44def encode_quopri(msg): 

45 """Encode the message's payload in quoted-printable. 

46 

47 Also, add an appropriate Content-Transfer-Encoding header. 

48 """ 

49 orig = msg.get_payload() 

50 encdata = _qencode(orig) 

51 msg.set_payload(encdata) 

52 msg['Content-Transfer-Encoding'] = 'quoted-printable' 

53 

54 

55def encode_7or8bit(msg): 

56 """Set the Content-Transfer-Encoding header to 7bit or 8bit.""" 

57 orig = msg.get_payload() 

58 if orig is None: 

59 # There's no payload. For backwards compatibility we use 7bit 

60 msg['Content-Transfer-Encoding'] = '7bit' 

61 return 

62 # We play a trick to make this go fast. If encoding/decode to ASCII 

63 # succeeds, we know the data must be 7bit, otherwise treat it as 8bit. 

64 try: 

65 if isinstance(orig, str): 

66 orig.encode('ascii') 

67 else: 

68 orig.decode('ascii') 

69 except UnicodeError: 

70 charset = msg.get_charset() 

71 output_cset = charset and charset.output_charset 

72 # iso-2022-* is non-ASCII but encodes to a 7-bit representation 

73 if output_cset and output_cset.lower().startswith('iso-2022-'): 

74 msg['Content-Transfer-Encoding'] = '7bit' 

75 else: 

76 msg['Content-Transfer-Encoding'] = '8bit' 

77 else: 

78 msg['Content-Transfer-Encoding'] = '7bit' 

79 if not isinstance(orig, str): 

80 msg.set_payload(orig.decode('ascii', 'surrogateescape')) 

81 

82 

83def encode_noop(msg): 

84 """Do nothing.""" 

85 # Well, not quite *nothing*: in Python3 we have to turn bytes into a string 

86 # in our internal surrogateescaped form in order to keep the model 

87 # consistent. 

88 orig = msg.get_payload() 

89 if not isinstance(orig, str): 

90 msg.set_payload(orig.decode('ascii', 'surrogateescape'))