New submission from Samwyse <samw...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue44882>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to