Skip to content

Commit

Permalink
Check input data types
Browse files Browse the repository at this point in the history
  • Loading branch information
suminb committed Apr 9, 2017
1 parent e4cca68 commit be37eaa
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions base62.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def encodebytes(s):
:param s: A byte array
"""
_check_bytes_type(s)
return encode(bytes_to_int(s))


Expand All @@ -73,6 +74,8 @@ def decodebytes(s):
:param s: A bytestring to be decoded in base62
:rtype: bytearray
"""
_check_bytes_type(s)

decoded = decode(s.decode(DEFAULT_ENCODING))
buf = []
while decoded > 0:
Expand All @@ -94,3 +97,10 @@ def __value__(ch):
return ord(ch) - ord('a') + 36
else:
raise ValueError('base62: Invalid character (%s)' % ch)


def _check_bytes_type(s):
"""Checks if the input is in an appropriate type."""
if not isinstance(s, bytes):
msg = 'expected bytes-like object, not %s' % s.__class__.__name__
raise TypeError(msg)

0 comments on commit be37eaa

Please sign in to comment.