[New-bugs-announce] [issue44882] add .rollback() for in-place filters

Samwyse report at bugs.python.org
Tue Aug 10 18:22:25 EDT 2021


New submission from Samwyse <samwyse at gmail.com>:

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()

----------
components: Library (Lib)
messages: 399361
nosy: samwyse
priority: normal
severity: normal
status: open
title: add .rollback() for in-place filters
type: enhancement
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44882>
_______________________________________


More information about the New-bugs-announce mailing list