File tree Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Original file line number Diff line number Diff line change @@ -300,16 +300,25 @@ def _process_comment(self, line) -> None:
300
300
301
301
def parse (self , fileobj : IO [AnyStr ] | Iterable [AnyStr ]) -> None :
302
302
"""
303
- Reads from the file-like object `fileobj` and adds any po file
304
- units found in it to the `Catalog` supplied to the constructor.
303
+ Reads from the file-like object (or iterable of string-likes) `fileobj`
304
+ and adds any po file units found in it to the `Catalog`
305
+ supplied to the constructor.
306
+
307
+ All of the items in the iterable must be the same type; either `str`
308
+ or `bytes` (decoded with the catalog charset), but not a mixture.
305
309
"""
310
+ needs_decode = None
306
311
307
312
for lineno , line in enumerate (fileobj ):
308
313
line = line .strip ()
309
- if not isinstance (line , str ):
310
- line = line .decode (self .catalog .charset )
314
+ if needs_decode is None :
315
+ # If we don't yet know whether we need to decode,
316
+ # let's find out now.
317
+ needs_decode = not isinstance (line , str )
311
318
if not line :
312
319
continue
320
+ if needs_decode :
321
+ line = line .decode (self .catalog .charset )
313
322
if line [0 ] == '#' :
314
323
if line [:2 ] == '#~' :
315
324
self ._process_message_line (lineno , line [2 :].lstrip (), obsolete = True )
Original file line number Diff line number Diff line change @@ -1068,11 +1068,18 @@ def test_iterable_of_strings():
1068
1068
"""
1069
1069
Test we can parse from an iterable of strings.
1070
1070
"""
1071
- catalog = pofile .read_po (['msgid "foo"' , b 'msgstr "Voh"' ], locale = "en_US" )
1071
+ catalog = pofile .read_po (['msgid "foo"' , 'msgstr "Voh"' ], locale = "en_US" )
1072
1072
assert catalog .locale == Locale ("en" , "US" )
1073
1073
assert catalog .get ("foo" ).string == "Voh"
1074
1074
1075
1075
1076
+ @pytest .mark .parametrize ("order" , [1 , - 1 ])
1077
+ def test_iterable_of_mismatching_strings (order ):
1078
+ # Mixing and matching byteses and strs in the same read_po call is not allowed.
1079
+ with pytest .raises (Exception ): # noqa: B017 (will raise either TypeError or AttributeError)
1080
+ pofile .read_po (['msgid "foo"' , b'msgstr "Voh"' ][::order ])
1081
+
1082
+
1076
1083
def test_issue_1087 ():
1077
1084
buf = StringIO (r'''
1078
1085
msgid ""
You can’t perform that action at this time.
0 commit comments