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

Delta Between Two Patch Sets: Lib/base64.py

Issue 17839: base64 module should use memoryview
Left Patch Set: Created 6 years, 3 months ago
Right Patch Set: Created 6 years, 3 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/codecs.rst ('k') | Lib/test/test_base64.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 294
295 def decode(input, output): 295 def decode(input, output):
296 """Decode a file; input and output are binary files.""" 296 """Decode a file; input and output are binary files."""
297 while True: 297 while True:
298 line = input.readline() 298 line = input.readline()
299 if not line: 299 if not line:
300 break 300 break
301 s = binascii.a2b_base64(line) 301 s = binascii.a2b_base64(line)
302 output.write(s) 302 output.write(s)
303 303
304 def _input_type_check(s):
305 try:
306 memoryview(s)
307 except TypeError as err:
308 msg = "expected bytes-like object, not %s" % s.__class__.__name__
309 raise TypeError(msg) from err
304 310
305 def encodebytes(s): 311 def encodebytes(s):
306 """Encode a bytestring into a bytestring containing multiple lines 312 """Encode a bytestring into a bytestring containing multiple lines
307 of base-64 data.""" 313 of base-64 data."""
308 if not isinstance(s, bytes_types): 314 _input_type_check(s)
309 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
310 pieces = [] 315 pieces = []
311 for i in range(0, len(s), MAXBINSIZE): 316 for i in range(0, len(s), MAXBINSIZE):
312 chunk = s[i : i + MAXBINSIZE] 317 chunk = s[i : i + MAXBINSIZE]
313 pieces.append(binascii.b2a_base64(chunk)) 318 pieces.append(binascii.b2a_base64(chunk))
314 return b"".join(pieces) 319 return b"".join(pieces)
315 320
316 def encodestring(s): 321 def encodestring(s):
317 """Legacy alias of encodebytes().""" 322 """Legacy alias of encodebytes()."""
318 import warnings 323 import warnings
319 warnings.warn("encodestring() is a deprecated alias, use encodebytes()", 324 warnings.warn("encodestring() is a deprecated alias, use encodebytes()",
320 DeprecationWarning, 2) 325 DeprecationWarning, 2)
321 return encodebytes(s) 326 return encodebytes(s)
322 327
323 328
324 def decodebytes(s): 329 def decodebytes(s):
325 """Decode a bytestring of base-64 data into a bytestring.""" 330 """Decode a bytestring of base-64 data into a bytestring."""
326 if not isinstance(s, bytes_types): 331 _input_type_check(s)
327 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
328 return binascii.a2b_base64(s) 332 return binascii.a2b_base64(s)
329 333
330 def decodestring(s): 334 def decodestring(s):
331 """Legacy alias of decodebytes().""" 335 """Legacy alias of decodebytes()."""
332 import warnings 336 import warnings
333 warnings.warn("decodestring() is a deprecated alias, use decodebytes()", 337 warnings.warn("decodestring() is a deprecated alias, use decodebytes()",
334 DeprecationWarning, 2) 338 DeprecationWarning, 2)
335 return decodebytes(s) 339 return decodebytes(s)
336 340
337 341
(...skipping 29 matching lines...) Expand all
367 print(repr(s0)) 371 print(repr(s0))
368 s1 = encodebytes(s0) 372 s1 = encodebytes(s0)
369 print(repr(s1)) 373 print(repr(s1))
370 s2 = decodebytes(s1) 374 s2 = decodebytes(s1)
371 print(repr(s2)) 375 print(repr(s2))
372 assert s0 == s2 376 assert s0 == s2
373 377
374 378
375 if __name__ == '__main__': 379 if __name__ == '__main__':
376 main() 380 main()
LEFTRIGHT

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