Skip to content

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

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:filedialog-cancel-normalize
Jun 29, 2026
Merged

gh-103878: Return a consistent empty value from cancelled file dialogs#152435
serhiy-storchaka merged 3 commits 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: Christopher Chavez <chrischavez@gmx.us>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
serhiy-storchaka and others added 2 commits June 29, 2026 22:59
askopenfiles() returned a list of open files normally but an empty tuple
when cancelled.  Make it always return a list, empty when cancelled, as its
docstring already describes.

Update the file dialog documentation accordingly, now that the empty value
returned on cancellation is consistent for each function.

Co-authored-by: Christopher Chavez <chrischavez@gmx.us>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33362666 | 📁 Comparing 6694f02 against main (0a29d84)

  🔍 Preview build  

2 files changed
± library/dialog.html
± whatsnew/changelog.html

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) June 29, 2026 20:29
@serhiy-storchaka serhiy-storchaka merged commit edcc07d into python:main Jun 29, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant