This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add FileInput.rollback() for in-place filters
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: samwyse
Priority: normal Keywords:

Created on 2021-08-10 22:22 by samwyse, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg399361 - (view) Author: Samwyse (samwyse) * Date: 2021-08-10 22:22
Sometimes bad things happen when processing an in-place filter, leaving an empty or incomplete input file and a backup file that needs to recovered. The FileInput class has all the information needed to do this, but it is in private instance variables.  A .rollback() method could close the current file and rename the backup file to its original name.  For example:

  for line in fileinput.input(inplace=True):
    try:
      ...
    except SomeError:
      fileinput.rollback(close=False)  # continue with next file

A simplistic implementation could be:

  def rollback(self, close=True):
    if self._backupfilename:
      os.rename(self._backupfilename, self.filename)
      self._backupfilename = None
    if close:
      self.close()
    else:
      self.nextfile()
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89045
2021-08-10 22:29:22samwysesettitle: add .rollback() for in-place filters -> add FileInput.rollback() for in-place filters
2021-08-10 22:22:25samwysecreate