Skip to content

Commit

Permalink
read_po: note interface also supports iterable-of-strings, not a file…
Browse files Browse the repository at this point in the history
…like
  • Loading branch information
akx committed Mar 26, 2024
1 parent 40e60a1 commit e2880f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 4 additions & 4 deletions babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def _process_comment(self, line) -> None:
# 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.
Expand Down Expand Up @@ -329,15 +329,15 @@ def _invalid_pofile(self, line, lineno, msg) -> None:


def read_po(
fileobj: IO[AnyStr],
fileobj: IO[AnyStr] | Iterable[AnyStr],
locale: str | Locale | None = None,
domain: str | None = None,
ignore_obsolete: bool = False,
charset: str | None = None,
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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tests/messages/test_pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit e2880f5

Please sign in to comment.