gh-153333: Read tkinter profile scripts with the source file's encoding#153334
Open
tonghuaroot wants to merge 1 commit into
Open
gh-153333: Read tkinter profile scripts with the source file's encoding#153334tonghuaroot wants to merge 1 commit into
tonghuaroot wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tk.readprofileexecs the user's~/.CLASSNAME.pyand~/.BASENAME.pyprofile scripts, read with
open(...)and noencodingargument, so thesource was decoded with the locale default encoding. That emits an
EncodingWarningunder-X warn_default_encodingand, 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 theencoding 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()inLib/tkinter(grep-verified).A gui-gated regression test (
MiscTest.test_readprofile) is added: it writes aLatin-1-cookie profile under a temporary
HOMEand assertsreadprofiledecodes it correctly with no
EncodingWarning.