-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base64 module should use memoryview #62039
Comments
The base64 module is currently restricted specifically to bytes and bytearray objects. By using memoryview, it could effectively decode any input type that provides an 8-bit C contiguous view of the underlying data. |
Working on this. |
A patch with tests update to use memoryview in base64 module. |
Not only memoryview should be supported, but any class which supports the buffer protocol and is C-contiguous (i.e. array.array). |
Yes, this should probably be done the other way around: wrap the base64 argument in a memoryview, and encode/decode from it. |
As Serhiy and Antoine noted, what I had in mind here was to try to wrap the input in a memoryview if it wasn't an instance of str, bytes or bytearray. An existing memoryview will be passed back unmodified, while something like array.array will provide a view into its raw data for encoding or decoding. The tests would then cover both memoryview and array.array to ensure it was all working as expected. |
Note that some functions use bytes/bytearray methods and an argument should converted to bytes for them. Other functions use C functions which supports the buffer protocol and do not required any conversion. |
Here is a patch which allows bytes-like arguments in the base64 module. I just removed type checking if underlying function raises an exception with an appropriate message. I'm not sure about b32encode(), perhaps we can left an exception from memoryview(). |
Nick, what you say about the patch? |
Oops, my review comments don't actually make sense because I looked at the patch in isolation, rather than checking the full context in the module. Sorry about that. We have 2 different cases to deal with, only one of which currently has a helper function. I suggest renaming _bytes_from_decode_data to "_bytes_for_decoding" and adding "_bytes_for_encoding". The difference between them is the implicit encoding of pure ASCII strings to bytes in the decoding case and the details of the error message thrown. The encoding and decoding functions should then use the appropriate coercion helper for both the input data and for altchars. |
Thank you Ezio and Nick for your comments.
I rather think a TypeError exception raised by |
We now also need to update the new footnote that I added for bpo-17844 in http://hg.python.org/cpython/rev/801567d6302c |
The codec uses the old API that breaks the encoded output up into multiple lines. The new patch:
|
Just adding a note for easier cross-referencing: the old behaviour of these functions was one of the culprits that led to the removal of the codec aliases as discussed in bpo-7475 |
It works only if input also supports slicing and this slicing is corresponded to bytes-slicing. It perhaps doesn't catch a non C-continuous buffers. Actually we need something like (unlikely cast() doesn't work with empty buffers): s = memoryview(s)
if not s:
return b''
s = s.cast('B')
... |
binascii already only supports simple C contiguous buffers, expanding it and the base64 module to handle anything else should be a separate RFE. |
However, _input_type_check should enforce that (as binascii does), so I'll add that before committing. |
After working through this, I found that the modern base64 API just relies on the checks in binascii. All that checks for is:
It completely ignores the number of dimensions and the format information. I added tests to at least capture this behaviour, even though it seems a little dubious to me. For the legacy API, I didn't relax the input checks that far - the legacy API will still complain if there is more than 1 dimension and if the format code isn't one of 'c', 'b' or 'B'. That's already substantially more permissive than what it supported in previous versions. Just running the full test suite now, will push after that finishes. |
New changeset d90f25e1a705 by Nick Coghlan in branch 'default': |
New changeset e53984133740 by Nick Coghlan in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: