Skip to content

Commit 72ffe28

Browse files
authored
Merge pull request #42 from Eddy114514/base64_warn
Add warning for base64 encode functions
2 parents 7fa89f1 + 67dcccf commit 72ffe28

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*.profclang?
1616
*.profraw
1717
*.dyn
18+
.history/
1819
Doc/build/
1920
Doc/venv/
2021
Doc/.venv/

Lib/base64.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import struct
1010
import string
1111
import binascii
12-
from warnings import warnpy3k_with_fix
12+
from warnings import warnpy3k_with_fix, warnpy3k
13+
from functools import wraps
1314

1415

1516
__all__ = [
@@ -367,5 +368,20 @@ def test1():
367368
print s0, repr(s1), s2
368369

369370

371+
def _warn_encode(func, name):
372+
@wraps(func)
373+
def encode_wrapper(*args, **kwargs):
374+
warnpy3k(
375+
"base64.{0} returns str in Python 2 (bytes in 3.x)".format(name),
376+
UserWarning,
377+
stacklevel= 2,
378+
)
379+
return func(*args, **kwargs)
380+
return encode_wrapper
381+
382+
383+
for _name in ["b64encode", "b32encode", "b16encode"]:
384+
globals()[_name] = _warn_encode(globals()[_name], _name)
385+
370386
if __name__ == '__main__':
371387
test()

Lib/test/test_base64.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ def test_b64encode(self):
111111
eq(base64.urlsafe_b64encode('\xd3V\xbeo\xf7\x1d'), '01a-b_cd')
112112
# Non-bytes
113113
eq(base64.urlsafe_b64encode(bytearray('\xd3V\xbeo\xf7\x1d')), '01a-b_cd')
114+
115+
def test_b64encode_warns(self):
116+
import warnings, base64
117+
with warnings.catch_warnings(record=True) as w:
118+
warnings.simplefilter('always', UserWarning)
119+
base64.b64encode(b'test')
120+
self.assertTrue(any('base64.b64encode returns str in Python 2 (bytes in 3.x)' == str(x.message) for x in w))
121+
122+
def test_b32encode_warns(self):
123+
import warnings, base64
124+
with warnings.catch_warnings(record=True) as w:
125+
warnings.simplefilter('always', UserWarning)
126+
base64.b32encode(b'test')
127+
self.assertTrue(any('base64.b32encode returns str in Python 2 (bytes in 3.x)' == str(x.message) for x in w))
128+
129+
def test_b16encode_warns(self):
130+
import warnings, base64
131+
with warnings.catch_warnings(record=True) as w:
132+
warnings.simplefilter('always', UserWarning)
133+
base64.b16encode(b'test')
134+
self.assertTrue(any('base64.b16encode returns str in Python 2 (bytes in 3.x)' == str(x.message) for x in w))
114135

115136
def test_b64decode(self):
116137
eq = self.assertEqual

Lib/test/test_py3kwarn.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
from test.test_support import check_py3k_warnings, CleanImport, run_unittest
44
import warnings
5+
import base64
56
from test import test_support
67

78
if not sys.py3kwarning:
@@ -384,7 +385,22 @@ def test_raise_three_components(self):
384385
use 'raise' with a single object"""
385386
with check_py3k_warnings() as w:
386387
excType, excValue, excTraceback = sys.exc_info()
387-
388+
389+
def test_b64encode_warns(self):
390+
expected = "base64.b64encode returns str in Python 2 (bytes in 3.x)"
391+
base64.b64encode(b'test')
392+
check_py3k_warnings(expected, UserWarning)
393+
394+
def test_b32encode_warns(self):
395+
expected = "base64.b32encode returns str in Python 2 (bytes in 3.x)"
396+
base64.b32encode(b'test')
397+
check_py3k_warnings(expected, UserWarning)
398+
399+
def test_b16encode_warns(self):
400+
expected = "base64.b16encode returns str in Python 2 (bytes in 3.x)"
401+
base64.b16encode(b'test')
402+
check_py3k_warnings(expected, UserWarning)
403+
388404

389405
class TestStdlibRemovals(unittest.TestCase):
390406

0 commit comments

Comments
 (0)