Skip to content
Merged
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
21 changes: 21 additions & 0 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections.abc
import functools
import os
import platform
import sys
import textwrap
Expand Down Expand Up @@ -755,6 +756,26 @@ def test_iterable_protocol(self):

class TkTest(AbstractTkTest, unittest.TestCase):

def test_readprofile(self):
# gh-153333: profile scripts are decoded with their own coding cookie,
# not the locale encoding. Two cookies so no locale can mask the bug.
profiles = {
'.RpClass.py': ('latin-1', "self._rp_latin1 = 'caf\xe9'"),
'.rpbase.py': ('utf-8', "self._rp_utf8 = 'caf\xe9'"),
}
self.addCleanup(self.root.__dict__.pop, '_rp_latin1', None)
self.addCleanup(self.root.__dict__.pop, '_rp_utf8', None)
with (os_helper.temp_dir() as home,
os_helper.EnvironmentVarGuard() as env):
env['HOME'] = home
for filename, (encoding, body) in profiles.items():
script = '# -*- coding: %s -*-\n%s\n' % (encoding, body)
with open(os.path.join(home, filename), 'wb') as f:
f.write(script.encode(encoding))
self.root.readprofile('rpbase', 'RpClass')
self.assertEqual(self.root._rp_latin1, 'caf\xe9')
self.assertEqual(self.root._rp_utf8, 'caf\xe9')

def test_className(self):
# The className argument sets the class of the root window. Tk
# title-cases it: the first letter is upper-cased, the rest lower-cased.
Expand Down
6 changes: 4 additions & 2 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2639,11 +2639,13 @@ def readprofile(self, baseName, className):
if os.path.isfile(class_tcl):
self.tk.call('source', class_tcl)
if os.path.isfile(class_py):
exec(open(class_py).read(), dir)
with open(class_py, 'rb') as f:
exec(f.read(), dir)
if os.path.isfile(base_tcl):
self.tk.call('source', base_tcl)
if os.path.isfile(base_py):
exec(open(base_py).read(), dir)
with open(base_py, 'rb') as f:
exec(f.read(), dir)

def report_callback_exception(self, exc, val, tb):
"""Report callback exception on sys.stderr.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The ``readprofile`` method of :class:`tkinter.Tk` now reads the user's
profile scripts using the encoding declared in the file, instead of the
locale encoding.
Loading