Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*.profclang?
*.profraw
*.dyn
.history/
Doc/build/
Doc/venv/
Doc/.venv/
Expand Down
18 changes: 17 additions & 1 deletion Lib/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down Expand Up @@ -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()
18 changes: 17 additions & 1 deletion Lib/test/test_py3kwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):

Expand Down