Skip to content
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

Accessing a tkinter object's string representation converts the object to a string on Windows #101830

Open
daniilS opened this issue Feb 11, 2023 · 0 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@daniilS
Copy link

daniilS commented Feb 11, 2023

Introduced in #16545
Affects Python 3.7+ on Windows only


The FromObj function in _tkinter.c attempt to convert a Tcl_Obj to an equivalent Python object if possible, and otherwise returns a _tkinter.Tcl_Obj with the typename attribute set to the original object's type.

However, on Windows, accessing the resulting object's string representation calls Tcl_GetUnicodeFromObj, which converts the Tcl_Obj to a String. This side effect isn't mentioned in the Tcl documentation, but is in the Tcl source code. As a result, retrieving the same tk property afterwards will return a Python string instead.

Minimal example:

import tkinter as tk

root = tk.Tk()
print(type(root.cget("padx")))
_ = str(root.cget("padx"))  # should really not cause any side effects
print(type(root.cget("padx")))

# Windows:
# <class '_tkinter.Tcl_Obj'>
# <class 'str'>
# Other platforms:
# <class '_tkinter.Tcl_Obj'>
# <class '_tkinter.Tcl_Obj'>

Possible solutions: unicodeFromTclObj should copy the object before passing calling Tcl_GetUnicodeFromObj, or handle Unicode without it like on other platforms.

@daniilS daniilS added the type-bug An unexpected behavior, bug, or error label Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant