Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
05ae5ad
Add Ascii85, base85, and Z85 support to binascii
kangtastic Mar 8, 2023
aa06c5d
Restore base64.py
kangtastic Apr 26, 2025
6377440
Create _base64 module with wrappers for accelerated functions
kangtastic Apr 26, 2025
6c0e4a3
Test both Python and C codepaths in base64
kangtastic Apr 26, 2025
ce4773c
Match behavior between Python and C base 85 functions
kangtastic Apr 26, 2025
4072e3b
Add Z85 tests to binascii
kangtastic Apr 27, 2025
bc9217f
Update generated files
kangtastic Apr 27, 2025
2c40ba0
Avoid importing functools
kangtastic Apr 28, 2025
fd9eaf7
Avoid circular import in _base64
kangtastic Apr 28, 2025
4746d18
Do not use a decorator for changing exception type
kangtastic Apr 28, 2025
d075593
Test Python and C codepaths in base64 using mixins
kangtastic Apr 28, 2025
6d65fec
Remove leading underscore from functions in private module
kangtastic Apr 29, 2025
a241356
Merge branch 'main' into gh-101178-rework-base85
serhiy-storchaka Dec 24, 2025
0df9a40
Use more modern C API.
serhiy-storchaka Dec 24, 2025
60fbd7c
Fix tests.
serhiy-storchaka Dec 24, 2025
a070887
Merge branch 'main' into gh-101178-rework-base85
serhiy-storchaka Dec 25, 2025
167e83e
Fix new tests.
serhiy-storchaka Dec 25, 2025
01df442
Optimize binascii.b2a_ascii85().
serhiy-storchaka Dec 26, 2025
7885918
Apply suggestions from code review
serhiy-storchaka Dec 27, 2025
1e928e3
Update C style to more closely adhere to PEP-7
kangtastic Dec 28, 2025
2691a0a
Remove pure-Python base-85-related codepaths in base64
kangtastic Dec 28, 2025
b9d27bd
Remove now-unnecessary _base64 module and fix tests
kangtastic Dec 28, 2025
780517a
Separate Z85 from Base85 on the Python API side
kangtastic Dec 28, 2025
bc9a66d
Fix tests after separating Base85 from Z85
kangtastic Dec 28, 2025
dc1d3fc
Merge branch 'main' into gh-101178-rework-base85
kangtastic Dec 28, 2025
c5de5a1
Update generated files after merging main
kangtastic Dec 28, 2025
3bb3b18
Update Misc/NEWS.d and Misc/ACKS
kangtastic Dec 28, 2025
6f09fa8
Update generated files again
kangtastic Dec 29, 2025
6d8f897
Fix typo in NEWS entry
kangtastic Dec 29, 2025
3582492
Merge branch 'main' into gh-101178-rework-base85
serhiy-storchaka Jan 14, 2026
879dd86
Move the NEWS entry to the correct section.
serhiy-storchaka Jan 14, 2026
3cdc3c5
Minor cleanups, align lookup tables to 64 bytes (NFC)
kangtastic Jan 17, 2026
8adaf2c
Allow up to sys.maxsize output length when encoding base 85
kangtastic Jan 18, 2026
2b2ecc4
Fix Ascii85 test from mainline
kangtastic Jan 18, 2026
da165d1
Allow up to sys.maxsize output length when decoding base 85
kangtastic Jan 18, 2026
bf32f99
Defer base 85 overflow check during decoding
kangtastic Jan 18, 2026
74f6ceb
Merge branch 'main' into gh-101178-rework-base85
kangtastic Jan 18, 2026
4ba3e50
Merge branch 'main' into gh-101178-rework-base85
serhiy-storchaka Feb 6, 2026
99e0bde
Rename parameters to match the base64 module.
serhiy-storchaka Feb 6, 2026
cc6d485
Remove parameters strict_mode and newline.
serhiy-storchaka Feb 6, 2026
30f54a1
Optimize ignorechars cache.
serhiy-storchaka Feb 6, 2026
37df735
Harmonize documentation.
serhiy-storchaka Feb 6, 2026
56a02b2
Add What's New entries.
serhiy-storchaka Feb 6, 2026
adb1922
Polish tests.
serhiy-storchaka Feb 6, 2026
0730fdf
Rename internal Base 85 codec functions to match Base 64 helpers
kangtastic Feb 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid circular import in _base64
This requires some code duplication, but oh well.
  • Loading branch information
kangtastic committed Apr 28, 2025
commit fd9eaf7ba9aec9d56ca5d0d92e8c3fedf217eb4c
23 changes: 21 additions & 2 deletions Lib/_base64.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
"""C accelerator wrappers for originally pure-Python parts of base64."""

from binascii import Error, a2b_ascii85, a2b_base85, b2a_ascii85, b2a_base85
from base64 import _bytes_from_decode_data, bytes_types


# Base 85 encoder functions in base64 silently convert input to bytes.
# Base 85 functions in base64 silently convert input to bytes.
# Copy the conversion logic from base64 to avoid circular imports.

bytes_types = (bytes, bytearray) # Types acceptable as binary data


def _bytes_from_decode_data(s):
if isinstance(s, str):
try:
return s.encode('ascii')
except UnicodeEncodeError:
raise ValueError('string argument should contain only ASCII characters')
if isinstance(s, bytes_types):
return s
try:
return memoryview(s).tobytes()
except TypeError:
raise TypeError("argument should be a bytes-like object or ASCII "
"string, not %r" % s.__class__.__name__) from None


def _bytes_from_encode_data(b):
return b if isinstance(b, bytes_types) else memoryview(b).tobytes()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memoryview(b).tobytes() may raise a generic TypeError here, whereas it is in _bytes_from_decode_data. Should we do it as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The discussion by @serhiy-storchaka seems to have been resolved with the conclusion that the pure-Python base-85-related functions from base64 may be removed.

I'll therefore turn base64.a85encode(), etc. into wrappers for the new binascii.b2a_ascii85(), etc. as I had it originally, similarly to how e.g. base64.b64encode() is a wrapper for binascii.b2a_base64().

_base64.py will be removed, since it will no longer be necessary to test against both Python and C implementations of the base-85-related functions. So that makes your concerns for this specific file moot.

However, to follow your comment here a bit:

The type conversion logic is replicated from the existing _85encode() helper function, which itself seems to have replicated the logic from the existing _b32encode() helper function.

The logic in _b32encode(), in turn, seems to be an attempt to replicate the existing binascii facility for input type conversions.

The new additions (base64.a85encode(), etc.) use the existing binascii facility, which raises a more descriptive TypeError instead of a generic one.

Can you or someone else verify that the C type conversion logic in binascii and the Python logic in base64 are equivalent, or at least equivalent enough?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh so it was something that was already there. Ok, sorry for the noise. I was more worried about the fact that a TypeError was caught during decoding but not during encoding.


Expand Down