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

Side by Side Diff: Lib/base64.py

Issue 17839: base64 module should use memoryview
Patch Set: Created 6 years, 6 months 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
« no previous file with comments | « no previous file | Lib/test/test_base64.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #! /usr/bin/env python3 1 #! /usr/bin/env python3
2 2
3 """RFC 3548: Base16, Base32, Base64 Data Encodings""" 3 """RFC 3548: Base16, Base32, Base64 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
11 import binascii 11 import binascii
12 12
13 13
14 __all__ = [ 14 __all__ = [
15 # Legacy interface exports traditional RFC 1521 Base64 encodings 15 # Legacy interface exports traditional RFC 1521 Base64 encodings
16 'encode', 'decode', 'encodebytes', 'decodebytes', 16 'encode', 'decode', 'encodebytes', 'decodebytes',
17 # Generalized interface for other encodings 17 # Generalized interface for other encodings
18 'b64encode', 'b64decode', 'b32encode', 'b32decode', 18 'b64encode', 'b64decode', 'b32encode', 'b32decode',
19 'b16encode', 'b16decode', 19 'b16encode', 'b16decode',
20 # Standard Base64 encoding 20 # Standard Base64 encoding
21 'standard_b64encode', 'standard_b64decode', 21 'standard_b64encode', 'standard_b64decode',
22 # Some common Base64 alternatives. As referenced by RFC 3458, see thread 22 # Some common Base64 alternatives. As referenced by RFC 3458, see thread
23 # starting at: 23 # starting at:
24 # 24 #
25 # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html 25 # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
26 'urlsafe_b64encode', 'urlsafe_b64decode', 26 'urlsafe_b64encode', 'urlsafe_b64decode',
27 ] 27 ]
28 28
29 29
30 bytes_types = (bytes, bytearray) # Types acceptable as binary data 30 bytes_types = (bytes, bytearray, memoryview) # Types acceptable as binary data
31 31
32 def _bytes_from_decode_data(s): 32 def _bytes_from_decode_data(s):
33 if isinstance(s, str): 33 if isinstance(s, str):
34 try: 34 try:
35 return s.encode('ascii') 35 return s.encode('ascii')
36 except UnicodeEncodeError: 36 except UnicodeEncodeError:
37 raise ValueError('string argument should contain only ASCII characte rs') 37 raise ValueError('string argument should contain only ASCII characte rs')
38 elif isinstance(s, bytes_types): 38 elif isinstance(s, bytes_types):
39 return s 39 return s
40 else: 40 else:
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 _b32rev = dict([(v[0], k) for k, v in _b32alphabet.items()]) 154 _b32rev = dict([(v[0], k) for k, v in _b32alphabet.items()])
155 155
156 156
157 def b32encode(s): 157 def b32encode(s):
158 """Encode a byte string using Base32. 158 """Encode a byte string using Base32.
159 159
160 s is the byte string to encode. The encoded byte string is returned. 160 s is the byte string to encode. The encoded byte string is returned.
161 """ 161 """
162 if not isinstance(s, bytes_types): 162 if not isinstance(s, bytes_types):
163 raise TypeError("expected bytes, not %s" % s.__class__.__name__) 163 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
164 if isinstance(s, memoryview):
165 s = s.tobytes()
164 quanta, leftover = divmod(len(s), 5) 166 quanta, leftover = divmod(len(s), 5)
165 # Pad the last quantum with zero bits if necessary 167 # Pad the last quantum with zero bits if necessary
166 if leftover: 168 if leftover:
167 s = s + bytes(5 - leftover) # Don't use += ! 169 s = s + bytes(5 - leftover) # Don't use += !
168 quanta += 1 170 quanta += 1
169 encoded = bytes() 171 encoded = bytes()
170 for i in range(quanta): 172 for i in range(quanta):
171 # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this 173 # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
172 # code is to process the 40 bits in units of 5 bits. So we take the 1 174 # code is to process the 40 bits in units of 5 bits. So we take the 1
173 # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover 175 # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 print(repr(s0)) 403 print(repr(s0))
402 s1 = encodebytes(s0) 404 s1 = encodebytes(s0)
403 print(repr(s1)) 405 print(repr(s1))
404 s2 = decodebytes(s1) 406 s2 = decodebytes(s1)
405 print(repr(s2)) 407 print(repr(s2))
406 assert s0 == s2 408 assert s0 == s2
407 409
408 410
409 if __name__ == '__main__': 411 if __name__ == '__main__':
410 main() 412 main()
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_base64.py » ('j') | no next file with comments »

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