Skip to content

Commit de0d976

Browse files
tonghuarootserhiy-storchakaterryjreedy
authored
gh-153319: Read turtledemo scripts with the source file's own encoding (#153321)
The demo viewer read each script with open() and no encoding argument, i.e. the locale default encoding. That emits an EncodingWarning under -X warn_default_encoding and can mis-decode a script on a non-UTF-8 locale. Read the source with tokenize.open() instead, which decodes using the encoding detected from the file (its coding cookie, else UTF-8), matching how Python read the module when it was imported just above. * gh-153319: Read turtledemo scripts as UTF-8 Per review, open the demo scripts with encoding='utf-8' instead of tokenize.open(); the scripts are UTF-8. --------- Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
1 parent a64b7ec commit de0d976

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

‎Lib/turtledemo/__main__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def loadfile(self, filename):
333333
modname = 'turtledemo.' + filename
334334
__import__(modname)
335335
self.module = sys.modules[modname]
336-
with open(self.module.__file__, 'r') as f:
336+
with open(self.module.__file__, encoding='utf-8') as f:
337337
chars = f.read()
338338
self.text.delete("1.0", "end")
339339
self.text.insert("1.0", chars)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`turtledemo` viewer now reads each demo script as UTF-8 instead of
2+
the locale encoding.

0 commit comments

Comments
 (0)