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
5 changes: 3 additions & 2 deletions bitmath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
# Python 3.x compat
if sys.version > '3':
long = int # pragma: PY2X no cover
unicode = str # pragma: PY2X no cover

#: A list of all the valid prefix unit types. Mostly for reference,
#: also used by the CLI tool as valid types
Expand Down Expand Up @@ -1331,13 +1332,13 @@ def parse_string(s):
the unit.
"""
# Strings only please
if type(s) is not str:
if type(s) is not str and type(s) is not unicode:
raise ValueError("parse_string only accepts string inputs but a %s was given" %
type(s))

# get the index of the first alphabetic character
try:
index = list(map(str.isalpha, s)).index(True)
index = list([i.isalpha() for i in s]).index(True)
except ValueError:
# If there's no alphabetic characters we won't be able to .index(True)
raise ValueError("No unit detected, can not parse string '%s' into a bitmath object" % s)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ def test_parse_string_non_string_input(self):
"""parse_string can identify a non-string input"""
with self.assertRaises(ValueError):
bitmath.parse_string(12345)

def test_parse_string_unicode(self):
"""parse_string can handle a unicode string"""
self.assertEqual(
bitmath.parse_string(u"750 GiB"),
bitmath.GiB(750))