Skip to content

Commit e290181

Browse files
authored
Merge pull request #161 from rtobar/delimited-strings
Detect and report incorrectly delimited strings
2 parents 85f6d1c + 9c760ba commit e290181

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

polib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,10 @@ def parse(self):
13761376
raise IOError('Syntax error in po file %s(line %s): '
13771377
'unescaped double quote found' %
13781378
(fpath, self.current_line))
1379+
if line[0] != '"' or line[-1] != '"':
1380+
raise IOError('Syntax error in po file %s(line %s): '
1381+
'string not delimited by double quotes' %
1382+
(fpath, self.current_line))
13791383
self.current_token = line
13801384
self.process(keywords[tokens[0]])
13811385
continue
@@ -1394,6 +1398,10 @@ def parse(self):
13941398
raise IOError('Syntax error in po file %s(line %s): '
13951399
'unescaped double quote found' %
13961400
(fpath, self.current_line))
1401+
if line[-1] != '"':
1402+
raise IOError('Syntax error in po file %s(line %s): '
1403+
'string not delimited by double quotes' %
1404+
(fpath, self.current_line))
13971405
self.process('mc')
13981406

13991407
elif line[:7] == 'msgstr[':

tests/test_ufeff.po

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# test for pofile/mofile with ufeff
22
msgid ""
33
msgstr ""
4-
"Project-Id-Version: django
5-
"
4+
"Project-Id-Version: django"
65

76
msgid "foo"
87
msgstr "bar"

tests/tests.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_pofile_and_mofile1(self):
2727
data = u('''# test for pofile/mofile with string buffer
2828
msgid ""
2929
msgstr ""
30-
"Project-Id-Version: django\n"
30+
"Project-Id-Version: django"
3131
3232
msgid "foo"
3333
msgstr "bar"
@@ -116,7 +116,7 @@ def test_ufeff_data_pofile(self):
116116
data = u('''\ufeff# test for pofile/mofile with ufeff
117117
msgid ""
118118
msgstr ""
119-
"Project-Id-Version: django\n"
119+
"Project-Id-Version: django"
120120
121121
msgid "foo"
122122
msgstr "bar"
@@ -258,6 +258,35 @@ def test_unescaped_double_quote4(self):
258258
msg = 'Syntax error in po file (line 4): unescaped double quote found'
259259
self.assertEqual(str(exc), msg)
260260

261+
def test_no_double_quote_delimiters(self):
262+
"""
263+
Test that polib reports an error when a string is not delimited by double quotes.
264+
"""
265+
invalid_msgstr = r'''
266+
msgid "A"
267+
msgstr *B"
268+
'''
269+
invalid_msgid = r'''
270+
msgid "A/
271+
msgstr "B"
272+
'''
273+
invalid_msgid_plural = r'''
274+
msgid_plural A
275+
msgstr "B"
276+
'''
277+
invalid_msgstr_continuation = r'''
278+
msgid "A"
279+
msgstr ""
280+
"B
281+
'''
282+
for data in (invalid_msgid, invalid_msgid_plural, invalid_msgstr, invalid_msgstr_continuation):
283+
try:
284+
polib.pofile(data)
285+
self.fail("Strings not delimited by double quotes not detected")
286+
except IOError as ex:
287+
msg = 'string not delimited by double quotes'
288+
self.assertIn(msg, str(ex))
289+
261290
def test_syntax_error1(self):
262291
"""
263292
Test that syntax error is raised while processing a symbol parsing.

0 commit comments

Comments
 (0)