gh-140481: Improve error message when trying to iterate a Tk widget, image or font (GH-140501)

This commit is contained in:
Serhiy Storchaka 2025-10-30 13:11:56 +02:00 committed by GitHub
parent ad0a3f733b
commit 09b1f10ef7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 59 additions and 22 deletions

View file

@ -1,3 +1,4 @@
import collections.abc
import unittest
import tkinter
from test import support
@ -61,7 +62,33 @@ def test_image_create_photo(self):
self.assertRaises(RuntimeError, tkinter.PhotoImage)
class BitmapImageTest(AbstractTkTest, unittest.TestCase):
class BaseImageTest:
def create(self):
return self.image_class('::img::test', master=self.root,
file=self.testfile)
def test_bug_100814(self):
# gh-100814: Passing a callable option value causes AttributeError.
with self.assertRaises(tkinter.TclError):
self.image_class('::img::test', master=self.root, spam=print)
image = self.image_class('::img::test', master=self.root)
with self.assertRaises(tkinter.TclError):
image.configure(spam=print)
def test_iterable_protocol(self):
image = self.create()
self.assertNotIsSubclass(self.image_class, collections.abc.Iterable)
self.assertNotIsSubclass(self.image_class, collections.abc.Container)
self.assertNotIsInstance(image, collections.abc.Iterable)
self.assertNotIsInstance(image, collections.abc.Container)
with self.assertRaisesRegex(TypeError, 'is not iterable'):
iter(image)
with self.assertRaisesRegex(TypeError, 'is not a container or iterable'):
image in image
class BitmapImageTest(BaseImageTest, AbstractTkTest, unittest.TestCase):
image_class = tkinter.BitmapImage
@classmethod
def setUpClass(cls):
@ -144,26 +171,15 @@ def test_configure_foreground(self):
self.assertEqual(image['foreground'],
'-foreground {} {} #000000 yellow')
def test_bug_100814(self):
# gh-100814: Passing a callable option value causes AttributeError.
with self.assertRaises(tkinter.TclError):
tkinter.BitmapImage('::img::test', master=self.root, spam=print)
image = tkinter.BitmapImage('::img::test', master=self.root)
with self.assertRaises(tkinter.TclError):
image.configure(spam=print)
class PhotoImageTest(AbstractTkTest, unittest.TestCase):
class PhotoImageTest(BaseImageTest, AbstractTkTest, unittest.TestCase):
image_class = tkinter.PhotoImage
@classmethod
def setUpClass(cls):
AbstractTkTest.setUpClass.__func__(cls)
cls.testfile = support.findfile('python.gif', subdir='tkinterdata')
def create(self):
return tkinter.PhotoImage('::img::test', master=self.root,
file=self.testfile)
def colorlist(self, *args):
if tkinter.TkVersion >= 8.6 and self.wantobjects:
return args
@ -282,14 +298,6 @@ def test_configure_palette(self):
image.configure(palette='3/4/2')
self.assertEqual(image['palette'], '3/4/2')
def test_bug_100814(self):
# gh-100814: Passing a callable option value causes AttributeError.
with self.assertRaises(tkinter.TclError):
tkinter.PhotoImage('::img::test', master=self.root, spam=print)
image = tkinter.PhotoImage('::img::test', master=self.root)
with self.assertRaises(tkinter.TclError):
image.configure(spam=print)
def test_blank(self):
image = self.create()
image.blank()