diff --git a/.gitignore b/.gitignore index 8f3a2d133b5e35..ce44ed217973aa 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ *.profclang? *.profraw *.dyn +.history/ Doc/build/ Doc/venv/ Doc/.venv/ diff --git a/Lib/base64.py b/Lib/base64.py index 7fd4c3cca32c55..88c4e2c20875f0 100755 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -9,7 +9,8 @@ import struct import string import binascii -from warnings import warnpy3k_with_fix +from warnings import warnpy3k_with_fix, warnpy3k +from functools import wraps __all__ = [ @@ -367,5 +368,20 @@ def test1(): print s0, repr(s1), s2 +def _warn_encode(func, name): + @wraps(func) + def encode_wrapper(*args, **kwargs): + warnpy3k( + "base64.{0} returns str in Python 2 (bytes in 3.x)".format(name), + UserWarning, + stacklevel= 2, + ) + return func(*args, **kwargs) + return encode_wrapper + + +for _name in ["b64encode", "b32encode", "b16encode"]: + globals()[_name] = _warn_encode(globals()[_name], _name) + if __name__ == '__main__': test() diff --git a/Lib/test/test_py3kwarn.py b/Lib/test/test_py3kwarn.py index 94b8e3e0ca0b9f..2ea08c5491c662 100644 --- a/Lib/test/test_py3kwarn.py +++ b/Lib/test/test_py3kwarn.py @@ -2,6 +2,7 @@ import sys from test.test_support import check_py3k_warnings, CleanImport, run_unittest import warnings +import base64 from test import test_support if not sys.py3kwarning: @@ -384,7 +385,22 @@ def test_raise_three_components(self): use 'raise' with a single object""" with check_py3k_warnings() as w: excType, excValue, excTraceback = sys.exc_info() - + + def test_b64encode_warns(self): + expected = "base64.b64encode returns str in Python 2 (bytes in 3.x)" + base64.b64encode(b'test') + check_py3k_warnings(expected, UserWarning) + + def test_b32encode_warns(self): + expected = "base64.b32encode returns str in Python 2 (bytes in 3.x)" + base64.b32encode(b'test') + check_py3k_warnings(expected, UserWarning) + + def test_b16encode_warns(self): + expected = "base64.b16encode returns str in Python 2 (bytes in 3.x)" + base64.b16encode(b'test') + check_py3k_warnings(expected, UserWarning) + class TestStdlibRemovals(unittest.TestCase):