Skip to content

Commit ff5213b

Browse files
committed
Formalise constraint on _NormalizeString's inputs
Now that all strings given as inputs to _NormalizeString have been verified (or corrected) to be correctly delimited with double quotes, there's no reason to continue doing an internal strip anymore. Moreover, we can express this internal constraint with an assertion to avoid issues in the future. Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
1 parent 00062fb commit ff5213b

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

babel/messages/pofile.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ def __init__(self, *args: str) -> None:
144144
self.append(arg)
145145

146146
def append(self, s: str) -> None:
147-
self._strs.append(s.strip())
147+
assert s[0] == '"' and s[-1] == '"'
148+
self._strs.append(s)
148149

149150
def denormalize(self) -> str:
150151
return ''.join(map(unescape, self._strs))

tests/messages/test_normalized_string.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44

55

66
def test_normalized_string():
7-
ab1 = _NormalizedString('"a"', '"b" ')
8-
ab2 = _NormalizedString('"a"', ' "b"')
9-
ac1 = _NormalizedString('"a"', '"c"')
10-
ac2 = _NormalizedString(' "a"', '"c" ')
7+
ab = _NormalizedString('"a"', '"b"')
8+
ac = _NormalizedString('"a"', '"c"')
119
z = _NormalizedString()
12-
assert ab1 == ab2 and ac1 == ac2 # __eq__
13-
assert ab1 < ac1 # __lt__
14-
assert ac1 > ab2 # __gt__
15-
assert ac1 >= ac2 # __ge__
16-
assert ab1 <= ab2 # __le__
17-
assert ab1 != ac1 # __ne__
10+
assert ab < ac # __lt__
11+
assert ac > ab # __gt__
12+
assert ab != ac # __ne__
1813
assert not z # __nonzero__ / __bool__
19-
assert sorted([ab1, ab2, ac1]) == [ab1, ab2, ac1] # sorted() is stable
14+
assert sorted([ac, ab, z]) == [z, ab, ac] # sorted() is stable
2015

2116

2217
@pytest.mark.parametrize(
@@ -38,10 +33,10 @@ def test_denormalized_simple_normalized_string(original, denormalized):
3833
"originals, denormalized",
3934
(
4035
(('"a"', '"b"'), "ab"),
41-
(('"a" ', '"b"'), "ab"),
36+
(('"a"', '"b"'), "ab"),
4237
(('"ab"', '""'), "ab"),
4338
(('"ab"', '"c"'), "abc"),
44-
(('"a"', ' "bc"'), "abc"),
39+
(('"a"', '"bc"'), "abc"),
4540
),
4641
)
4742
def test_denormalized_multi_normalized_string(originals, denormalized):

0 commit comments

Comments
 (0)