Skip to content

gh-153333: Read tkinter profile scripts with the source file's encoding#153334

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:fix-tkinter-readprofile-encoding
Open

gh-153333: Read tkinter profile scripts with the source file's encoding#153334
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:fix-tkinter-readprofile-encoding

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Tk.readprofile execs the user's ~/.CLASSNAME.py and ~/.BASENAME.py
profile scripts, read with open(...) and no encoding argument, so the
source was decoded with the locale default encoding. That emits an
EncodingWarning under -X warn_default_encoding and, on a non-UTF-8 locale,
mis-decodes a UTF-8 or coding-cookie'd profile. The bare open(...).read()
also leaked the file descriptor until garbage collection.

This reads the scripts with tokenize.open() instead, which decodes using the
encoding detected from the file (its PEP 263 coding cookie, else UTF-8). That
is the correct encoding for reading Python source, and the context-manager
form closes the file promptly. It is the only unspecified-encoding open() in
Lib/tkinter (grep-verified).

A gui-gated regression test (MiscTest.test_readprofile) is added: it writes a
Latin-1-cookie profile under a temporary HOME and asserts readprofile
decodes it correctly with no EncodingWarning.

…encoding

Tk.readprofile execed the user's ~/.CLASSNAME.py and ~/.BASENAME.py profile scripts read via open() with no encoding argument, i.e. the locale default encoding. That emits an EncodingWarning under -X warn_default_encoding and mis-decodes a profile whose source carries a PEP 263 coding cookie. Read them with tokenize.open() instead, which decodes using the encoding detected from the file (its coding cookie, else UTF-8), matching how Python reads source. As an incidental effect of using the context-manager form, each script is now closed promptly after reading instead of at garbage collection, removing a ResourceWarning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant