diff --git a/Lib/tkinter/filedialog.py b/Lib/tkinter/filedialog.py index e2eff98e601c07..bf7e406d1dc464 100644 --- a/Lib/tkinter/filedialog.py +++ b/Lib/tkinter/filedialog.py @@ -311,6 +311,10 @@ def _fixoptions(self): pass def _fixresult(self, widget, result): + # gh-103878: empty string from Tcl may have non-string type + if result in ((), b''): + result = '' + if result: # keep directory and filename until next time # convert Tcl path objects to strings @@ -335,6 +339,10 @@ class Open(_Dialog): command = "tk_getOpenFile" def _fixresult(self, widget, result): + # gh-103878: empty string from Tcl may have non-string type + if result in ((), b''): + result = '' + if isinstance(result, tuple): # multiple results: result = tuple([getattr(r, "string", r) for r in result]) @@ -362,6 +370,10 @@ class Directory(commondialog.Dialog): command = "tk_chooseDirectory" def _fixresult(self, widget, result): + # gh-103878: empty string from Tcl may have non-string type + if result in ((), b''): + result = '' + if result: # convert Tcl path objects to strings try: diff --git a/Misc/NEWS.d/next/Library/2023-10-23-01-00-00.gh-issue-103878.molDay.rst b/Misc/NEWS.d/next/Library/2023-10-23-01-00-00.gh-issue-103878.molDay.rst new file mode 100644 index 00000000000000..bee021d680016d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-23-01-00-00.gh-issue-103878.molDay.rst @@ -0,0 +1,2 @@ +Ensure Tkinter file dialogs return ``""`` rather than ``()`` or ``b""`` +when canceled.