Skip to content

Commit e2880f5

Browse files
committed
read_po: note interface also supports iterable-of-strings, not a filelike
1 parent 40e60a1 commit e2880f5

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

babel/messages/pofile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def _process_comment(self, line) -> None:
291291
# These are called user comments
292292
self.user_comments.append(line[1:].strip())
293293

294-
def parse(self, fileobj: IO[AnyStr]) -> None:
294+
def parse(self, fileobj: IO[AnyStr] | Iterable[AnyStr]) -> None:
295295
"""
296296
Reads from the file-like object `fileobj` and adds any po file
297297
units found in it to the `Catalog` supplied to the constructor.
@@ -329,15 +329,15 @@ def _invalid_pofile(self, line, lineno, msg) -> None:
329329

330330

331331
def read_po(
332-
fileobj: IO[AnyStr],
332+
fileobj: IO[AnyStr] | Iterable[AnyStr],
333333
locale: str | Locale | None = None,
334334
domain: str | None = None,
335335
ignore_obsolete: bool = False,
336336
charset: str | None = None,
337337
abort_invalid: bool = False,
338338
) -> Catalog:
339339
"""Read messages from a ``gettext`` PO (portable object) file from the given
340-
file-like object and return a `Catalog`.
340+
file-like object (or an iterable of lines) and return a `Catalog`.
341341
342342
>>> from datetime import datetime
343343
>>> from io import StringIO
@@ -373,7 +373,7 @@ def read_po(
373373
.. versionadded:: 1.0
374374
Added support for explicit charset argument.
375375
376-
:param fileobj: the file-like object to read the PO file from
376+
:param fileobj: the file-like object (or iterable of lines) to read the PO file from
377377
:param locale: the locale identifier or `Locale` object, or `None`
378378
if the catalog is not bound to a locale (which basically
379379
means it's a template)

tests/messages/test_pofile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,3 +884,12 @@ def test_unknown_language_write():
884884
buf = BytesIO()
885885
pofile.write_po(buf, catalog)
886886
assert 'sr_SP' in buf.getvalue().decode()
887+
888+
889+
def test_iterable_of_strings():
890+
"""
891+
Test we can parse from an iterable of strings.
892+
"""
893+
catalog = pofile.read_po(['msgid "foo"', b'msgstr "Voh"'], locale="en_US")
894+
assert catalog.locale == Locale("en", "US")
895+
assert catalog.get("foo").string == "Voh"

0 commit comments

Comments
 (0)