Skip to content

Commit 6377440

Browse files
committed
Create _base64 module with wrappers for accelerated functions
If we were strictly following PEP-0399, _base64 would be a C module for accelerated functions in base64. Due to historical reasons, those should actually go in binascii instead. We still want to preserve the existing Python code in base64. Parting out facilities for accessing the C functions into a module named _base64 shouldn't risk a naming conflict and will simplify testing.
1 parent aa06c5d commit 6377440

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Lib/_base64.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""C accelerator wrappers for originally pure-Python parts of base64."""
2+
3+
from binascii import a2b_ascii85, a2b_base85, b2a_ascii85, b2a_base85
4+
5+
__all__ = ['a85encode', 'a85decode',
6+
'b85encode', 'b85decode',
7+
'z85encode', 'z85decode']
8+
9+
10+
def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):
11+
return b2a_ascii85(b, fold_spaces=foldspaces,
12+
wrap=adobe, width=wrapcol, pad=pad)
13+
14+
15+
def a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v'):
16+
return a2b_ascii85(b, fold_spaces=foldspaces,
17+
wrap=adobe, ignore=ignorechars)
18+
19+
20+
def b85encode(b, pad=False):
21+
return b2a_base85(b, pad=pad, newline=False)
22+
23+
24+
def b85decode(b):
25+
return a2b_base85(b, strict_mode=True)
26+
27+
28+
def z85encode(s):
29+
return b2a_base85(s, newline=False, z85=True)
30+
31+
32+
def z85decode(s):
33+
return a2b_base85(s, strict_mode=True, z85=True)

Lib/base64.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,13 @@ def decodebytes(s):
576576
return binascii.a2b_base64(s)
577577

578578

579+
# Use accelerated implementations of originally pure-Python parts if possible.
580+
try:
581+
from _base64 import *
582+
except ImportError:
583+
pass
584+
585+
579586
# Usable as a script...
580587
def main():
581588
"""Small main program"""

0 commit comments

Comments
 (0)