Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15b16a3

Browse files
committed
Issue #4387: binascii now refuses to accept str as binary input.
1 parent eae122b commit 15b16a3

6 files changed

Lines changed: 34 additions & 32 deletions

File tree

Lib/email/test/test_email.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ def test_get_body_encoding_with_uppercase_charset(self):
22792279
eq(charsets[0], 'utf-8')
22802280
charset = Charset(charsets[0])
22812281
eq(charset.get_body_encoding(), 'base64')
2282-
msg.set_payload('hello world', charset=charset)
2282+
msg.set_payload(b'hello world', charset=charset)
22832283
eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n')
22842284
eq(msg.get_payload(decode=True), b'hello world')
22852285
eq(msg['content-transfer-encoding'], 'base64')
@@ -2539,7 +2539,7 @@ class TestBase64(unittest.TestCase):
25392539
def test_len(self):
25402540
eq = self.assertEqual
25412541
eq(base64mime.header_length('hello'),
2542-
len(base64mime.body_encode('hello', eol='')))
2542+
len(base64mime.body_encode(b'hello', eol='')))
25432543
for size in range(15):
25442544
if size == 0 : bsize = 0
25452545
elif size <= 3 : bsize = 4
@@ -2556,19 +2556,19 @@ def test_decode(self):
25562556

25572557
def test_encode(self):
25582558
eq = self.assertEqual
2559-
eq(base64mime.body_encode(''), '')
2560-
eq(base64mime.body_encode('hello'), 'aGVsbG8=\n')
2559+
eq(base64mime.body_encode(b''), b'')
2560+
eq(base64mime.body_encode(b'hello'), 'aGVsbG8=\n')
25612561
# Test the binary flag
2562-
eq(base64mime.body_encode('hello\n'), 'aGVsbG8K\n')
2562+
eq(base64mime.body_encode(b'hello\n'), 'aGVsbG8K\n')
25632563
# Test the maxlinelen arg
2564-
eq(base64mime.body_encode('xxxx ' * 20, maxlinelen=40), """\
2564+
eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40), """\
25652565
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
25662566
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
25672567
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
25682568
eHh4eCB4eHh4IA==
25692569
""")
25702570
# Test the eol argument
2571-
eq(base64mime.body_encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'),
2571+
eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40, eol='\r\n'),
25722572
"""\
25732573
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
25742574
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
@@ -2734,7 +2734,7 @@ def test_body_encode(self):
27342734
eq('hello w=F6rld', c.body_encode('hello w\xf6rld'))
27352735
# Try a charset with Base64 body encoding
27362736
c = Charset('utf-8')
2737-
eq('aGVsbG8gd29ybGQ=\n', c.body_encode('hello world'))
2737+
eq('aGVsbG8gd29ybGQ=\n', c.body_encode(b'hello world'))
27382738
# Try a charset with None body encoding
27392739
c = Charset('us-ascii')
27402740
eq('hello world', c.body_encode('hello world'))

Lib/test/test_binascii.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_hex(self):
121121
self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1])
122122
self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q')
123123

124-
self.assertEqual(binascii.hexlify('a'), b'61')
124+
self.assertEqual(binascii.hexlify(b'a'), b'61')
125125

126126
def test_qp(self):
127127
# A test for SF bug 534347 (segfaults without the proper fix)
@@ -166,7 +166,15 @@ def test_empty_string(self):
166166
f(b'')
167167
except SystemError as err:
168168
self.fail("%s(b'') raises SystemError: %s" % (n, err))
169-
binascii.crc_hqx('', 0)
169+
binascii.crc_hqx(b'', 0)
170+
171+
def test_no_binary_strings(self):
172+
# b2a_ must not accept strings
173+
for f in (binascii.b2a_uu, binascii.b2a_base64,
174+
binascii.b2a_hqx, binascii.b2a_qp,
175+
binascii.hexlify, binascii.rlecode_hqx,
176+
binascii.crc_hqx, binascii.crc32):
177+
self.assertRaises(TypeError, f, "test")
170178

171179
def test_main():
172180
support.run_unittest(BinASCIITest)

Lib/test/test_descr.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,22 +3097,14 @@ def test_buffer_inheritance(self):
30973097
import binascii
30983098
# SF bug [#470040] ParseTuple t# vs subclasses.
30993099

3100-
class MyStr(str):
3100+
class MyBytes(bytes):
31013101
pass
3102-
base = 'abc'
3103-
m = MyStr(base)
3102+
base = b'abc'
3103+
m = MyBytes(base)
31043104
# b2a_hex uses the buffer interface to get its argument's value, via
31053105
# PyArg_ParseTuple 't#' code.
31063106
self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
31073107

3108-
# It's not clear that unicode will continue to support the character
3109-
# buffer interface, and this test will fail if that's taken away.
3110-
class MyUni(str):
3111-
pass
3112-
base = 'abc'
3113-
m = MyUni(base)
3114-
self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
3115-
31163108
class MyInt(int):
31173109
pass
31183110
m = MyInt(42)
@@ -3129,7 +3121,7 @@ def test_str_of_str_subclass(self):
31293121

31303122
class octetstring(str):
31313123
def __str__(self):
3132-
return binascii.b2a_hex(self).decode("ascii")
3124+
return binascii.b2a_hex(self.encode('ascii')).decode("ascii")
31333125
def __repr__(self):
31343126
return self + " repr"
31353127

Lib/test/test_zlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def test_crc32_adler32_unsigned(self):
4848
self.assertEqual(zlib.adler32('spam'), 72286642)
4949

5050
def test_same_as_binascii_crc32(self):
51-
foo = 'abcdefghijklmnop'
51+
foo = b'abcdefghijklmnop'
5252
crc = 2486878355
5353
self.assertEqual(binascii.crc32(foo), crc)
5454
self.assertEqual(zlib.crc32(foo), crc)
55-
self.assertEqual(binascii.crc32('spam'), zlib.crc32('spam'))
55+
self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam'))
5656

5757

5858

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #4387: binascii now refuses to accept str as binary input.
28+
2729
- Issue #4073: Add 2to3 support to build_scripts, refactor that support
2830
in build_py.
2931

Modules/binascii.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ binascii_b2a_uu(PyObject *self, PyObject *args)
282282
PyObject *rv;
283283
Py_ssize_t bin_len;
284284

285-
if ( !PyArg_ParseTuple(args, "s*:b2a_uu", &pbin) )
285+
if ( !PyArg_ParseTuple(args, "y*:b2a_uu", &pbin) )
286286
return NULL;
287287
bin_data = pbin.buf;
288288
bin_len = pbin.len;
@@ -478,7 +478,7 @@ binascii_b2a_base64(PyObject *self, PyObject *args)
478478
PyObject *rv;
479479
Py_ssize_t bin_len;
480480

481-
if ( !PyArg_ParseTuple(args, "s*:b2a_base64", &pbuf) )
481+
if ( !PyArg_ParseTuple(args, "y*:b2a_base64", &pbuf) )
482482
return NULL;
483483
bin_data = pbuf.buf;
484484
bin_len = pbuf.len;
@@ -618,7 +618,7 @@ binascii_rlecode_hqx(PyObject *self, PyObject *args)
618618
unsigned char ch;
619619
Py_ssize_t in, inend, len;
620620

621-
if ( !PyArg_ParseTuple(args, "s*:rlecode_hqx", &pbuf) )
621+
if ( !PyArg_ParseTuple(args, "y*:rlecode_hqx", &pbuf) )
622622
return NULL;
623623
in_data = pbuf.buf;
624624
len = pbuf.len;
@@ -684,7 +684,7 @@ binascii_b2a_hqx(PyObject *self, PyObject *args)
684684
PyObject *rv;
685685
Py_ssize_t len;
686686

687-
if ( !PyArg_ParseTuple(args, "s*:b2a_hqx", &pbin) )
687+
if ( !PyArg_ParseTuple(args, "y*:b2a_hqx", &pbin) )
688688
return NULL;
689689
bin_data = pbin.buf;
690690
len = pbin.len;
@@ -856,7 +856,7 @@ binascii_crc_hqx(PyObject *self, PyObject *args)
856856
unsigned int crc;
857857
Py_ssize_t len;
858858

859-
if ( !PyArg_ParseTuple(args, "s*i:crc_hqx", &pin, &crc) )
859+
if ( !PyArg_ParseTuple(args, "y*i:crc_hqx", &pin, &crc) )
860860
return NULL;
861861
bin_data = pin.buf;
862862
len = pin.len;
@@ -883,7 +883,7 @@ binascii_crc32(PyObject *self, PyObject *args)
883883
Py_ssize_t len;
884884
int signed_val;
885885

886-
if (!PyArg_ParseTuple(args, "s*|I:crc32", &pbuf, &crc32val))
886+
if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
887887
return NULL;
888888
buf = (Byte*)pbuf.buf;
889889
len = pbuf.len;
@@ -1047,7 +1047,7 @@ binascii_hexlify(PyObject *self, PyObject *args)
10471047
char* retbuf;
10481048
Py_ssize_t i, j;
10491049

1050-
if (!PyArg_ParseTuple(args, "s*:b2a_hex", &parg))
1050+
if (!PyArg_ParseTuple(args, "y*:b2a_hex", &parg))
10511051
return NULL;
10521052
argbuf = parg.buf;
10531053
arglen = parg.len;
@@ -1300,7 +1300,7 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
13001300
int crlf = 0;
13011301
unsigned char *p;
13021302

1303-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|iii", kwlist, &pdata,
1303+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|iii", kwlist, &pdata,
13041304
&quotetabs, &istext, &header))
13051305
return NULL;
13061306
data = pdata.buf;

0 commit comments

Comments
 (0)