]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Introduce invariant that _invalid_pofile() takes unicode line.
authorNiklas Hambüchen <mail@nh2.me>
Tue, 28 Jan 2020 01:46:25 +0000 (02:46 +0100)
committerNiklas Hambüchen <mail@nh2.me>
Tue, 28 Jan 2020 01:58:42 +0000 (02:58 +0100)
This makes debugging and reasoning about the code easier;
otherwise it is surprising that sometimes `line` is a unicode
and sometimes not.

So far, when it was not, it could either be only `""` or
`'Algo esta mal'`; thus this commit makes those two u"" strings.
In all other cases, it was guaranteed that it's unicode,
because all code paths leading to `_invalid_pofile()` went through

    if not isinstance(line, text_type):
        line = line.decode(self.catalog.charset)

before.

babel/messages/pofile.py
tests/messages/test_pofile.py

index 93b0697c6886f3910c7abf532ad374e14236c56d..f6771bedf5b9f0394b31c8de8d56875c2412f79d 100644 (file)
@@ -178,7 +178,7 @@ class PoFileParser(object):
             string = ['' for _ in range(self.catalog.num_plurals)]
             for idx, translation in self.translations:
                 if idx >= self.catalog.num_plurals:
-                    self._invalid_pofile("", self.offset, "msg has more translations than num_plurals of catalog")
+                    self._invalid_pofile(u"", self.offset, "msg has more translations than num_plurals of catalog")
                     continue
                 string[idx] = translation.denormalize()
             string = tuple(string)
@@ -319,6 +319,7 @@ class PoFileParser(object):
             self._add_message()
 
     def _invalid_pofile(self, line, lineno, msg):
+        assert isinstance(line, text_type)
         if self.abort_invalid:
             raise PoFileError(msg, self.catalog, line, lineno)
         print("WARNING:", msg)
index e77fa6e02d0c6b7d923dae1cd0e8953035485e63..214ddf5d504cdafbe4f185fa1a9ac710db391ebb 100644 (file)
@@ -480,7 +480,7 @@ msgstr[2] "Vohs [text]"
     def test_invalid_pofile_with_abort_flag(self):
         parser = pofile.PoFileParser(None, abort_invalid=True)
         lineno = 10
-        line = 'Algo esta mal'
+        line = u'Algo esta mal'
         msg = 'invalid file'
         with self.assertRaises(pofile.PoFileError) as e:
             parser._invalid_pofile(line, lineno, msg)