Commits

Anonymous committed dbafdc6

Add check for unescaped quotes at the beginning of the string + unit tests

Comments (0)

Files changed (2)

             # msgid, msgid_plural, msgctxt & msgstr.
             if tokens[0] in keywords and nb_tokens > 1:
                 line = line[len(tokens[0]):].lstrip()
-                if re.search(r'[^\\]"', line[1:-1]):
+                if re.search(r'([^\\]|^)"', line[1:-1]):
                     raise IOError('Syntax error in po file %s (line %s): '\
                                   'unescaped double quote found' % \
                                   (self.instance.fpath, i))
 
             elif line[:1] == '"':
                 # we are on a continuation line
-                if re.search(r'[^\\]"', line[1:-1]):
+                if re.search(r'([^\\]|^)"', line[1:-1]):
                     raise IOError('Syntax error in po file %s (line %s): '\
                                   'unescaped double quote found' % \
                                   (self.instance.fpath, i))
             msg = 'Syntax error in po file None (line 4): unescaped double quote found'
             self.assertEqual(str(exc), msg)
 
+    def test_unescaped_double_quote3(self):
+        """
+        Test that polib reports an error when unescaped double quote is found at the beginning of the string.
+        """
+        data = r'''
+msgid "Some msgid with \"double\" quotes"
+msgid ""Some msgstr with double\" quotes"
+'''
+        try:
+            po = polib.pofile(data)
+            self.fail("Unescaped quote not detected")
+        except IOError, exc:
+            msg = 'Syntax error in po file None (line 3): unescaped double quote found'
+
+    def test_unescaped_double_quote4(self):
+        """
+        Test that polib reports an error when unescaped double quote is found at the beginning of the string.
+        """
+        data = r'''
+msgid "Some msgid with \"double\" quotes"
+msgstr ""
+""Some msgstr with double\" quotes"
+'''
+        try:
+            po = polib.pofile(data)
+            self.fail("Unescaped quote not detected")
+        except IOError, exc:
+            msg = 'Syntax error in po file None (line 4): unescaped double quote found'
+            self.assertEqual(str(exc), msg)
+    
     def test_detect_encoding1(self):
         """
         Test that given enconding is returned when file has no encoding defined.