Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_tools/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,22 @@ def test_multiple_keywords_same_funcname_errors(self):
'\tkeyword="_": Expected a string constant for argument 1, got 42\n'
'\tkeyword="_:2": Expected a string constant for argument 2, got y\n')

def test_utf8_encoding(self):
with temp_cwd(None):
with open('test.py', 'w', encoding='utf-8') as fp:
fp.write('_("áscii")')

res = assert_python_ok(self.script,
'test.py',
PYTHONCOERCECLOCALE="0",
PYTHONUTF8="0",
LANG="C",
LC_ALL="C")
self.assertEqual(res.err, b'')

with open('messages.pot', encoding='utf-8') as fp:
pot_content = fp.read()
self.assertIn('áscii', pot_content)

def extract_from_snapshots():
snapshots = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:program:`pygettext`: open files with the uft-8 encoding
6 changes: 3 additions & 3 deletions Tools/i18n/pygettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ class Options:
elif opt in ('-x', '--exclude-file'):
options.excludefilename = arg
elif opt in ('-X', '--no-docstrings'):
fp = open(arg)
fp = open(arg, encoding='utf-8')
try:
while 1:
line = fp.readline()
Expand All @@ -794,7 +794,7 @@ class Options:
# initialize list of strings to exclude
if options.excludefilename:
try:
with open(options.excludefilename) as fp:
with open(options.excludefilename, encoding='utf-8') as fp:
options.toexclude = fp.readlines()
except OSError:
print(f"Can't read --exclude-file: {options.excludefilename}",
Expand Down Expand Up @@ -834,7 +834,7 @@ class Options:
else:
if options.outpath:
options.outfile = os.path.join(options.outpath, options.outfile)
fp = open(options.outfile, 'w')
fp = open(options.outfile, 'w', encoding='utf-8')
closep = 1
try:
write_pot_file(visitor.messages, options, fp)
Expand Down
Loading