Skip to content

Commit 6c0e4a3

Browse files
committed
Test both Python and C codepaths in base64
This is done differently to PEP-0399 to minimize the number of changed lines.
1 parent 6377440 commit 6c0e4a3

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Lib/test/test_base64.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
import unittest
2-
import base64
32
import binascii
43
import os
54
from array import array
5+
from functools import update_wrapper
66
from test.support import os_helper
77
from test.support import script_helper
8+
from test.support.import_helper import import_fresh_module
9+
10+
base64 = import_fresh_module("base64", blocked=["_base64"])
11+
c_base64 = import_fresh_module("base64", fresh=["_base64"])
12+
13+
14+
def with_c_implementation(test_func):
15+
if c_base64 is None:
16+
return test_func
17+
18+
def _test_func(self):
19+
global base64
20+
21+
# Test Python implementation
22+
test_func(self)
23+
24+
# Test C implementation
25+
base64_ = base64
26+
try:
27+
base64 = c_base64
28+
test_func(self)
29+
finally:
30+
base64 = base64_
31+
32+
update_wrapper(_test_func, test_func)
33+
34+
return _test_func
835

936

1037
class LegacyBase64TestCase(unittest.TestCase):
@@ -461,6 +488,7 @@ def test_b16decode(self):
461488
# Incorrect "padding"
462489
self.assertRaises(binascii.Error, base64.b16decode, '010')
463490

491+
@with_c_implementation
464492
def test_a85encode(self):
465493
eq = self.assertEqual
466494

@@ -511,6 +539,7 @@ def test_a85encode(self):
511539
eq(base64.a85encode(b' '*6, foldspaces=True, adobe=False), b'y+<U')
512540
eq(base64.a85encode(b' '*5, foldspaces=True, adobe=False), b'y+9')
513541

542+
@with_c_implementation
514543
def test_b85encode(self):
515544
eq = self.assertEqual
516545

@@ -545,6 +574,7 @@ def test_b85encode(self):
545574
self.check_other_types(base64.b85encode, b"www.python.org",
546575
b'cXxL#aCvlSZ*DGca%T')
547576

577+
@with_c_implementation
548578
def test_z85encode(self):
549579
eq = self.assertEqual
550580

@@ -579,6 +609,7 @@ def test_z85encode(self):
579609
self.check_other_types(base64.z85encode, b"www.python.org",
580610
b'CxXl-AcVLsz/dgCA+t')
581611

612+
@with_c_implementation
582613
def test_a85decode(self):
583614
eq = self.assertEqual
584615

@@ -625,6 +656,7 @@ def test_a85decode(self):
625656
self.check_other_types(base64.a85decode, b'GB\\6`E-ZP=Df.1GEb>',
626657
b"www.python.org")
627658

659+
@with_c_implementation
628660
def test_b85decode(self):
629661
eq = self.assertEqual
630662

@@ -660,6 +692,7 @@ def test_b85decode(self):
660692
self.check_other_types(base64.b85decode, b'cXxL#aCvlSZ*DGca%T',
661693
b"www.python.org")
662694

695+
@with_c_implementation
663696
def test_z85decode(self):
664697
eq = self.assertEqual
665698

@@ -695,6 +728,7 @@ def test_z85decode(self):
695728
self.check_other_types(base64.z85decode, b'CxXl-AcVLsz/dgCA+t',
696729
b'www.python.org')
697730

731+
@with_c_implementation
698732
def test_a85_padding(self):
699733
eq = self.assertEqual
700734

@@ -710,6 +744,7 @@ def test_a85_padding(self):
710744
eq(base64.a85decode(b'G^+IX'), b"xxxx")
711745
eq(base64.a85decode(b'G^+IXGQ7^D'), b"xxxxx\x00\x00\x00")
712746

747+
@with_c_implementation
713748
def test_b85_padding(self):
714749
eq = self.assertEqual
715750

@@ -725,6 +760,7 @@ def test_b85_padding(self):
725760
eq(base64.b85decode(b'czAet'), b"xxxx")
726761
eq(base64.b85decode(b'czAetcmMzZ'), b"xxxxx\x00\x00\x00")
727762

763+
@with_c_implementation
728764
def test_a85decode_errors(self):
729765
illegal = (set(range(32)) | set(range(118, 256))) - set(b' \t\n\r\v')
730766
for c in illegal:
@@ -762,6 +798,7 @@ def test_a85decode_errors(self):
762798
self.assertRaises(ValueError, base64.a85decode, b'aaaay',
763799
foldspaces=True)
764800

801+
@with_c_implementation
765802
def test_b85decode_errors(self):
766803
illegal = list(range(33)) + \
767804
list(b'"\',./:[\\]') + \
@@ -776,6 +813,7 @@ def test_b85decode_errors(self):
776813
self.assertRaises(ValueError, base64.b85decode, b'|NsC')
777814
self.assertRaises(ValueError, base64.b85decode, b'|NsC1')
778815

816+
@with_c_implementation
779817
def test_z85decode_errors(self):
780818
illegal = list(range(33)) + \
781819
list(b'"\',;_`|\\~') + \

0 commit comments

Comments
 (0)