Repository navigation
Support string input #21
Description
Activity
Also, it seems
base64.b64decode()already handles whitespaces out of the box, at least with Python 3.5:>>> from base64 import b64decode >>> import string >>> b64decode('SGVsbG8gZn JvbSB1bmljb2RlIMOmw7jDpQ==') b'Hello from unicode \xc3\xa6\xc3\xb8\xc3\xa5' >>> b64decode('SGVsbG8gZn JvbSB1b \n \t mljb2RlIMOmw7jDpQ== ') b'Hello from unicode \xc3\xa6\xc3\xb8\xc3\xa5' >>> b64decode('SGVsbG8gZnJvbSB' + string.whitespace + '1bmljb2RlIMOmw7jDpQ==') b'Hello from unicode \xc3\xa6\xc3\xb8\xc3\xa5'
EDIT: This assumes, of course, that
b64decodehas access to the full stream.This is a good catch.
b64decodedoes in fact acceptstras input in2and since3.3, so we should as well. Fortunately, we duck-type everywhere except for these encoding steps, so as you've seen, there's not a lot that needs to change.I don't think that making the whitespace removal optional is the right approach. I would rather we find a more efficient way of performing it if this is an issue. Have you seen a performance issue with the whitespace removal? I'm wondering if just always passing through
_read_additional_data_removing_whitespacemight be the right approach rather than passing through all members ofstring.whitespaceto check first.Thanks for the feedback! The
ignore_whitespacepatch was just to show what I meant. I have benchmarked the whitespace removal code with a 10MB randomstrand it's almost instant, so I agree there's no obvious performance penalty there.I'll implement the suggestions in the code review and drop the other PR.
This is released in
1.0.3now.
I have an
strinput stream that I would like to decode.base64.b64decode()handlesstrjust fine, so I would like to avoid ASCII encoding the input, which would consume the entire stream unless I'm very clever. I tried this:which fails in https://github.com/aws/base64io-python/blob/master/src/base64io/__init__.py#L276 because the code assumes
datais abytesinstance.if any([char in data for char in string.whitespace]):then the example code works fine. So we could test the type ofdataand then run the version that applies._read_additional_data_removing_whitespace()Any comments?