From b5694fbb14f90604a2540428cde58f592c62eba3 Mon Sep 17 00:00:00 2001 From: Shahadat Islam Koyel <179203101+ShahadatTest@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:59:52 +0100 Subject: [PATCH] Reject unknown size and length unit words --- humanfriendly/__init__.py | 8 ++++---- humanfriendly/tests.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/humanfriendly/__init__.py b/humanfriendly/__init__.py index 4c0a333..0a6326a 100644 --- a/humanfriendly/__init__.py +++ b/humanfriendly/__init__.py @@ -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: @@ -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)" @@ -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)" diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 72dad99..50ab569 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -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)) @@ -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))