Skip to content

Commit 5e9d423

Browse files
committed
gh-153333: Move readprofile test to TkTest and cover two encodings
1 parent edb85b6 commit 5e9d423

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

Lib/test/test_tkinter/test_misc.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sys
66
import textwrap
77
import unittest
8-
import warnings
98
import weakref
109
import tkinter
1110
from tkinter import TclError
@@ -366,24 +365,6 @@ def test_getdouble(self):
366365
self.assertEqual(self.root.getdouble(3), 3.0)
367366
self.assertRaises(ValueError, self.root.getdouble, 'spam')
368367

369-
def test_readprofile(self):
370-
# gh-153333: readprofile() reads the profile scripts with the source
371-
# file's encoding (here a Latin-1 coding cookie), not the locale
372-
# default encoding.
373-
name = 'readprofiletest'
374-
script = ("# -*- coding: latin-1 -*-\n"
375-
"self.readprofile_result = 'caf\xe9'\n")
376-
self.addCleanup(self.root.__dict__.pop, 'readprofile_result', None)
377-
with os_helper.temp_dir() as home:
378-
with open(os.path.join(home, '.%s.py' % name), 'wb') as f:
379-
f.write(script.encode('latin-1'))
380-
with os_helper.EnvironmentVarGuard() as env:
381-
env['HOME'] = home
382-
with warnings.catch_warnings():
383-
warnings.simplefilter('error', EncodingWarning)
384-
self.root.readprofile(name, name)
385-
self.assertEqual(self.root.readprofile_result, 'caf\xe9')
386-
387368
def test_getvar(self):
388369
self.root.setvar('test_var', 'hello')
389370
self.assertEqual(self.root.getvar('test_var'), 'hello')
@@ -829,6 +810,26 @@ def test_iterable_protocol(self):
829810

830811
class TkTest(AbstractTkTest, unittest.TestCase):
831812

813+
def test_readprofile(self):
814+
# gh-153333: profile scripts are decoded with their own coding cookie,
815+
# not the locale encoding. Two cookies so no locale can mask the bug.
816+
profiles = {
817+
'.RpClass.py': ('latin-1', "self._rp_latin1 = 'caf\xe9'"),
818+
'.rpbase.py': ('utf-8', "self._rp_utf8 = 'caf\xe9'"),
819+
}
820+
self.addCleanup(self.root.__dict__.pop, '_rp_latin1', None)
821+
self.addCleanup(self.root.__dict__.pop, '_rp_utf8', None)
822+
with (os_helper.temp_dir() as home,
823+
os_helper.EnvironmentVarGuard() as env):
824+
env['HOME'] = home
825+
for filename, (encoding, body) in profiles.items():
826+
script = '# -*- coding: %s -*-\n%s\n' % (encoding, body)
827+
with open(os.path.join(home, filename), 'wb') as f:
828+
f.write(script.encode(encoding))
829+
self.root.readprofile('rpbase', 'RpClass')
830+
self.assertEqual(self.root._rp_latin1, 'caf\xe9')
831+
self.assertEqual(self.root._rp_utf8, 'caf\xe9')
832+
832833
def test_className(self):
833834
# The className argument sets the class of the root window. Tk
834835
# title-cases it: the first letter is upper-cased, the rest lower-cased.

0 commit comments

Comments
 (0)