Skip to content
Open
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
8 changes: 4 additions & 4 deletions humanfriendly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def parse_size(size, binary=False):
normalized_unit = tokens[1].lower() if len(tokens) == 2 and is_string(tokens[1]) else ''
# If the input contains only a number, it's assumed to be the number of
# bytes. The second token can also explicitly reference the unit bytes.
if len(tokens) == 1 or normalized_unit.startswith('b'):
if len(tokens) == 1 or normalized_unit in ('b', 'byte', 'bytes'):
return int(tokens[0])
# Otherwise we expect two tokens: A number and a unit.
if normalized_unit:
Expand All @@ -252,7 +252,7 @@ def parse_size(size, binary=False):
# symbols (KB, MB, GB, etc) and names (kilobyte, megabyte,
# gigabyte, etc) according to the caller's preference.
if (normalized_unit in (unit.decimal.symbol.lower(), unit.decimal.name.lower()) or
normalized_unit.startswith(unit.decimal.symbol[0].lower())):
normalized_unit == unit.decimal.symbol[0].lower()):
return int(tokens[0] * (unit.binary.divider if binary else unit.decimal.divider))
# We failed to parse the size specification.
msg = "Failed to parse size! (input %r was tokenized as %r)"
Expand Down Expand Up @@ -319,9 +319,9 @@ def parse_length(length):
# Otherwise we expect to find two tokens: A number and a unit.
if len(tokens) == 2 and is_string(tokens[1]):
normalized_unit = tokens[1].lower()
# Try to match the first letter of the unit.
# Match the complete symbol or known unit name.
for unit in length_size_units:
if normalized_unit.startswith(unit['prefix']):
if normalized_unit in (unit['prefix'], unit['singular'], unit['plural']):
return tokens[0] * unit['divider']
# We failed to parse the length specification.
msg = "Failed to parse length! (input %r was tokenized as %r)"
Expand Down
22 changes: 22 additions & 0 deletions humanfriendly/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,19 @@ def test_parse_size(self):
with self.assertRaises(InvalidSize):
parse_size('a')

def test_parse_size_rejects_unknown_unit_words(self):
"""Reject words that merely start with a known size prefix."""
for value in ('1 taco', '1 person', '1 potato', '5 ghosts', '10 microns',
'2 millibytes', '1 banana', '1 bread'):
with self.assertRaises(InvalidSize):
parse_size(value)
with self.assertRaises(InvalidSize):
parse_size(value, binary=True)
for unit, expected in (('b', 1), ('byte', 1), ('bytes', 1), ('K', 1000),
('KB', 1000), ('kilobytes', 1000), ('KiB', 1024),
('kibibytes', 1024)):
self.assertEqual(expected, parse_size('1 ' + unit))

def test_format_length(self):
"""Test :func:`humanfriendly.format_length()`."""
self.assertEqual('0 metres', format_length(0))
Expand Down Expand Up @@ -565,6 +578,15 @@ def test_parse_length(self):
with self.assertRaises(InvalidLength):
parse_length('a')

def test_parse_length_rejects_unknown_unit_words(self):
"""Keep metre names and exact symbols while rejecting unrelated words."""
for value in ('1 moon', '1 monkey', '1 cmake', '1 kmph', '1 mmfoo', '1 nmfoo'):
with self.assertRaises(InvalidLength):
parse_length(value)
for unit, expected in (('m', 1), ('metre', 1), ('metres', 1),
('cm', 0.01), ('mm', 0.001), ('nm', 1e-9), ('km', 1000)):
self.assertEqual(expected, parse_length('1 ' + unit))

def test_format_number(self):
"""Test :func:`humanfriendly.format_number()`."""
self.assertEqual('1', format_number(1))
Expand Down