Skip to content

Commit 680dae1

Browse files
Support any bytes-like object as ignorechars.
1 parent c128590 commit 680dae1

File tree

6 files changed

+30
-26
lines changed

6 files changed

+30
-26
lines changed

Doc/library/base64.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ POST request.
8585
A :exc:`binascii.Error` exception is raised
8686
if *s* is incorrectly padded.
8787

88-
If *ignorechars* is specified, it should be a byte string containing
89-
characters to ignore from the input, and *validate* is ``True`` by default.
90-
Otherwise *validate* is ``False`` by default.
88+
If *ignorechars* is specified, it should be a :term:`bytes-like object`
89+
containing characters to ignore from the input when *validate* is true.
90+
The default value of *validate* is ``True`` if *ignorechars* is specified,
91+
``False`` otherwise.
9192

9293
If *validate* is false, characters that are neither
9394
in the normal base-64 alphabet nor the alternative alphabet are

Doc/library/binascii.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ The :mod:`binascii` module defines the following functions:
5454
Convert a block of base64 data back to binary and return the binary data. More
5555
than one line may be passed at a time.
5656

57-
If *ignorechars* is specified, it should be a byte string containing
58-
characters to ignore from the input when *strict_mode* is true.
59-
*strict_mode* is ``True`` by default, if *ignorechars* is specified,
57+
If *ignorechars* is specified, it should be a :term:`bytes-like object`
58+
containing characters to ignore from the input when *strict_mode* is true.
59+
The default value of *strict_mode* is ``True`` if *ignorechars* is specified,
6060
``False`` otherwise.
6161

6262
If *strict_mode* is true, only valid base64 data will be converted. Invalid base64

Lib/test/test_base64.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ def test_b64decode_invalid_chars(self):
307307
(b'@@', b'', b'@!'),
308308
(b'!', b'', b'@!'),
309309
(b"YWJj\n", b"abc", b'\n'),
310-
(b'YWJj\nYWI=', b'abcab', b'\n'))
310+
(b'YWJj\nYWI=', b'abcab', b'\n'),
311+
(b'YW\nJj', b'abc', b'\n'),
312+
(b'YW\nJj', b'abc', bytearray(b'\n')),
313+
(b'YW\nJj', b'abc', memoryview(b'\n')),
314+
)
311315
funcs = (
312316
base64.b64decode,
313317
base64.standard_b64decode,
@@ -329,8 +333,6 @@ def test_b64decode_invalid_chars(self):
329333
base64.b64decode(bstr, ignorechars=ignorechars),
330334
res)
331335

332-
with self.assertRaises(TypeError):
333-
base64.b64decode(b'', ignorechars=bytearray())
334336
with self.assertRaises(TypeError):
335337
base64.b64decode(b'', ignorechars='')
336338
with self.assertRaises(TypeError):

Lib/test/test_binascii.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ def assertNonBase64Data(data, expected, ignorechars):
199199
assertNonBase64Data(b'abc=:', b'i\xb7', ignorechars=b':')
200200
assertNonBase64Data(b'ab==\n', b'i', ignorechars=b'\n')
201201
assertNonBase64Data(b'ab=:=', b'i', ignorechars=b':')
202+
assertNonBase64Data(b'a\nb==', b'i', ignorechars=bytearray(b'\n'))
203+
assertNonBase64Data(b'a\nb==', b'i', ignorechars=memoryview(b'\n'))
202204

203205
data = self.type2test(b'a\nb==')
204-
with self.assertRaises(TypeError):
205-
binascii.a2b_base64(data, ignorechars=bytearray())
206206
with self.assertRaises(TypeError):
207207
binascii.a2b_base64(data, ignorechars='')
208208
with self.assertRaises(TypeError):

Modules/binascii.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,10 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick)
471471

472472

473473
static int
474-
ignorechar(unsigned char c, PyBytesObject *ignorechars)
474+
ignorechar(unsigned char c, Py_buffer *ignorechars)
475475
{
476-
return (ignorechars != NULL &&
477-
memchr(PyBytes_AS_STRING(ignorechars), c,
478-
PyBytes_GET_SIZE(ignorechars)));
476+
return (ignorechars->buf != NULL &&
477+
memchr(ignorechars->buf, c, ignorechars->len));
479478
}
480479

481480
/*[clinic input]
@@ -488,7 +487,7 @@ binascii.a2b_base64
488487
When set to true, bytes that are not part of the base64 standard are
489488
not allowed. The same applies to excess data after padding (= / ==).
490489
Set to True by default if ignorechars is specified, False otherwise.
491-
ignorechars: PyBytesObject = NULL
490+
ignorechars: Py_buffer(py_default="<unrepresentable>") = None
492491
A byte string containing characters to ignore from the input when
493492
strict_mode is true.
494493
@@ -497,8 +496,8 @@ Decode a line of base64 data.
497496

498497
static PyObject *
499498
binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,
500-
PyBytesObject *ignorechars)
501-
/*[clinic end generated code: output=b1868e0d886cd8cf input=e2d2e48c986e2afb]*/
499+
Py_buffer *ignorechars)
500+
/*[clinic end generated code: output=eab37aea4cfa6daa input=3be4937d72943835]*/
502501
{
503502
assert(data->len >= 0);
504503

@@ -507,7 +506,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,
507506
binascii_state *state = NULL;
508507

509508
if (strict_mode == -1) {
510-
strict_mode = (ignorechars != NULL);
509+
strict_mode = (ignorechars->buf != NULL);
511510
}
512511

513512
/* Allocate the buffer */

Modules/clinic/binascii.c.h

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)