Skip to content

Commit cc6d485

Browse files
Remove parameters strict_mode and newline.
1 parent 99e0bde commit cc6d485

File tree

5 files changed

+88
-321
lines changed

5 files changed

+88
-321
lines changed

Doc/library/binascii.rst

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,12 @@ The :mod:`binascii` module defines the following functions:
145145
.. versionadded:: next
146146

147147

148-
.. function:: a2b_base85(string, /, *, strict_mode=False)
148+
.. function:: a2b_base85(string, /)
149149

150150
Convert Base85 data back to binary and return the binary data.
151151
More than one line may be passed at a time.
152152

153-
If *strict_mode* is true, only valid Base85 data will be converted.
154153
Invalid Base85 data will raise :exc:`binascii.Error`.
155-
156154
Valid Base85 data contains characters from the Base85 alphabet in groups
157155
of five (except for the final group, which may have from two to five
158156
characters). Each group encodes 32 bits of binary data in the range from
@@ -161,26 +159,22 @@ The :mod:`binascii` module defines the following functions:
161159
.. versionadded:: next
162160

163161

164-
.. function:: b2a_base85(data, /, *, pad=False, newline=True)
162+
.. function:: b2a_base85(data, /, *, pad=False)
165163

166164
Convert binary data to a line of ASCII characters in Base85 coding.
167165
The return value is the converted line.
168166

169167
If *pad* is true, the input is padded to a multiple of 4 before encoding.
170168

171-
If *newline* is true, a newline char is appended to the result.
172-
173169
.. versionadded:: next
174170

175171

176-
.. function:: a2b_z85(string, /, *, strict_mode=False)
172+
.. function:: a2b_z85(string, /)
177173

178174
Convert Z85 data back to binary and return the binary data.
179175
More than one line may be passed at a time.
180176

181-
If *strict_mode* is true, only valid Z85 data will be converted.
182177
Invalid Z85 data will raise :exc:`binascii.Error`.
183-
184178
Valid Z85 data contains characters from the Z85 alphabet in groups
185179
of five (except for the final group, which may have from two to five
186180
characters). Each group encodes 32 bits of binary data in the range from
@@ -191,15 +185,13 @@ The :mod:`binascii` module defines the following functions:
191185
.. versionadded:: next
192186

193187

194-
.. function:: b2a_z85(data, /, *, pad=False, newline=True)
188+
.. function:: b2a_z85(data, /, *, pad=False)
195189

196190
Convert binary data to a line of ASCII characters in Z85 coding.
197191
The return value is the converted line.
198192

199193
If *pad* is true, the input is padded to a multiple of 4 before encoding.
200194

201-
If *newline* is true, a newline char is appended to the result.
202-
203195
See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information.
204196

205197
.. versionadded:: next

Lib/base64.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,25 +382,25 @@ def b85encode(b, pad=False):
382382
If pad is true, the input is padded with b'\\0' so its length is a multiple of
383383
4 bytes before encoding.
384384
"""
385-
return binascii.b2a_base85(b, pad=pad, newline=False)
385+
return binascii.b2a_base85(b, pad=pad)
386386

387387
def b85decode(b):
388388
"""Decode the base85-encoded bytes-like object or ASCII string b
389389
390390
The result is returned as a bytes object.
391391
"""
392-
return binascii.a2b_base85(b, strict_mode=True)
392+
return binascii.a2b_base85(b)
393393

394394
def z85encode(s, pad=False):
395395
"""Encode bytes-like object b in z85 format and return a bytes object."""
396-
return binascii.b2a_z85(s, pad=pad, newline=False)
396+
return binascii.b2a_z85(s, pad=pad)
397397

398398
def z85decode(s):
399399
"""Decode the z85-encoded bytes-like object or ASCII string b
400400
401401
The result is returned as a bytes object.
402402
"""
403-
return binascii.a2b_z85(s, strict_mode=True)
403+
return binascii.a2b_z85(s)
404404

405405
# Legacy interface. This code could be cleaned up since I don't believe
406406
# binascii has any line length limitations. It just doesn't seem worth it

Lib/test/test_binascii.py

Lines changed: 30 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -536,49 +536,30 @@ def test_base85_valid(self):
536536
self.assertEqual(res, self.rawdata)
537537

538538
# Test decoding inputs with length 1 mod 5
539-
self.assertEqual(binascii.a2b_base85(self.type2test(b"a")), b"")
540-
self.assertEqual(binascii.a2b_base85(self.type2test(b" b ")), b"")
541-
self.assertEqual(binascii.a2b_base85(self.type2test(b"b/Y\"*,j'Nc")), b"test")
542-
543-
def test_base85_invalid(self):
544-
# Test base85 with invalid characters interleaved
545-
lines, i = [], 0
546-
for k in range(1, len(self.rawdata) + 1):
547-
b = self.type2test(self.rawdata[i:i + k])
548-
a = binascii.b2a_base85(b)
549-
lines.append(a)
550-
i += k
551-
if i >= len(self.rawdata):
552-
break
553-
554-
fillers = bytearray()
555-
valid = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
556-
b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
557-
for i in range(256):
558-
if i not in valid:
559-
fillers.append(i)
560-
def addnoise(line):
561-
res = bytearray()
562-
for i in range(len(line)):
563-
res.append(line[i])
564-
for j in range(i, len(fillers), len(line)):
565-
res.append(fillers[j])
566-
return res
567-
res = bytearray()
568-
for line in map(addnoise, lines):
569-
a = self.type2test(line)
570-
b = binascii.a2b_base85(a)
571-
res += b
572-
self.assertEqual(res, self.rawdata)
539+
self.assertEqual(binascii.a2b_base85(self.type2test(b'a')), b'')
540+
self.assertEqual(binascii.a2b_base85(self.type2test(b'a')), b'')
541+
self.assertEqual(binascii.a2b_base85(self.type2test(b'ab')), b'q')
542+
self.assertEqual(binascii.a2b_base85(self.type2test(b'abc')), b'qa')
543+
self.assertEqual(binascii.a2b_base85(self.type2test(b'abcd')), b'qa\x9e')
544+
self.assertEqual(binascii.a2b_base85(self.type2test(b'abcde')), b'qa\x9e\xb6')
545+
self.assertEqual(binascii.a2b_base85(self.type2test(b'abcdef')), b'qa\x9e\xb6')
573546

574547
def test_base85_errors(self):
575548
def _assertRegexTemplate(assert_regex, data, **kwargs):
576549
with self.assertRaisesRegex(binascii.Error, assert_regex):
577550
binascii.a2b_base85(self.type2test(data), **kwargs)
578551

552+
def assertNonBase85Data(data):
553+
_assertRegexTemplate(r"(?i)bad base85 character", data)
554+
579555
def assertOverflow(data):
580556
_assertRegexTemplate(r"(?i)base85 overflow", data)
581557

558+
assertNonBase85Data(b"\xda")
559+
assertNonBase85Data(b"00\0\0")
560+
assertNonBase85Data(b"Z )*")
561+
assertNonBase85Data(b"bY*jNb0Hyq\n")
562+
582563
# Test base85 with out-of-range encoded value
583564
assertOverflow(b"}")
584565
assertOverflow(b"|O")
@@ -599,29 +580,6 @@ def test_base85_pad(self):
599580
b_pad_expected = b + b"\0" * padding
600581
self.assertEqual(b_pad, b_pad_expected)
601582

602-
def test_base85_strict_mode(self):
603-
# Test base85 with strict mode on
604-
def assertNonBase85Data(data, expected):
605-
data = self.type2test(data)
606-
with self.assertRaisesRegex(binascii.Error, r"(?i)bad base85 character"):
607-
binascii.a2b_base85(data, strict_mode=True)
608-
default_res = binascii.a2b_base85(data)
609-
non_strict_res = binascii.a2b_base85(data, strict_mode=False)
610-
self.assertEqual(default_res, non_strict_res)
611-
self.assertEqual(non_strict_res, expected)
612-
613-
assertNonBase85Data(b"\xda", b"")
614-
assertNonBase85Data(b"00\0\0", b"\0")
615-
assertNonBase85Data(b"Z )*", b"ok")
616-
assertNonBase85Data(b"bY*jNb0Hyq\n", b"tests!!~")
617-
618-
def test_base85_newline(self):
619-
# Test base85 newline parameter
620-
b = self.type2test(b"t3s\t ")
621-
self.assertEqual(binascii.b2a_base85(b), b"bTe}aAO\n")
622-
self.assertEqual(binascii.b2a_base85(b, newline=True), b"bTe}aAO\n")
623-
self.assertEqual(binascii.b2a_base85(b, newline=False), b"bTe}aAO")
624-
625583
def test_z85_valid(self):
626584
# Test Z85 with valid data
627585
lines, i = [], 0
@@ -640,49 +598,30 @@ def test_z85_valid(self):
640598
self.assertEqual(res, self.rawdata)
641599

642600
# Test decoding inputs with length 1 mod 5
643-
self.assertEqual(binascii.a2b_z85(self.type2test(b"a")), b"")
644-
self.assertEqual(binascii.a2b_z85(self.type2test(b" b ")), b"")
645-
self.assertEqual(binascii.a2b_z85(self.type2test(b"B y,/;J_n\\c")), b"test")
646-
647-
def test_z85_invalid(self):
648-
# Test Z85 with invalid characters interleaved
649-
lines, i = [], 0
650-
for k in range(1, len(self.rawdata) + 1):
651-
b = self.type2test(self.rawdata[i:i + k])
652-
a = binascii.b2a_z85(b)
653-
lines.append(a)
654-
i += k
655-
if i >= len(self.rawdata):
656-
break
657-
658-
fillers = bytearray()
659-
valid = b"0123456789abcdefghijklmnopqrstuvwxyz" \
660-
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
661-
for i in range(256):
662-
if i not in valid:
663-
fillers.append(i)
664-
def addnoise(line):
665-
res = bytearray()
666-
for i in range(len(line)):
667-
res.append(line[i])
668-
for j in range(i, len(fillers), len(line)):
669-
res.append(fillers[j])
670-
return res
671-
res = bytearray()
672-
for line in map(addnoise, lines):
673-
a = self.type2test(line)
674-
b = binascii.a2b_z85(a)
675-
res += b
676-
self.assertEqual(res, self.rawdata)
601+
self.assertEqual(binascii.a2b_z85(self.type2test(b'')), b'')
602+
self.assertEqual(binascii.a2b_z85(self.type2test(b'a')), b'')
603+
self.assertEqual(binascii.a2b_z85(self.type2test(b'ab')), b'\x1f')
604+
self.assertEqual(binascii.a2b_z85(self.type2test(b'abc')), b'\x1f\x85')
605+
self.assertEqual(binascii.a2b_z85(self.type2test(b'abcd')), b'\x1f\x85\x9a')
606+
self.assertEqual(binascii.a2b_z85(self.type2test(b'abcde')), b'\x1f\x85\x9a$')
607+
self.assertEqual(binascii.a2b_z85(self.type2test(b'abcdef')), b'\x1f\x85\x9a$')
677608

678609
def test_z85_errors(self):
679610
def _assertRegexTemplate(assert_regex, data, **kwargs):
680611
with self.assertRaisesRegex(binascii.Error, assert_regex):
681612
binascii.a2b_z85(self.type2test(data), **kwargs)
682613

614+
def assertNonZ85Data(data):
615+
_assertRegexTemplate(r"(?i)bad z85 character", data)
616+
683617
def assertOverflow(data):
684618
_assertRegexTemplate(r"(?i)z85 overflow", data)
685619

620+
assertNonZ85Data(b"\xda")
621+
assertNonZ85Data(b"00\0\0")
622+
assertNonZ85Data(b"z !/")
623+
assertNonZ85Data(b"By/JnB0hYQ\n")
624+
686625
# Test Z85 with out-of-range encoded value
687626
assertOverflow(b"%")
688627
assertOverflow(b"%n")
@@ -703,29 +642,6 @@ def test_z85_pad(self):
703642
b_pad_expected = b + b"\0" * padding
704643
self.assertEqual(b_pad, b_pad_expected)
705644

706-
def test_z85_strict_mode(self):
707-
# Test Z85 with strict mode on
708-
def assertNonZ85Data(data, expected):
709-
data = self.type2test(data)
710-
with self.assertRaisesRegex(binascii.Error, r"(?i)bad z85 character"):
711-
binascii.a2b_z85(data, strict_mode=True)
712-
default_res = binascii.a2b_z85(data)
713-
non_strict_res = binascii.a2b_z85(data, strict_mode=False)
714-
self.assertEqual(default_res, non_strict_res)
715-
self.assertEqual(non_strict_res, expected)
716-
717-
assertNonZ85Data(b"\xda", b"")
718-
assertNonZ85Data(b"00\0\0", b"\0")
719-
assertNonZ85Data(b"z !/", b"ok")
720-
assertNonZ85Data(b"By/JnB0hYQ\n", b"tests!!~")
721-
722-
def test_z85_newline(self):
723-
# Test Z85 newline parameter
724-
b = self.type2test(b"t3s\t ")
725-
self.assertEqual(binascii.b2a_z85(b), b"BtE$Aao\n")
726-
self.assertEqual(binascii.b2a_z85(b, newline=True), b"BtE$Aao\n")
727-
self.assertEqual(binascii.b2a_z85(b, newline=False), b"BtE$Aao")
728-
729645
def test_uu(self):
730646
MAX_UU = 45
731647
for backtick in (True, False):

0 commit comments

Comments
 (0)