Skip to content

gh-160406: Keep the backup and create output exclusively in fileinput - #160407

Merged
gpshead merged 1 commit into
python:mainfrom
gpshead:scanfix-fileinput-3tzs4h87
Oct 23, 2026
Merged

gpshead merged 1 commit into
python:mainfrom
gpshead:scanfix-fileinput-3tzs4h87

Conversation

@gpshead

@gpshead gpshead commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

FileInput.nextfile() no longer deletes the backup file when no output file was created, since the backup is then the only copy of the data (for example when the input file cannot be opened for reading), and an existing backup is now replaced with os.replace() instead of being unlinked first, so running the same command again does not delete it either. The output file is now created with O_EXCL ('x' mode in the fstat() failure fallback) and its mode is set through the file descriptor where os.chmod() supports that, so a file or symlink that appears under the input file's name after the rename raises FileExistsError instead of being written through and chmodded.

The FileExistsError is a behaviour change, documented with a versionchanged. After a failure the data is left in <name>.bak rather than renamed back, because renaming back could replace whatever now has the name.

Fixes #160406

ANT-2026-3TZS4H87

…einput

In-place filtering deleted the backup file, the only copy of the data,
when the output file could not be created.  An existing backup file is
now only replaced once there is a file to back up, so trying again does
not delete it either.  The output file is now created with O_EXCL and
its mode is set through the file descriptor.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35340322 | 📁 Comparing e6ab761 against main (4af1265)

  🔍 Preview build  

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

@gpshead
gpshead enabled auto-merge (squash) October 23, 2026 00:29
@gpshead
gpshead merged commit e6190ac into python:main Oct 23, 2026
57 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Sprint Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fileinput in-place filtering deletes the file when it cannot be opened

1 participant