Base64 encoding into buffer, to reduce allocations
Description
base64 encoding is supported by binasci.b2a_base64. This implements the standard API in CPython. It always allocates a new buffer, and returns it. The allocations could be reduced/eliminated if a b2a_base64_into type API would (also) be available. This function would take an additional parameter: the buffer (bytearray) that is to be filled with encoded data. The caller must make sure it is sufficiently large (4/3 the size of the input + up to 4 bytes of padding).
This is similar to how there is struct.unpack_into, et.c.
Code Size
Existing code could be moved into the b2a_base64_into implementation. And then have b2a_base64 be a compatibility wrapper, which creates the needed buffer internally. This should make the code increase minimal.
Implementation
I hope the MicroPython maintainers or community will implement this feature
Code of Conduct
Yes, I agree
ubinascii: a2b_base64(...) behavior is inconsistent with CPython
The a2b_base64(...) function in the ubinascii module raises an exception if its byte string argument contains any unexpected characters. In comparison to CPython, the CPython implementation ignores all bytes representing new line, carriage return, and space characters, as well as all bytes whose most significant bit is set (i.e. bytes representing extended 8-bit ASCII characters). This behavior can be seen in the CPython implementation here.
Aside from the current behavior being inconsistent with CPython, this creates an oddity in the MicroPython ubinascii module in that the b2a_base64(...) and a2b_base64(...) inverse functions are incompatible. Specifically, the b2a_base64(...) function returns a Base64 representation with a trailing new line character. While this is consistent with this function's CPython behavior, passing this result to the a2b_base64(...) function fails because the trailing new line character is unexpected. Logically, it should be possible to cyclically pass the result of either function into its inverse function without failure.