Skip to content

bpo-45979: Fix Tkinter tests with old Tk #29913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Lib/test/test_tcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,7 @@ def test_splitlist(self):
]
tk_patchlevel = get_tk_patchlevel()
if tcl_version >= (8, 5):
if not self.wantobjects or tk_patchlevel < (8, 5, 5):
# Before 8.5.5 dicts were converted to lists through string
if not self.wantobjects:
expected = ('12', '\u20ac', '\xe2\x82\xac', '3.4')
else:
expected = (12, '\u20ac', b'\xe2\x82\xac', (3.4,))
Expand Down Expand Up @@ -645,8 +644,7 @@ def test_splitdict(self):
if tcl_version >= (8, 5):
arg = tcl.call('dict', 'create',
'-a', (1, 2, 3), '-something', 'foo', 'status', ())
if not self.wantobjects or get_tk_patchlevel() < (8, 5, 5):
# Before 8.5.5 dicts were converted to lists through string
if not self.wantobjects:
expected = {'a': '1 2 3', 'something': 'foo', 'status': ''}
else:
expected = {'a': (1, 2, 3), 'something': 'foo', 'status': ''}
Expand Down
6 changes: 5 additions & 1 deletion Lib/tkinter/simpledialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ def _setup_dialog(w):
w.tk.call("::tk::unsupported::MacWindowStyle", "style",
w, "moveableModal", "")
elif w._windowingsystem == "x11":
w.wm_attributes("-type", "dialog")
try:
w.wm_attributes("-type", "dialog")
except TclError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than catching an error, would we be better checking for supported Tk version like you've done elsewhere? (if get_tk_patchlevel() >= (8, 5, 9):)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no get_tk_patchlevel() in the tkinter module. It is only defined in tests.

# only supported since Tk 8.5.9
pass

# --------------------------------------------------------------------
# convenience dialogues
Expand Down
6 changes: 4 additions & 2 deletions Lib/tkinter/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tkinter
import enum
from test import support
from tkinter.test.support import AbstractTkTest, AbstractDefaultRootTest
from tkinter.test.support import AbstractTkTest, AbstractDefaultRootTest, get_tk_patchlevel

support.requires('gui')

Expand Down Expand Up @@ -215,7 +215,9 @@ def assertApprox(col1, col2):
self.assertEqual(rgb('red'), (65535, 0, 0))
self.assertEqual(rgb('dark slate blue'), (18504, 15677, 35723))
# #RGB - extends each 4-bit hex value to be 16-bit.
self.assertEqual(rgb('#F0F'), (0xFFFF, 0x0000, 0xFFFF))
self.assertEqual(rgb('#F0F'), (0xFFFF, 0x0000, 0xFFFF)
if get_tk_patchlevel() >= (8, 5, 12)
else (0xF0F0, 0x0000, 0xF0F0))
# #RRGGBB - extends each 8-bit hex value to be 16-bit.
assertApprox(rgb('#4a3c8c'), (0x4a4a, 0x3c3c, 0x8c8c))
# #RRRRGGGGBBBB
Expand Down
4 changes: 2 additions & 2 deletions Lib/tkinter/test/test_tkinter/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def test_configure_activestyle(self):
self.checkEnumParam(widget, 'activestyle',
'dotbox', 'none', 'underline')

test_justify = requires_tcl(8, 6, 5)(StandardOptionsTests.test_configure_justify)
test_configure_justify = requires_tcl(8, 6, 5)(StandardOptionsTests.test_configure_justify)

def test_configure_listvariable(self):
widget = self.create()
Expand Down Expand Up @@ -939,7 +939,7 @@ def test_configure_digits(self):

def test_configure_from(self):
widget = self.create()
conv = False if get_tk_patchlevel() >= (8, 6, 10) else float_round
conv = float if get_tk_patchlevel() >= (8, 6, 10) else float_round
self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=conv)

def test_configure_label(self):
Expand Down
7 changes: 4 additions & 3 deletions Lib/tkinter/test/test_ttk/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tkinter
from tkinter import ttk
from test.support import requires, gc_collect
from tkinter.test.support import AbstractTkTest, AbstractDefaultRootTest
from tkinter.test.support import AbstractTkTest, AbstractDefaultRootTest, get_tk_patchlevel

requires('gui')

Expand Down Expand Up @@ -57,8 +57,9 @@ def test_initialization(self):

# variable initialization/passing
passed_expected = (('0', 0), (0, 0), (10, 10),
(-1, -1), (sys.maxsize + 1, sys.maxsize + 1),
(2.5, 2), ('2.5', 2))
(-1, -1), (2.5, 2), ('2.5', 2))
if get_tk_patchlevel() >= (8, 5, 8):
passed_expected += ((sys.maxsize + 1, sys.maxsize + 1),)
for pair in passed_expected:
x = ttk.LabeledScale(self.root, from_=pair[0])
self.assertEqual(x.value, pair[1])
Expand Down
4 changes: 3 additions & 1 deletion Lib/tkinter/test/test_ttk/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tkinter import ttk
from test import support
from test.support import requires
from tkinter.test.support import AbstractTkTest
from tkinter.test.support import AbstractTkTest, get_tk_patchlevel

requires('gui')

Expand Down Expand Up @@ -170,6 +170,8 @@ def test_map_custom_copy(self):
newname = f'C.{name}'
self.assertEqual(style.map(newname), {})
style.map(newname, **default)
if theme == 'alt' and name == '.' and get_tk_patchlevel() < (8, 6, 1):
default['embossed'] = [('disabled', '1')]
self.assertEqual(style.map(newname), default)
for key, value in default.items():
self.assertEqual(style.map(newname, key), value)
Expand Down
13 changes: 12 additions & 1 deletion Lib/tkinter/test/test_ttk/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import sys

from test.test_ttk_textonly import MockTclObj
from tkinter.test.support import (AbstractTkTest, tcl_version, get_tk_patchlevel,
from tkinter.test.support import (AbstractTkTest,
tcl_version, get_tk_patchlevel, requires_tcl,
simulate_mouse_click, AbstractDefaultRootTest)
from tkinter.test.widget_tests import (add_standard_options, noconv,
AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests,
Expand Down Expand Up @@ -203,6 +204,12 @@ def test_configure_font(self):
self.checkParam(widget, 'font',
'-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')

if get_tk_patchlevel() < (8, 5, 11):
# different results on Tk < 8.5.11
@unittest.expectedFailure
def test_configure_borderwidth(self):
super().test_configure_borderwidth()


@add_standard_options(StandardTtkOptionsTests)
class ButtonTest(AbstractLabelTest, unittest.TestCase):
Expand Down Expand Up @@ -335,6 +342,7 @@ def test_identify(self):
self.assertRaises(tkinter.TclError, self.entry.identify, 5, None)
self.assertRaises(tkinter.TclError, self.entry.identify, 5, '')

@requires_tcl(8, 5, 10)
def test_validation_options(self):
success = []
test_invalid = lambda: success.append(True)
Expand Down Expand Up @@ -379,6 +387,7 @@ def validate(to_insert):
self.assertEqual(validation, [False, True])
self.assertEqual(self.entry.get(), 'a')

@requires_tcl(8, 5, 10)
def test_revalidation(self):
def validate(content):
for letter in content:
Expand Down Expand Up @@ -1099,6 +1108,7 @@ def test_traversal(self):
self.assertEqual(self.nb.select(), str(self.child2))


@requires_tcl(8, 5, 9)
@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
class SpinboxTest(EntryTest, unittest.TestCase):
OPTIONS = (
Expand Down Expand Up @@ -1800,6 +1810,7 @@ def test_tag_configure(self):
'blue')
self.assertIsInstance(self.tv.tag_configure('test'), dict)

@requires_tcl(8, 5, 9)
def test_tag_has(self):
item1 = self.tv.insert('', 'end', text='Item 1', tags=['tag1'])
item2 = self.tv.insert('', 'end', text='Item 2', tags=['tag2'])
Expand Down
3 changes: 3 additions & 0 deletions Lib/tkinter/test/widget_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def assertIsBoundingBox(self, bbox):
def test_keys(self):
widget = self.create()
keys = widget.keys()
if get_tk_patchlevel() < (8, 5, 11) and keys.count('takefocus') > 1:
# Tk bug: "takefocus" occurred twice in keys
keys.remove('takefocus')
self.assertEqual(sorted(keys), sorted(widget.configure()))
for k in keys:
widget[k]
Expand Down