Skip to content

Commit

Permalink
Fix output of --list-locales to not be a bytes repr.
Browse files Browse the repository at this point in the history
  • Loading branch information
morganwahl committed Feb 26, 2022
1 parent 5ec19a6 commit d94fd35
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 10 additions & 3 deletions babel/messages/frontend.py
Expand Up @@ -893,9 +893,16 @@ def run(self, argv=None):
for identifier in identifiers:
locale = Locale.parse(identifier)
output = format % (identifier, locale.english_name)
print(output.encode(sys.stdout.encoding or
getpreferredencoding() or
'ascii', 'replace'))
# Prevent errors on printing by doing an encode/decode
# roundtrip with errors='replace'.
encoding = (
sys.stdout.encoding
or getpreferredencoding()
or 'ascii'
)
print(
output.encode(encoding, errors='replace').decode(encoding)
)
return 0

if not args:
Expand Down
8 changes: 7 additions & 1 deletion tests/messages/test_frontend.py
Expand Up @@ -762,11 +762,17 @@ def test_usage(self):
""", sys.stderr.getvalue().lower())

def test_list_locales(self):
"""
Test the command with the --list-locales arg.
"""
result = self.cli.run(sys.argv + ['--list-locales'])
self.assertEqual(0, result)
output = sys.stdout.getvalue()
for line in output.splitlines():
self.assertRegex(line, r"b'[0-9A-Za-z_]{2,}\s+[0-9A-Za-z,&\\\-\.() ]+'")
self.assertRegex(
line,
r"[0-9A-Za-z_]{2,}\s+[0-9A-Za-z,&\\\-\.() ]+",
)

def _run_init_catalog(self):
i18n_dir = os.path.join(data_dir, 'project', 'i18n')
Expand Down

0 comments on commit d94fd35

Please sign in to comment.