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

Unified Diff: Lib/test/test_base64.py

Issue 17839: base64 module should use memoryview
Patch Set: Created 6 years, 3 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/base64.py ('k') | Lib/test/test_codecs.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_base64.py Thu May 23 20:25:09 2013 +1000
+++ b/Lib/test/test_base64.py Thu May 23 21:29:00 2013 +1000
@@ -5,6 +5,7 @@
import os
import sys
import subprocess
+from array import array
@@ -24,7 +25,10 @@
b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
# Non-bytes
eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n')
+ eq(base64.encodebytes(memoryview(b'abc')), b'YWJj\n')
+ eq(base64.encodebytes(array('B', b'abc')), b'YWJj\n')
self.assertRaises(TypeError, base64.encodebytes, "")
+ self.assertRaises(TypeError, base64.encodebytes, [])
def test_decodebytes(self):
eq = self.assertEqual
@@ -41,7 +45,10 @@
eq(base64.decodebytes(b''), b'')
# Non-bytes
eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc')
+ eq(base64.decodebytes(memoryview(b'YWJj\n')), b'abc')
+ eq(base64.decodebytes(array('B', b'YWJj\n')), b'abc')
self.assertRaises(TypeError, base64.decodebytes, "")
+ self.assertRaises(TypeError, base64.decodebytes, [])
def test_encode(self):
eq = self.assertEqual
@@ -92,11 +99,18 @@
eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd')
# Non-bytes
eq(base64.b64encode(bytearray(b'abcd')), b'YWJjZA==')
+ eq(base64.b64encode(memoryview(b'abcd')), b'YWJjZA==')
+ eq(base64.b64encode(array('B', b'abcd')), b'YWJjZA==')
eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')),
b'01a*b$cd')
+ eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=memoryview(b'*$')),
+ b'01a*b$cd')
+ eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=array('B', b'*$')),
+ b'01a*b$cd')
+ self.assertRaises(TypeError, base64.b64encode, [])
# Check if passing a str object raises an error
self.assertRaises(TypeError, base64.b64encode, "")
- self.assertRaises(TypeError, base64.b64encode, b"", altchars="")
+ self.assertRaises(TypeError, base64.b64encode, b"", altchars="*$")
# Test standard alphabet
eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=")
eq(base64.standard_b64encode(b"a"), b"YQ==")
@@ -111,12 +125,21 @@
b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
# Non-bytes
eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==')
+ eq(base64.standard_b64encode(memoryview(b'abcd')), b'YWJjZA==')
+ eq(base64.standard_b64encode(array('B', b'abcd')), b'YWJjZA==')
+ self.assertRaises(TypeError, base64.standard_b64encode, [])
# Check if passing a str object raises an error
self.assertRaises(TypeError, base64.standard_b64encode, "")
# Test with 'URL safe' alternative characters
eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd')
# Non-bytes
- eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_cd')
+ eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')),
+ b'01a-b_cd')
+ eq(base64.urlsafe_b64encode(memoryview(b'\xd3V\xbeo\xf7\x1d')),
+ b'01a-b_cd')
+ eq(base64.urlsafe_b64encode(array('B', b'\xd3V\xbeo\xf7\x1d')),
+ b'01a-b_cd')
+ self.assertRaises(TypeError, base64.urlsafe_b64encode, [])
# Check if passing a str object raises an error
self.assertRaises(TypeError, base64.urlsafe_b64encode, "")
@@ -142,6 +165,9 @@
eq(base64.b64decode(data.decode('ascii')), res)
# Non-bytes
eq(base64.b64decode(bytearray(b"YWJj")), b"abc")
+ eq(base64.b64decode(memoryview(b"YWJj")), b"abc")
+ eq(base64.b64decode(array('B', b"YWJj")), b"abc")
+ self.assertRaises(TypeError, base64.b64decode, [])
# Test with arbitrary alternative characters
tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d',
@@ -161,6 +187,9 @@
eq(base64.standard_b64decode(data.decode('ascii')), res)
# Non-bytes
eq(base64.standard_b64decode(bytearray(b"YWJj")), b"abc")
+ eq(base64.standard_b64decode(memoryview(b"YWJj")), b"abc")
+ eq(base64.standard_b64decode(array('B', b"YWJj")), b"abc")
+ self.assertRaises(TypeError, base64.standard_b64decode, [])
# Test with 'URL safe' alternative characters
tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d',
@@ -170,7 +199,13 @@
eq(base64.urlsafe_b64decode(data), res)
eq(base64.urlsafe_b64decode(data.decode('ascii')), res)
# Non-bytes
- eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x1d')
+ eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')),
+ b'\xd3V\xbeo\xf7\x1d')
+ eq(base64.urlsafe_b64decode(memoryview(b'01a-b_cd')),
+ b'\xd3V\xbeo\xf7\x1d')
+ eq(base64.urlsafe_b64decode(array('B', b'01a-b_cd')),
+ b'\xd3V\xbeo\xf7\x1d')
+ self.assertRaises(TypeError, base64.urlsafe_b64decode, [])
def test_b64decode_padding_error(self):
self.assertRaises(binascii.Error, base64.b64decode, b'abc')
@@ -206,7 +241,10 @@
eq(base64.b32encode(b'abcde'), b'MFRGGZDF')
# Non-bytes
eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=')
+ eq(base64.b32encode(memoryview(b'abcd')), b'MFRGGZA=')
+ eq(base64.b32encode(array('B', b'abcd')), b'MFRGGZA=')
self.assertRaises(TypeError, base64.b32encode, "")
+ self.assertRaises(TypeError, base64.b32encode, [])
def test_b32decode(self):
eq = self.assertEqual
@@ -223,6 +261,9 @@
eq(base64.b32decode(data.decode('ascii')), res)
# Non-bytes
eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc')
+ eq(base64.b32decode(memoryview(b'MFRGG===')), b'abc')
+ eq(base64.b32decode(array('B', b'MFRGG===')), b'abc')
+ self.assertRaises(TypeError, base64.b32decode, [])
def test_b32decode_casefold(self):
eq = self.assertEqual
@@ -276,7 +317,10 @@
eq(base64.b16encode(b'\x00'), b'00')
# Non-bytes
eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
+ eq(base64.b16encode(memoryview(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
+ eq(base64.b16encode(array('B', b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
self.assertRaises(TypeError, base64.b16encode, "")
+ self.assertRaises(TypeError, base64.b16encode, [])
def test_b16decode(self):
eq = self.assertEqual
@@ -292,6 +336,15 @@
eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef')
# Non-bytes
eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
+ eq(base64.b16decode(memoryview(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
+ eq(base64.b16decode(array('B', b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
+ eq(base64.b16decode(bytearray(b"0102abcdef"), True),
+ b'\x01\x02\xab\xcd\xef')
+ eq(base64.b16decode(memoryview(b"0102abcdef"), True),
+ b'\x01\x02\xab\xcd\xef')
+ eq(base64.b16decode(array('B', b"0102abcdef"), True),
+ b'\x01\x02\xab\xcd\xef')
+ self.assertRaises(TypeError, base64.b16decode, [])
def test_decode_nonascii_str(self):
decode_funcs = (base64.b64decode,
« no previous file with comments | « Lib/base64.py ('k') | Lib/test/test_codecs.py » ('j') | no next file with comments »

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