File tree Expand file tree Collapse file tree 4 files changed +42
-2
lines changed
Expand file tree Collapse file tree 4 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 1515* .profclang ?
1616* .profraw
1717* .dyn
18+ .history /
1819Doc /build /
1920Doc /venv /
2021Doc /.venv /
Original file line number Diff line number Diff line change 99import struct
1010import string
1111import 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+
370386if __name__ == '__main__' :
371387 test ()
Original file line number Diff line number Diff line change @@ -111,6 +111,13 @@ def test_b64encode(self):
111111 eq (base64 .urlsafe_b64encode ('\xd3 V\xbe o\xf7 \x1d ' ), '01a-b_cd' )
112112 # Non-bytes
113113 eq (base64 .urlsafe_b64encode (bytearray ('\xd3 V\xbe o\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 ))
114121
115122 def test_b64decode (self ):
116123 eq = self .assertEqual
Original file line number Diff line number Diff line change 22import sys
33from test .test_support import check_py3k_warnings , CleanImport , run_unittest
44import warnings
5+ import base64
56from test import test_support
67
78if 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
389405class TestStdlibRemovals (unittest .TestCase ):
390406
You can’t perform that action at this time.
0 commit comments