#18324: set_payload now correctly handles binary input. · python/cpython@00ae435 (original) (raw)

`@@ -28,7 +28,7 @@ def encode_base64(msg):

`

28

28

``

29

29

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

`

30

30

` """

`

31

``

`-

orig = msg.get_payload()

`

``

31

`+

orig = msg.get_payload(decode=True)

`

32

32

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

`

33

33

`msg.set_payload(encdata)

`

34

34

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

`

`@@ -40,20 +40,16 @@ def encode_quopri(msg):

`

40

40

``

41

41

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

`

42

42

` """

`

43

``

`-

orig = msg.get_payload()

`

44

``

`-

if isinstance(orig, str):

`

45

``

`-

If it is a string, the model data may have binary data encoded in via

`

46

``

`-

surrogateescape. Convert back to bytes so we can CTE encode it.

`

47

``

`-

orig = orig.encode('ascii', 'surrogateescape')

`

``

43

`+

orig = msg.get_payload(decode=True)

`

48

44

`encdata = _qencode(orig)

`

49

``

`-

msg.set_payload(encdata.decode('ascii', 'surrogateescape'))

`

``

45

`+

msg.set_payload(encdata)

`

50

46

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

`

51

47

``

52

48

``

53

49

``

54

50

`def encode_7or8bit(msg):

`

55

51

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

`

56

``

`-

orig = msg.get_payload()

`

``

52

`+

orig = msg.get_payload(decode=True)

`

57

53

`if orig is None:

`

58

54

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

`

59

55

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

`

`@@ -75,16 +71,8 @@ def encode_7or8bit(msg):

`

75

71

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

`

76

72

`else:

`

77

73

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

`

78

``

`-

if not isinstance(orig, str):

`

79

``

`-

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

`

80

74

``

81

75

``

82

76

``

83

77

`def encode_noop(msg):

`

84

78

`"""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'))

`