Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect and report incorrectly delimited strings #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions polib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,10 @@ def parse(self):
raise IOError('Syntax error in po file %s(line %s): '
'unescaped double quote found' %
(fpath, self.current_line))
if line[0] != '"' or line[-1] != '"':
raise IOError('Syntax error in po file %s(line %s): '
'string not delimited by double quotes' %
(fpath, self.current_line))
self.current_token = line
self.process(keywords[tokens[0]])
continue
Expand All @@ -1394,6 +1398,10 @@ def parse(self):
raise IOError('Syntax error in po file %s(line %s): '
'unescaped double quote found' %
(fpath, self.current_line))
if line[-1] != '"':
raise IOError('Syntax error in po file %s(line %s): '
'string not delimited by double quotes' %
(fpath, self.current_line))
self.process('mc')

elif line[:7] == 'msgstr[':
Expand Down
3 changes: 1 addition & 2 deletions tests/test_ufeff.po
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# test for pofile/mofile with ufeff
msgid ""
msgstr ""
"Project-Id-Version: django
"
"Project-Id-Version: django"

msgid "foo"
msgstr "bar"
33 changes: 31 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_pofile_and_mofile1(self):
data = u('''# test for pofile/mofile with string buffer
msgid ""
msgstr ""
"Project-Id-Version: django\n"
"Project-Id-Version: django"

msgid "foo"
msgstr "bar"
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_ufeff_data_pofile(self):
data = u('''\ufeff# test for pofile/mofile with ufeff
msgid ""
msgstr ""
"Project-Id-Version: django\n"
"Project-Id-Version: django"

msgid "foo"
msgstr "bar"
Expand Down Expand Up @@ -258,6 +258,35 @@ def test_unescaped_double_quote4(self):
msg = 'Syntax error in po file (line 4): unescaped double quote found'
self.assertEqual(str(exc), msg)

def test_no_double_quote_delimiters(self):
"""
Test that polib reports an error when a string is not delimited by double quotes.
"""
invalid_msgstr = r'''
msgid "A"
msgstr *B"
'''
invalid_msgid = r'''
msgid "A/
msgstr "B"
'''
invalid_msgid_plural = r'''
msgid_plural A
msgstr "B"
'''
invalid_msgstr_continuation = r'''
msgid "A"
msgstr ""
"B
'''
for data in (invalid_msgid, invalid_msgid_plural, invalid_msgstr, invalid_msgstr_continuation):
try:
polib.pofile(data)
self.fail("Strings not delimited by double quotes not detected")
except IOError as ex:
msg = 'string not delimited by double quotes'
self.assertIn(msg, str(ex))

def test_syntax_error1(self):
"""
Test that syntax error is raised while processing a symbol parsing.
Expand Down