Skip to content

Commit 794fc8b

Browse files
Serhiy's suggestions
1 parent ae53774 commit 794fc8b

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

Lib/test/test_tools/test_i18n.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -517,49 +517,50 @@ def test_parse_keyword_spec(self):
517517
parse_spec(spec)
518518
self.assertEqual(str(cm.exception), message)
519519

520-
def test_normalize_multiline(self):
521-
# required to set up normalize
522-
options = SimpleNamespace(width=78)
523-
make_escapes(True)
520+
# required to set up normalize
521+
make_escapes(True)
524522

523+
def test_normalize_multiline(self):
525524
s = 'multi-line\n translation'
526525
s_expected = '""\n"multi-line\\n"\n" translation"'
527526

528-
data = normalize(s, 'UTF-8', 'msgid', options)
527+
data = normalize(s, 'UTF-8', 'msgid', 78)
529528
self.assertEqual(s_expected, data)
530529

531530
def test_normalize_wrap(self):
532-
# required to set up normalize
533-
options = SimpleNamespace(width=30)
534-
make_escapes(True)
531+
s = 'fee fi fo fum fee fi ' # len = 29
532+
s_expected = '"fee fi fo fum fee fi "'
533+
data = normalize(s, 'UTF-8', 'msgid', 30)
534+
self.assertEqual(s_expected, data)
535535

536-
s = 'this string should be wrapped to 30 chars'
537-
s_expected = '""\n"this string should be "\n"wrapped to 30 chars"'
536+
s = 'fee fi fo fum fee fi f' # len = 30
537+
s_expected = '"fee fi fo fum fee fi f"'
538+
data = normalize(s, 'UTF-8', 'msgid', 30)
539+
self.assertEqual(s_expected, data)
538540

539-
data = normalize(s, 'UTF-8', 'msgid', options)
541+
s = 'fee fi fo fum fee fi fo' # len = 31
542+
s_expected = '""\n"fee fi fo fum fee fi fo"'
543+
data = normalize(s, 'UTF-8', 'msgid', 30)
540544
self.assertEqual(s_expected, data)
541545

542546
def test_normalize_nostr(self):
543-
# required to set up normalize
544-
options = SimpleNamespace(width=30)
545-
make_escapes(True)
546-
547-
s = ''
548-
s_expected = '""'
549-
550-
data = normalize(s, 'UTF-8', 'msgid', options)
551-
self.assertEqual(s_expected, data)
547+
data = normalize('', 'UTF-8', 'msgid', 30)
548+
self.assertEqual('""', data)
552549

553-
def test_normalize_short_width(self):
550+
def test_normalize_single_word(self):
554551
# required to set up normalize
555-
options = SimpleNamespace(width=3)
556552
make_escapes(True)
557-
558-
s = 'foos'
559-
s_expected = '"foos"'
560-
561-
data = normalize(s, 'UTF-8', 'msgid', options)
562-
self.assertEqual(s_expected, data)
553+
for s in ("fee", "fi", "fo", "fums"):
554+
data = normalize(s, 'UTF-8', 'msgid', 3)
555+
self.assertNotIn('""', data) # did not wrap
556+
557+
def test_normalize_split_on_whitespace(self):
558+
for space in (' ', ' ', ' ', '\t', '\r'):
559+
s = f'longlonglong{space}word'
560+
space = {'\t': '\\t', '\r': '\\r'}.get(space, space)
561+
s_expected = f'""\n"longlonglong{space}"\n"word"'
562+
data = normalize(s, 'UTF-8', 'msgid', 10)
563+
self.assertEqual(s_expected, data)
563564

564565

565566
def extract_from_snapshots():

Tools/i18n/pygettext.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155

156156
__version__ = '1.5'
157157

158+
from test.test_doctest.test_doctest import wrapped
158159

159160
# The normal pot-file header. msgmerge and Emacs's po-mode work better if it's
160161
# there.
@@ -216,24 +217,26 @@ def escape_nonascii(s, encoding):
216217

217218
_space_splitter = re.compile(r'\s+|\S+\s*')
218219

219-
def normalize(s, encoding, prefix, options):
220+
def normalize(s, encoding, prefix, width):
220221
# This converts the various Python string types into a format that is
221222
# appropriate for .po files, namely much closer to C style,
222223
# while wrapping to options.width.
223224
lines = []
225+
wrap = False
224226
for line in s.splitlines(True):
225227
escaped_line = escape(line, encoding)
226-
if len(escaped_line) + len(prefix) + 3 > options.width:
228+
if len(escaped_line) + len(prefix) + 3 > width:
229+
wrap = True
227230
words = _space_splitter.findall(line)
228231
words.reverse()
229232
buf = []
230-
size = 0
233+
size = 2
231234
while words:
232235
word = words.pop()
233236
escaped_word = escape(word, encoding)
234237
escaped_word_len = len(escaped_word)
235238
new_size = size + escaped_word_len
236-
if new_size + 2 <= options.width or not buf:
239+
if new_size <= width or not buf:
237240
buf.append(escaped_word)
238241
size = new_size
239242
else:
@@ -243,7 +246,7 @@ def normalize(s, encoding, prefix, options):
243246
lines.append(''.join(buf))
244247
else:
245248
lines.append(escaped_line)
246-
if len(lines) <= 1:
249+
if len(lines) <= 1 and (not wrap or len(_space_splitter.findall(lines[0])) == 1):
247250
return f'"{escape(s, encoding)}"'
248251
return '""\n' + '\n'.join(f'"{line}"' for line in lines)
249252

@@ -636,10 +639,10 @@ def write_pot_file(messages, options, fp):
636639
# to skip translating some unimportant docstrings.
637640
print('#, docstring', file=fp)
638641
if msg.msgctxt is not None:
639-
print('msgctxt', normalize(msg.msgctxt, encoding, 'msgctxt', options), file=fp)
640-
print('msgid', normalize(msg.msgid, encoding, 'msgid', options), file=fp)
642+
print('msgctxt', normalize(msg.msgctxt, encoding, 'msgctxt', options.width), file=fp)
643+
print('msgid', normalize(msg.msgid, encoding, 'msgid', options.width), file=fp)
641644
if msg.msgid_plural is not None:
642-
print('msgid_plural', normalize(msg.msgid_plural, encoding, 'msgid_plural', options), file=fp)
645+
print('msgid_plural', normalize(msg.msgid_plural, encoding, 'msgid_plural', options.width), file=fp)
643646
print('msgstr[0] ""', file=fp)
644647
print('msgstr[1] ""\n', file=fp)
645648
else:

0 commit comments

Comments
 (0)