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
20 changes: 20 additions & 0 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import collections.abc
import functools
import os
import platform
import sys
import textwrap
import unittest
import warnings
import weakref
import tkinter
from tkinter import TclError
Expand Down Expand Up @@ -364,6 +366,24 @@ def test_getdouble(self):
self.assertEqual(self.root.getdouble(3), 3.0)
self.assertRaises(ValueError, self.root.getdouble, 'spam')

def test_readprofile(self):
# gh-153333: readprofile() reads the profile scripts with the source
# file's encoding (here a Latin-1 coding cookie), not the locale
# default encoding.
name = 'readprofiletest'
script = ("# -*- coding: latin-1 -*-\n"
"self.readprofile_result = 'caf\xe9'\n")
self.addCleanup(self.root.__dict__.pop, 'readprofile_result', None)
with os_helper.temp_dir() as home:
with open(os.path.join(home, '.%s.py' % name), 'wb') as f:
f.write(script.encode('latin-1'))
with os_helper.EnvironmentVarGuard() as env:
env['HOME'] = home
with warnings.catch_warnings():
warnings.simplefilter('error', EncodingWarning)
self.root.readprofile(name, name)
self.assertEqual(self.root.readprofile_result, 'caf\xe9')

def test_getvar(self):
self.root.setvar('test_var', 'hello')
self.assertEqual(self.root.getvar('test_var'), 'hello')
Expand Down
7 changes: 5 additions & 2 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,7 @@ def readprofile(self, baseName, className):
the Tcl Interpreter and calls exec on the contents of .BASENAME.py and
.CLASSNAME.py if such a file exists in the home directory."""
import os
import tokenize
if 'HOME' in os.environ: home = os.environ['HOME']
else: home = os.curdir
class_tcl = os.path.join(home, '.%s.tcl' % className)
Expand All @@ -2750,11 +2751,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 tokenize.open(class_py) 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 tokenize.open(base_py) 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,2 @@
The ``readprofile`` method of :class:`tkinter.Tk` now reads the user's
profile scripts using :func:`tokenize.open`.
Loading