Prefer raising the python2.x type error for b64 decode errors

The current change made in de68f08d37 breaks cinder and other
projects that were expecting a TypeError, so for now and to keep
those projects operating translate the py3.x exception from binascii
into a type error to prefer consistency with existing code.

Change-Id: I4575ea3dad51be9bb2278eb0bfa31cef54c300d5
This commit is contained in:
Joshua Harlow 2017-01-09 11:23:40 -08:00
parent 004bc47522
commit ac5787d0e4
2 changed files with 8 additions and 6 deletions

View File

@ -22,6 +22,7 @@ Utilities to encode and decode Base64.
from __future__ import absolute_import
import base64
import binascii
import six
@ -64,13 +65,16 @@ def decode_as_bytes(encoded):
:returns: decoded bytes string (bytes)
Use decode_as_text() to get the decoded string as text.
A TypeError is raised if the input is invalid (or incorrectly padded).
"""
if isinstance(encoded, bytes):
encoded = encoded.decode('ascii')
if six.PY2:
return base64.decodestring(encoded)
else:
try:
return base64.b64decode(encoded)
except binascii.Error as e:
# Transform this exception for consistency.
raise TypeError(str(e))
def decode_as_text(encoded, encoding='utf-8'):

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import binascii
from oslo_serialization import base64
from oslotest import base as test_base
@ -48,7 +46,7 @@ class Base64Tests(test_base.BaseTestCase):
base64.decode_as_bytes(u'dGV4dA=='))
def test_decode_as_bytes__error(self):
self.assertRaises(binascii.Error,
self.assertRaises(TypeError,
base64.decode_as_bytes,
'hello world')