Skip to content

Commit 4b02678

Browse files
More of Benedikt's suggestions
1 parent ea5fa91 commit 4b02678

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Lib/test/test_tools/test_i18n.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import re
55
import sys
66
import unittest
7-
from types import SimpleNamespace
87
from textwrap import dedent
8+
from types import SimpleNamespace
99
from pathlib import Path
1010

1111
from test.support.script_helper import assert_python_ok
@@ -19,7 +19,7 @@
1919

2020

2121
with imports_under_tool("i18n"):
22-
from pygettext import parse_spec, normalize, make_escapes
22+
from pygettext import parse_spec, make_escapes, normalize
2323

2424

2525
def normalize_POT_file(pot):

Tools/i18n/pygettext.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ def escape_ascii(s, encoding):
213213
def escape_nonascii(s, encoding):
214214
return ''.join(escapes[b] for b in s.encode(encoding))
215215

216-
space_splitter = re.compile(r'(\s+)').split
216+
# Split a string according to whitespaces and keep
217+
# the whitespaces in the resulting array thanks to
218+
# the capturing group.
219+
_space_splitter = re.compile(r'(\s+)').split
217220

218221
def normalize(s, encoding, options):
219222
# This converts the various Python string types into a format that is
@@ -222,16 +225,17 @@ def normalize(s, encoding, options):
222225
lines = []
223226
for line in s.splitlines(True):
224227
if len(escape(line, encoding)) > options.width:
225-
words = space_splitter(line)
228+
words = _space_splitter(line)
226229
words.reverse()
227230
buf = []
228231
size = 0
229232
while words:
230233
word = words.pop()
231234
escaped_word_len = len(escape(word, encoding))
232-
if size + escaped_word_len <= options.width:
235+
new_size = size + escaped_word_len
236+
if new_size <= options.width:
233237
buf.append(word)
234-
size += escaped_word_len
238+
size = new_size
235239
else:
236240
lines.append(''.join(buf))
237241
buf = [word]
@@ -241,7 +245,7 @@ def normalize(s, encoding, options):
241245
lines.append(line)
242246
if len(lines) <= 1:
243247
return f'"{escape(s, encoding)}"'
244-
return '""\n' + '\n'.join([f'"{escape(line, encoding)}"' for line in lines])
248+
return '""\n' + '\n'.join(f'"{escape(line, encoding)}"' for line in lines)
245249

246250

247251
def containsAny(str, set):

0 commit comments

Comments
 (0)