-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
Description
Feature or enhancement
Proposal:
Currently base64, email.contentmanager, imaplib, and plistlib all have code which takes a contiguous bytes, splits it into at most bytes_per_line (or maxbinsize) length chunks. These chunks are passed to binascii.b2a_base64, the results collected, and then joined back together to create a final result. For example:
Lines 565 to 573 in 33efd71
| def encodebytes(s): | |
| """Encode a bytestring into a bytes object containing multiple lines | |
| of base-64 data.""" | |
| _input_type_check(s) | |
| pieces = [] | |
| for i in range(0, len(s), MAXBINSIZE): | |
| chunk = s[i : i + MAXBINSIZE] | |
| pieces.append(binascii.b2a_base64(chunk)) | |
| return b"".join(pieces) |
Internally b2a_base64 is using PyBytesWriter to manage the buffer and that could hold the final joined together bytes. To do that, need to teach it to handle bytes_per_line terminating lines after that many bytes and inserting a newline if required.
That reduces the amount of code to call as well as increasing efficiency of these cases by reducing the number of Python objects involved as well as the number of times data is copied.
Proposed new signature:
b2a_base64(data, *, newline=True, bytes_per_line=None)Sample implementation: cmaloney@705bd9b
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response