From 61220d1b1a3418e8f3293ad2b17fef02d5a20815 Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Wed, 8 Jul 2026 18:56:09 +0800 Subject: [PATCH 1/2] gh-153319: Read turtledemo scripts with the source file's own encoding 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. --- Lib/turtledemo/__main__.py | 3 ++- .../Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst diff --git a/Lib/turtledemo/__main__.py b/Lib/turtledemo/__main__.py index 7c2d753f4c31113..4b7f2814b75e5ad 100644 --- a/Lib/turtledemo/__main__.py +++ b/Lib/turtledemo/__main__.py @@ -85,6 +85,7 @@ """ import sys import os +import tokenize from tkinter import * from idlelib.colorizer import ColorDelegator, color_config @@ -333,7 +334,7 @@ def loadfile(self, filename): modname = 'turtledemo.' + filename __import__(modname) self.module = sys.modules[modname] - with open(self.module.__file__, 'r') as f: + with tokenize.open(self.module.__file__) as f: chars = f.read() self.text.delete("1.0", "end") self.text.insert("1.0", chars) diff --git a/Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst b/Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst new file mode 100644 index 000000000000000..b67b124dc575928 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst @@ -0,0 +1,3 @@ +The :mod:`turtledemo` viewer now reads each demo script using +:func:`tokenize.open`, decoding it with the encoding detected from the +source file. From d8a13aaa8308321c832e613f3ad1dbd361d8ed5b Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Sun, 20 Sep 2026 12:25:32 +0800 Subject: [PATCH 2/2] 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. --- Lib/turtledemo/__main__.py | 3 +-- .../Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/turtledemo/__main__.py b/Lib/turtledemo/__main__.py index 4b7f2814b75e5ad..cffc08f59b99d26 100644 --- a/Lib/turtledemo/__main__.py +++ b/Lib/turtledemo/__main__.py @@ -85,7 +85,6 @@ """ import sys import os -import tokenize from tkinter import * from idlelib.colorizer import ColorDelegator, color_config @@ -334,7 +333,7 @@ def loadfile(self, filename): modname = 'turtledemo.' + filename __import__(modname) self.module = sys.modules[modname] - with tokenize.open(self.module.__file__) as f: + with open(self.module.__file__, encoding='utf-8') as f: chars = f.read() self.text.delete("1.0", "end") self.text.insert("1.0", chars) diff --git a/Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst b/Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst index b67b124dc575928..95f8e2fb7f4f8be 100644 --- a/Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst +++ b/Misc/NEWS.d/next/Library/2026-07-08-19-40-00.gh-issue-153319.Qk7bE2.rst @@ -1,3 +1,2 @@ -The :mod:`turtledemo` viewer now reads each demo script using -:func:`tokenize.open`, decoding it with the encoding detected from the -source file. +The :mod:`turtledemo` viewer now reads each demo script as UTF-8 instead of +the locale encoding.