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.

Author samwyse
Recipients samwyse
Date 2021-08-10.22:22:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1628634145.27.0.557852545267.issue44882@roundup.psfhosted.org>
In-reply-to
Content
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
2021-08-10 22:22:25samwysesetrecipients: + samwyse
2021-08-10 22:22:25samwysesetmessageid: <1628634145.27.0.557852545267.issue44882@roundup.psfhosted.org>
2021-08-10 22:22:25samwyselinkissue44882 messages
2021-08-10 22:22:25samwysecreate