Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(128930)

Side by Side Diff: Lib/base64.py

Issue 25357: Add an optional newline parameter to binascii.b2a_base64() to not add a newline
Patch Set: Created 4 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #! /usr/bin/env python3 1 #! /usr/bin/env python3
2 2
3 """Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings""" 3 """Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings"""
4 4
5 # Modified 04-Oct-1995 by Jack Jansen to use binascii module 5 # Modified 04-Oct-1995 by Jack Jansen to use binascii module
6 # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support 6 # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
7 # Modified 22-May-2007 by Guido van Rossum to use bytes everywhere 7 # Modified 22-May-2007 by Guido van Rossum to use bytes everywhere
8 8
9 import re 9 import re
10 import struct 10 import struct
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 def b64encode(s, altchars=None): 51 def b64encode(s, altchars=None):
52 """Encode a byte string using Base64. 52 """Encode a byte string using Base64.
53 53
54 s is the byte string to encode. Optional altchars must be a byte 54 s is the byte string to encode. Optional altchars must be a byte
55 string of length 2 which specifies an alternative alphabet for the 55 string of length 2 which specifies an alternative alphabet for the
56 '+' and '/' characters. This allows an application to 56 '+' and '/' characters. This allows an application to
57 e.g. generate url or filesystem safe Base64 strings. 57 e.g. generate url or filesystem safe Base64 strings.
58 58
59 The encoded byte string is returned. 59 The encoded byte string is returned.
60 """ 60 """
61 # Strip off the trailing newline 61 encoded = binascii.b2a_base64(s, newline=False)
62 encoded = binascii.b2a_base64(s)[:-1]
63 if altchars is not None: 62 if altchars is not None:
64 assert len(altchars) == 2, repr(altchars) 63 assert len(altchars) == 2, repr(altchars)
65 return encoded.translate(bytes.maketrans(b'+/', altchars)) 64 return encoded.translate(bytes.maketrans(b'+/', altchars))
66 return encoded 65 return encoded
67 66
68 67
69 def b64decode(s, altchars=None, validate=False): 68 def b64decode(s, altchars=None, validate=False):
70 """Decode a Base64 encoded byte string. 69 """Decode a Base64 encoded byte string.
71 70
72 s is the byte string to decode. Optional altchars must be a 71 s is the byte string to decode. Optional altchars must be a
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 print(repr(s0)) 592 print(repr(s0))
594 s1 = encodebytes(s0) 593 s1 = encodebytes(s0)
595 print(repr(s1)) 594 print(repr(s1))
596 s2 = decodebytes(s1) 595 s2 = decodebytes(s1)
597 print(repr(s2)) 596 print(repr(s2))
598 assert s0 == s2 597 assert s0 == s2
599 598
600 599
601 if __name__ == '__main__': 600 if __name__ == '__main__':
602 main() 601 main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+