mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Make binascii use byte strings everywhere (in and out).
This commit is contained in:
parent
6dd15d3ab0
commit
0e225aa09b
2 changed files with 116 additions and 89 deletions
|
@ -7,10 +7,10 @@ import binascii
|
|||
class BinASCIITest(unittest.TestCase):
|
||||
|
||||
# Create binary test data
|
||||
data = "The quick brown fox jumps over the lazy dog.\r\n"
|
||||
data = b"The quick brown fox jumps over the lazy dog.\r\n"
|
||||
# Be slow so we don't depend on other modules
|
||||
data += "".join(map(chr, range(256)))
|
||||
data += "\r\nHello world.\n"
|
||||
data += bytes(range(256))
|
||||
data += b"\r\nHello world.\n"
|
||||
|
||||
def test_exceptions(self):
|
||||
# Check module exceptions
|
||||
|
@ -40,10 +40,10 @@ class BinASCIITest(unittest.TestCase):
|
|||
b = self.data[i:i+MAX_BASE64]
|
||||
a = binascii.b2a_base64(b)
|
||||
lines.append(a)
|
||||
res = ""
|
||||
res = bytes()
|
||||
for line in lines:
|
||||
b = binascii.a2b_base64(line)
|
||||
res = res + b
|
||||
res += b
|
||||
self.assertEqual(res, self.data)
|
||||
|
||||
def test_base64invalid(self):
|
||||
|
@ -56,24 +56,23 @@ class BinASCIITest(unittest.TestCase):
|
|||
a = binascii.b2a_base64(b)
|
||||
lines.append(a)
|
||||
|
||||
fillers = ""
|
||||
valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
|
||||
fillers = bytes()
|
||||
valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
|
||||
for i in range(256):
|
||||
c = chr(i)
|
||||
if c not in valid:
|
||||
fillers += c
|
||||
if i not in valid:
|
||||
fillers.append(i)
|
||||
def addnoise(line):
|
||||
noise = fillers
|
||||
ratio = len(line) // len(noise)
|
||||
res = ""
|
||||
res = bytes()
|
||||
while line and noise:
|
||||
if len(line) // len(noise) > ratio:
|
||||
c, line = line[0], line[1:]
|
||||
else:
|
||||
c, noise = noise[0], noise[1:]
|
||||
res += c
|
||||
res.append(c)
|
||||
return res + noise + line
|
||||
res = ""
|
||||
res = bytes()
|
||||
for line in map(addnoise, lines):
|
||||
b = binascii.a2b_base64(line)
|
||||
res += b
|
||||
|
@ -81,7 +80,7 @@ class BinASCIITest(unittest.TestCase):
|
|||
|
||||
# Test base64 with just invalid characters, which should return
|
||||
# empty strings. TBD: shouldn't it raise an exception instead ?
|
||||
self.assertEqual(binascii.a2b_base64(fillers), '')
|
||||
self.assertEqual(binascii.a2b_base64(fillers), b'')
|
||||
|
||||
def test_uu(self):
|
||||
MAX_UU = 45
|
||||
|
@ -90,23 +89,23 @@ class BinASCIITest(unittest.TestCase):
|
|||
b = self.data[i:i+MAX_UU]
|
||||
a = binascii.b2a_uu(b)
|
||||
lines.append(a)
|
||||
res = ""
|
||||
res = bytes()
|
||||
for line in lines:
|
||||
b = binascii.a2b_uu(line)
|
||||
res += b
|
||||
self.assertEqual(res, self.data)
|
||||
|
||||
self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31)
|
||||
self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32)
|
||||
self.assertEqual(binascii.a2b_uu("\xff"), "\x00"*31)
|
||||
self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00")
|
||||
self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!")
|
||||
self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31)
|
||||
self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32)
|
||||
self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31)
|
||||
self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00")
|
||||
self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!")
|
||||
|
||||
self.assertRaises(binascii.Error, binascii.b2a_uu, 46*"!")
|
||||
self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!")
|
||||
|
||||
def test_crc32(self):
|
||||
crc = binascii.crc32("Test the CRC-32 of")
|
||||
crc = binascii.crc32(" this string.", crc)
|
||||
crc = binascii.crc32(b"Test the CRC-32 of")
|
||||
crc = binascii.crc32(b" this string.", crc)
|
||||
self.assertEqual(crc, 1571220330)
|
||||
|
||||
self.assertRaises(TypeError, binascii.crc32)
|
||||
|
@ -115,16 +114,16 @@ class BinASCIITest(unittest.TestCase):
|
|||
|
||||
def test_hex(self):
|
||||
# test hexlification
|
||||
s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
|
||||
s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
|
||||
t = binascii.b2a_hex(s)
|
||||
u = binascii.a2b_hex(t)
|
||||
self.assertEqual(s, u)
|
||||
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1])
|
||||
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + 'q')
|
||||
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + b'q')
|
||||
|
||||
# Verify the treatment of Unicode strings
|
||||
if test_support.have_unicode:
|
||||
self.assertEqual(binascii.hexlify(str('a', 'ascii')), '61')
|
||||
self.assertEqual(binascii.hexlify('a'), b'61')
|
||||
|
||||
def test_qp(self):
|
||||
# A test for SF bug 534347 (segfaults without the proper fix)
|
||||
|
@ -134,28 +133,29 @@ class BinASCIITest(unittest.TestCase):
|
|||
pass
|
||||
else:
|
||||
self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
|
||||
self.assertEqual(binascii.a2b_qp("= "), "= ")
|
||||
self.assertEqual(binascii.a2b_qp("=="), "=")
|
||||
self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
|
||||
self.assertEqual(binascii.a2b_qp(b"= "), b"= ")
|
||||
self.assertEqual(binascii.a2b_qp(b"=="), b"=")
|
||||
self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX")
|
||||
self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
|
||||
self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
|
||||
self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00")
|
||||
self.assertEqual(
|
||||
binascii.b2a_qp("\xff\r\n\xff\n\xff"),
|
||||
"=FF\r\n=FF\r\n=FF"
|
||||
binascii.b2a_qp(b"\xff\r\n\xff\n\xff"),
|
||||
b"=FF\r\n=FF\r\n=FF"
|
||||
)
|
||||
self.assertEqual(
|
||||
binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
|
||||
"0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
|
||||
binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"),
|
||||
b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF"
|
||||
)
|
||||
|
||||
self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')
|
||||
self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')
|
||||
self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
|
||||
self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True), 'foo=09bar=09\n')
|
||||
self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n')
|
||||
self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n')
|
||||
self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n')
|
||||
self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True),
|
||||
b'foo=09bar=09\n')
|
||||
|
||||
self.assertEqual(binascii.b2a_qp('.'), '=2E')
|
||||
self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')
|
||||
self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n')
|
||||
self.assertEqual(binascii.b2a_qp(b'.'), b'=2E')
|
||||
self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n')
|
||||
self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n')
|
||||
|
||||
def test_empty_string(self):
|
||||
# A test for SF bug #1022953. Make sure SystemError is not raised.
|
||||
|
@ -164,7 +164,10 @@ class BinASCIITest(unittest.TestCase):
|
|||
'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu',
|
||||
'rledecode_hqx']:
|
||||
f = getattr(binascii, n)
|
||||
f('')
|
||||
try:
|
||||
f(b'')
|
||||
except SystemError as err:
|
||||
self.fail("%s(b'') raises SystemError: %s" % (n, err))
|
||||
binascii.crc_hqx('', 0)
|
||||
|
||||
def test_main():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue