gh-156961: Fix tkinter.font.Font for a font name returned as a Tcl object by serhiy-storchaka · Pull Request #157028 · python/cpython · GitHub
Skip to content
Open
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
40 changes: 40 additions & 0 deletions Lib/test/test_tkinter/test_font.py
21 changes: 14 additions & 7 deletions Lib/tkinter/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def __init__(self, root=None, font=None, name=None, exists=False,
if exists:
self.name = name
# confirm font exists
if self.name not in tk.splitlist(tk.call("font", "names")):
name = getattr(name, 'string', name) # can be a Tcl object
if name not in tk.splitlist(tk.call("font", "names")):
raise tkinter._tkinter.TclError(
"named font %s does not already exist" % (self.name,))
# if font config info supplied, apply it
Expand All @@ -123,11 +124,11 @@ def __init__(self, root=None, font=None, name=None, exists=False,
self._call = tk.call

def __str__(self):
# A wrapped description is a list or tuple, not a string; format it as
# a Tcl word so it can be used as an option value (as ttk does).
if isinstance(self.name, str):
return self.name
return tkinter._join(self.name)
# A wrapped description can be a list or tuple; format it as a Tcl
# word so it can be used as an option value (as ttk does).
if isinstance(self.name, (list, tuple)):
return tkinter._join(self.name)
return str(self.name)

def __repr__(self):
return f"<{self.__class__.__module__}.{self.__class__.__qualname__}" \
Expand All @@ -136,7 +137,13 @@ def __repr__(self):
def __eq__(self, other):
if not isinstance(other, Font):
return NotImplemented
return self.name == other.name and self._tk == other._tk
name = self.name
other_name = other.name
if type(name) is not type(other_name):
# A Tcl object does not compare equal to a string.
name = getattr(name, 'string', name)
other_name = getattr(other_name, 'string', other_name)
return name == other_name and self._tk == other._tk

def __getitem__(self, key):
return self.cget(key)
Expand Down
Loading