Skip to content

gh-103878: Return a consistent empty value from cancelled file dialogs#152435

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:filedialog-cancel-normalize
Open

gh-103878: Return a consistent empty value from cancelled file dialogs#152435
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:filedialog-cancel-normalize

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

When a native file dialog is cancelled, Tcl reports the empty result as '', an empty tuple, or b'' depending on the platform and Tk version (the value comes back with whatever internal type the empty Tcl object happened to have). As a result askdirectory() returns () on some platforms and '' on others, and askopenfilename()/asksaveasfilename() are similarly inconsistent.

This normalizes the cancelled result per the value the function normally returns:

  • the filename functions (askopenfilename(), asksaveasfilename(), askdirectory()) always return '';
  • askopenfilenames() always returns an empty tuple.

askopenfile()/asksaveasfile() keep returning None, since an empty value of a file object's "type" does not exist.

Open._fixresult() now distinguishes single from multiple results by the multiple option rather than by the result type, so an empty single result (an empty tuple from Tcl) is no longer mistaken for a multiple selection. This avoids the issue with PR #111195, which collapsed the legitimate empty tuple returned by askopenfilenames().

Since this changes behavior, it targets the main branch only. The documentation half of the issue was handled separately in #152429.

…dialogs

On cancellation Tcl may report the empty result as '', () or b'' depending
on the platform and Tk version. Normalize it so that askopenfilename(),
asksaveasfilename() and askdirectory() always return '' and
askopenfilenames() always returns ().

Open._fixresult() now distinguishes single from multiple results by the
'multiple' option rather than the result type.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant