]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
read_po: note interface also supports iterable-of-strings, not a filelike
authorAarni Koskela <akx@iki.fi>
Tue, 26 Mar 2024 13:32:00 +0000 (15:32 +0200)
committerAarni Koskela <akx@iki.fi>
Tue, 26 Mar 2024 13:32:00 +0000 (15:32 +0200)
babel/messages/pofile.py
tests/messages/test_pofile.py

index b64a5085e81104498a7894c7624e90f596b420e4..7c8518de7a0affe42cc58f50db7c194c377b34c6 100644 (file)
@@ -291,7 +291,7 @@ class PoFileParser:
             # These are called user comments
             self.user_comments.append(line[1:].strip())
 
-    def parse(self, fileobj: IO[AnyStr]) -> None:
+    def parse(self, fileobj: IO[AnyStr] | Iterable[AnyStr]) -> None:
         """
         Reads from the file-like object `fileobj` and adds any po file
         units found in it to the `Catalog` supplied to the constructor.
@@ -329,7 +329,7 @@ class PoFileParser:
 
 
 def read_po(
-    fileobj: IO[AnyStr],
+    fileobj: IO[AnyStr] | Iterable[AnyStr],
     locale: str | Locale | None = None,
     domain: str | None = None,
     ignore_obsolete: bool = False,
@@ -337,7 +337,7 @@ def read_po(
     abort_invalid: bool = False,
 ) -> Catalog:
     """Read messages from a ``gettext`` PO (portable object) file from the given
-    file-like object and return a `Catalog`.
+    file-like object (or an iterable of lines) and return a `Catalog`.
 
     >>> from datetime import datetime
     >>> from io import StringIO
@@ -373,7 +373,7 @@ def read_po(
     .. versionadded:: 1.0
        Added support for explicit charset argument.
 
-    :param fileobj: the file-like object to read the PO file from
+    :param fileobj: the file-like object (or iterable of lines) to read the PO file from
     :param locale: the locale identifier or `Locale` object, or `None`
                    if the catalog is not bound to a locale (which basically
                    means it's a template)
index 043f9c8bd15af6d7561e5a5fde5dfadac23a0531..d322857d57e923d8984cc445aa49254491e32b08 100644 (file)
@@ -884,3 +884,12 @@ def test_unknown_language_write():
     buf = BytesIO()
     pofile.write_po(buf, catalog)
     assert 'sr_SP' in buf.getvalue().decode()
+
+
+def test_iterable_of_strings():
+    """
+    Test we can parse from an iterable of strings.
+    """
+    catalog = pofile.read_po(['msgid "foo"', b'msgstr "Voh"'], locale="en_US")
+    assert catalog.locale == Locale("en", "US")
+    assert catalog.get("foo").string == "Voh"