diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -22,6 +22,8 @@ b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") + # Test for bytearray, issue17842 + eq(base64.encodebytes(bytearray(b'abcd')), b'YWJjZA==\n') self.assertRaises(TypeError, base64.encodebytes, "") def test_decodebytes(self): @@ -30,6 +32,8 @@ eq(base64.decodebytes(b"YQ==\n"), b"a") eq(base64.decodebytes(b"YWI=\n"), b"ab") eq(base64.decodebytes(b"YWJj\n"), b"abc") + # Test for bytearray, issue17842 + eq(base64.decodebytes(bytearray(b"YWJj\n")), b"abc") eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), @@ -93,6 +97,8 @@ b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") + # Test for bytearray, issue17842 + eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==') # Check if passing a str object raises an error self.assertRaises(TypeError, base64.standard_b64encode, "") self.assertRaises(TypeError, base64.standard_b64encode, b"", altchars="") @@ -121,6 +127,8 @@ for data, res in tests.items(): eq(base64.b64decode(data), res) eq(base64.b64decode(data.decode('ascii')), res) + # Test for bytearray, issue17842 + eq(base64.b64decode(bytearray(b"YWJj")), b"abc") # Test with arbitrary alternative characters tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d', @@ -179,6 +187,8 @@ eq(base64.b32encode(b'abc'), b'MFRGG===') eq(base64.b32encode(b'abcd'), b'MFRGGZA=') eq(base64.b32encode(b'abcde'), b'MFRGGZDF') + # Test for bytearray, issue17842 + eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=') self.assertRaises(TypeError, base64.b32encode, "") def test_b32decode(self): @@ -194,6 +204,8 @@ for data, res in tests.items(): eq(base64.b32decode(data), res) eq(base64.b32decode(data.decode('ascii')), res) + # Test for bytearray, issue17842 + eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc') def test_b32decode_casefold(self): eq = self.assertEqual @@ -245,6 +257,8 @@ eq = self.assertEqual eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') eq(base64.b16encode(b'\x00'), b'00') + # Test for bytearray, issue17842 + eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF') self.assertRaises(TypeError, base64.b16encode, "") def test_b16decode(self): @@ -259,6 +273,8 @@ # Case fold eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef') eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') + # Test for bytearray, issue17842 + eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef') def test_decode_nonascii_str(self): decode_funcs = (base64.b64decode,