diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 63401f10d..15a7f4bf1 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -306,7 +306,9 @@ def _process_comment(self, line) -> None: try: self.locations.append((a, int(b))) except ValueError: - continue + # The colon is part of the filename and there is + # no line number, e.g. `C:/project/main.py` + self.locations.append((location, None)) else: # No line number specified self.locations.append((location, None)) return diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index 00e439b7c..140e9c7ad 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -63,6 +63,29 @@ def test_enclosed_filenames_in_location_comment(): assert True is catalog.is_identical(catalog2) +def test_read_po_location_with_colon_in_filename(): + buf = StringIO('''#: C:/project/main.py lib:utils.py other.py:12 C:/project/views.py:7 +msgid "foo" +msgstr "" +''') + catalog = pofile.read_po(buf) + assert catalog["foo"].locations == [ + ("C:/project/main.py", None), + ("lib:utils.py", None), + ("other.py", 12), + ("C:/project/views.py", 7), + ] + + +def test_location_with_colon_in_filename_roundtrip(): + catalog = Catalog() + catalog.add("foo", locations=[("C:/project/main.py", 3)], string="") + buf = BytesIO() + pofile.write_po(buf, catalog, omit_header=True, include_lineno=False) + buf.seek(0) + assert pofile.read_po(buf)["foo"].locations == [("C:/project/main.py", None)] + + @pytest.mark.parametrize('include_lineno', [False, True]) @pytest.mark.parametrize( ('locations', 'with_lines', 'without_lines'), diff --git a/tests/messages/test_pofile_write.py b/tests/messages/test_pofile_write.py index 6f02b5b42..1800620cf 100644 --- a/tests/messages/test_pofile_write.py +++ b/tests/messages/test_pofile_write.py @@ -358,7 +358,9 @@ def test_silent_location_fallback(): msgstr ""''') catalog = pofile.read_po(buf) assert catalog['missing line number'].locations == [('broken_file.py', None)] - assert catalog['broken line number'].locations == [] + assert catalog['broken line number'].locations == [ + ('broken_file.py:broken_line_number', None), + ] def test_include_lineno():