Skip to content

Commit

Permalink
pythongh-107687: Use correct Tcl or Tk version in Tkinter tests
Browse files Browse the repository at this point in the history
In future Tcl and Tk versions can be desynchronized.
  • Loading branch information
serhiy-storchaka committed Aug 6, 2023
1 parent 71a7c96 commit fa06545
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 41 deletions.
13 changes: 2 additions & 11 deletions Lib/test/test_tcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@

tcl_version = tuple(map(int, _tkinter.TCL_VERSION.split('.')))

_tk_patchlevel = None
def get_tk_patchlevel():
global _tk_patchlevel
if _tk_patchlevel is None:
tcl = Tcl()
_tk_patchlevel = tcl.info_patchlevel()
return _tk_patchlevel


class TkinterTest(unittest.TestCase):

Expand Down Expand Up @@ -571,7 +563,6 @@ def test_splitlist(self):
(1, '2', (3.4,)) if self.wantobjects else
('1', '2', '3.4')),
]
tk_patchlevel = get_tk_patchlevel()
if not self.wantobjects:
expected = ('12', '\u20ac', '\xe2\x82\xac', '3.4')
else:
Expand All @@ -580,8 +571,8 @@ def test_splitlist(self):
(call('dict', 'create', 12, '\u20ac', b'\xe2\x82\xac', (3.4,)),
expected),
]
dbg_info = ('want objects? %s, Tcl version: %s, Tk patchlevel: %s'
% (self.wantobjects, tcl_version, tk_patchlevel))
dbg_info = ('want objects? %s, Tcl version: %s, Tcl patchlevel: %s'
% (self.wantobjects, tcl_version, self.interp.info_patchlevel()))
for arg, res in testcases:
self.assertEqual(splitlist(arg), res,
'arg=%a, %s' % (arg, dbg_info))
Expand Down
44 changes: 33 additions & 11 deletions Lib/test/test_tkinter/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,51 @@ def simulate_mouse_click(widget, x, y):

import _tkinter
tcl_version = tuple(map(int, _tkinter.TCL_VERSION.split('.')))
tk_version = tuple(map(int, _tkinter.TK_VERSION.split('.')))

def requires_tcl(*version):
if len(version) <= 2:
return unittest.skipUnless(tcl_version >= version,
'requires Tcl version >= ' + '.'.join(map(str, version)))
if len(version) <= 2 and tcl_version >= version:
return lambda test: test

def deco(test):
@functools.wraps(test)
def newtest(self):
if get_tk_patchlevel() < version:
root = getattr(self, 'root', None)
if get_tcl_patchlevel(root) < version:
self.skipTest('requires Tcl version >= ' +
'.'.join(map(str, version)))
test(self)
return newtest
return deco

_tk_patchlevel = None
def get_tk_patchlevel():
global _tk_patchlevel
if _tk_patchlevel is None:
tcl = tkinter.Tcl()
_tk_patchlevel = tcl.info_patchlevel()
return _tk_patchlevel
def requires_tk(*version):
if len(version) <= 2 and tk_version >= version:
return lambda test: test

def deco(test):
@functools.wraps(test)
def newtest(self):
root = getattr(self, 'root', None)
assert root is not None
if get_tk_patchlevel(root) < version:
self.skipTest('requires Tk version >= ' +
'.'.join(map(str, version)))
test(self)
return newtest
return deco

@functools.cache
def get_tcl_patchlevel(root=None):
if root is None:
root = tkinter.Tcl()
return root.info_patchlevel()

@functools.cache
def get_tk_patchlevel(root=None):
if root is None:
root = tkinter.Tcl()
root.withdraw()
return tkinter._parse_version(root.tk.globalgetvar('tcl_patchLevel'))

units = {
'c': 72 / 2.54, # centimeters
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_tkinter/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import tkinter
from test import support
from test.support import os_helper
from test.test_tkinter.support import AbstractTkTest, AbstractDefaultRootTest, requires_tcl
from test.test_tkinter.support import AbstractTkTest, AbstractDefaultRootTest, requires_tk

support.requires('gui')

Expand Down Expand Up @@ -213,11 +213,11 @@ def test_create_from_gif_file(self):
def test_create_from_gif_data(self):
self.check_create_from_data('gif')

@requires_tcl(8, 6)
@requires_tk(8, 6)
def test_create_from_png_file(self):
self.check_create_from_file('png')

@requires_tcl(8, 6)
@requires_tk(8, 6)
def test_create_from_png_data(self):
self.check_create_from_data('png')

Expand Down
16 changes: 8 additions & 8 deletions Lib/test/test_tkinter/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from test.support import requires

from test.test_tkinter.support import (requires_tcl,
from test.test_tkinter.support import (requires_tk,
get_tk_patchlevel, widget_eq,
AbstractDefaultRootTest)
from test.test_tkinter.widget_tests import (
Expand Down Expand Up @@ -613,7 +613,7 @@ def test_configure_inactiveselectbackground(self):
widget = self.create()
self.checkColorParam(widget, 'inactiveselectbackground')

@requires_tcl(8, 6)
@requires_tk(8, 6)
def test_configure_insertunfocussed(self):
widget = self.create()
self.checkEnumParam(widget, 'insertunfocussed',
Expand Down Expand Up @@ -924,7 +924,7 @@ def test_coords(self):
for i in range(4):
self.assertIsInstance(coords[i], float)

@requires_tcl(8, 6)
@requires_tk(8, 6)
def test_moveto(self):
widget = self.create()
i1 = widget.create_rectangle(1, 1, 20, 20, tags='group')
Expand Down Expand Up @@ -969,7 +969,7 @@ def test_configure_activestyle(self):
self.checkEnumParam(widget, 'activestyle',
'dotbox', 'none', 'underline')

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

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

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

def test_configure_label(self):
Expand Down Expand Up @@ -1235,19 +1235,19 @@ def test_configure_opaqueresize(self):
widget = self.create()
self.checkBooleanParam(widget, 'opaqueresize')

@requires_tcl(8, 6, 5)
@requires_tk(8, 6, 5)
def test_configure_proxybackground(self):
widget = self.create()
self.checkColorParam(widget, 'proxybackground')

@requires_tcl(8, 6, 5)
@requires_tk(8, 6, 5)
def test_configure_proxyborderwidth(self):
widget = self.create()
self.checkPixelsParam(widget, 'proxyborderwidth',
0, 1.3, 2.9, 6, -2, '10p',
conv=False)

@requires_tcl(8, 6, 5)
@requires_tk(8, 6, 5)
def test_configure_proxyrelief(self):
widget = self.create()
self.checkReliefParam(widget, 'proxyrelief')
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_tkinter/widget_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Common tests for test_tkinter/test_widgets.py and test_ttk/test_widgets.py

import tkinter
from test.test_tkinter.support import (AbstractTkTest, tcl_version,
from test.test_tkinter.support import (AbstractTkTest, tk_version,
pixels_conv, tcl_obj_eq)
import test.support

Expand All @@ -22,7 +22,7 @@ def scaling(self):
return self._scaling

def _str(self, value):
if not self._stringify and self.wantobjects and tcl_version >= (8, 6):
if not self._stringify and self.wantobjects and tk_version >= (8, 6):
return value
if isinstance(value, tuple):
return ' '.join(map(self._str, value))
Expand Down Expand Up @@ -156,7 +156,7 @@ def checkReliefParam(self, widget, name):
'flat', 'groove', 'raised', 'ridge', 'solid', 'sunken')
errmsg='bad relief "spam": must be '\
'flat, groove, raised, ridge, solid, or sunken'
if tcl_version < (8, 6):
if tk_version < (8, 6):
errmsg = None
self.checkInvalidParam(widget, name, 'spam',
errmsg=errmsg)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ttk/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ 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):
if theme == 'alt' and name == '.' and get_tk_patchlevel(self.root) < (8, 6, 1):
default['embossed'] = [('disabled', '1')]
self.assertEqual(style.map(newname), default)
for key, value in default.items():
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_ttk/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys

from test.test_ttk_textonly import MockTclObj
from test.test_tkinter.support import (AbstractTkTest, tcl_version, get_tk_patchlevel,
from test.test_tkinter.support import (AbstractTkTest, tk_version, get_tk_patchlevel,
simulate_mouse_click, AbstractDefaultRootTest)
from test.test_tkinter.widget_tests import (add_standard_options,
AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests)
Expand All @@ -19,7 +19,7 @@ def test_configure_class(self):
widget = self.create()
self.assertEqual(widget['class'], '')
errmsg='attempt to change read-only option'
if get_tk_patchlevel() < (8, 6, 0, 'beta', 3):
if get_tk_patchlevel(self.root) < (8, 6, 0, 'beta', 3):
errmsg='Attempt to change read-only option'
self.checkInvalidParam(widget, 'class', 'Foo', errmsg=errmsg)
widget2 = self.create(class_='Foo')
Expand Down Expand Up @@ -560,7 +560,7 @@ def test_configure_orient(self):
widget = self.create()
self.assertEqual(str(widget['orient']), 'vertical')
errmsg='attempt to change read-only option'
if get_tk_patchlevel() < (8, 6, 0, 'beta', 3):
if get_tk_patchlevel(self.root) < (8, 6, 0, 'beta', 3):
errmsg='Attempt to change read-only option'
self.checkInvalidParam(widget, 'orient', 'horizontal',
errmsg=errmsg)
Expand Down Expand Up @@ -1526,7 +1526,7 @@ def test_heading(self):

def test_heading_callback(self):
def simulate_heading_click(x, y):
if tcl_version >= (8, 6):
if tk_version >= (8, 6):
self.assertEqual(self.tv.identify_column(x), '#0')
self.assertEqual(self.tv.identify_region(x, y), 'heading')
simulate_mouse_click(self.tv, x, y)
Expand Down

0 comments on commit fa06545

Please sign in to comment.