Skip to content

Commit ce2dc07

Browse files
Merge python#18
18: Warn for encode and decode in base64 r=ltratt a=nanjekyejoannah Add warnings for the base module. **Notes:** ``` py2.x >>> import base64 >>> from base64 import decodestring >>> py3.x: >>> import base64 >>> from base64 import decodestring Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name 'decodestring' from 'base64' (/usr/local/Cellar/[email protected]/3.9.12_1/ Frameworks/Python.framework/Versions/3.9/lib/python3.9/ base64.py) ``` Co-authored-by: Joannah Nanjekye <[email protected]>
2 parents 5f5332a + e103465 commit ce2dc07

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/base64.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import struct
1010
import string
1111
import binascii
12+
from warnings import warnpy3k_with_fix
1213

1314

1415
__all__ = [
@@ -29,7 +30,6 @@
2930
_translation = [chr(_x) for _x in range(256)]
3031
EMPTYSTRING = ''
3132

32-
3333
def _translate(s, altchars):
3434
translation = _translation[:]
3535
for k, v in altchars.items():
@@ -316,6 +316,8 @@ def decode(input, output):
316316

317317
def encodestring(s):
318318
"""Encode a string into multiple lines of base-64 data."""
319+
warnpy3k_with_fix("base64.encodestring is not supported in 3.x",
320+
"use base64.encodebytes instead", stacklevel=2)
319321
pieces = []
320322
for i in range(0, len(s), MAXBINSIZE):
321323
chunk = s[i : i + MAXBINSIZE]
@@ -325,6 +327,8 @@ def encodestring(s):
325327

326328
def decodestring(s):
327329
"""Decode a string."""
330+
warnpy3k_with_fix("base64.decodestring is not supported in 3.x",
331+
"use base64.decodebytes instead", stacklevel=2)
328332
return binascii.a2b_base64(s)
329333

330334

Lib/test/test_base64.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
from test import test_support
33
import base64
4+
import sys
45

56

67

@@ -21,6 +22,11 @@ def test_encodestring(self):
2122
# Non-bytes
2223
eq(base64.encodestring(bytearray('abc')), 'YWJj\n')
2324

25+
if sys.py3kwarning:
26+
with warnings.catch_warnings(record=True) as w:
27+
warnings.filterwarnings('always', category=Py3xWarning)
28+
base64.encodestring("")
29+
2430
def test_decodestring(self):
2531
eq = self.assertEqual
2632
eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), "www.python.org")
@@ -37,6 +43,11 @@ def test_decodestring(self):
3743
# Non-bytes
3844
eq(base64.decodestring(bytearray("YWJj\n")), "abc")
3945

46+
if sys.py3kwarning:
47+
with warnings.catch_warnings(record=True) as w:
48+
warnings.filterwarnings('always', category=Py3xWarning)
49+
base64.decodestring('')
50+
4051
def test_encode(self):
4152
eq = self.assertEqual
4253
from cStringIO import StringIO

0 commit comments

Comments
 (0)