def METHOD_NAME(self):
    changes = 0
    changed_file = None
    changed_filename = self.filename + '.new'
    try:
        lines = []
        backup_filename = self.filename + '.bak'
        # noinspection PyUnusedLocal
        stat = None
        with open(self.filename, 'r') as orig:
            stat = os.fstat(orig.fileno())
            for line in orig:
                changed_line = line
                for edit in self.edits:
                    remaining_count = 0
                    if edit.count != 0:
                        remaining_count = edit.count - edit.subs
                        if remaining_count < 0:
                            raise Exception("Made too many edits")
                        elif remaining_count == 0:
                            continue
                    changed_line, subs = edit.pattern.subn(
                        edit.sub, line, remaining_count)
                    if changed_line != line:
                        if changed_file is None:
                            logging.debug("Editing file %s" % self.filename)
                        logging.debug("  - %s" % line[:-1])
                        logging.debug("  + %s" % changed_line[:-1])
                        changes += subs
                        edit.subs += subs
                if changes == 0:                                   # buffer until we find a change
                    lines.append(changed_line)
                elif changed_file is None:                         # found first change, flush buffer
                    changed_file = open(changed_filename, 'w')
                    if hasattr(os, 'fchmod'):
                        os.fchmod(changed_file.fileno(),           # can cause OSError which aborts
                                  stat.st_mode)
                    if hasattr(os, 'fchown'):
                        os.fchown(changed_file.fileno(),           # can cause OSError which aborts
                                  stat.st_uid, stat.st_gid)
                    changed_file.writelines(lines)
                    changed_file.write(changed_line)
                    del lines                                      # reclaim buffer memory
                else:                                              # already flushed, just write
                    changed_file.write(changed_line)
        if changes == 0:
            logging.info("No edits need for file %s" %
                         self.filename)
        else:
            changed_file.close()
            changed_file = None
            if os.path.exists(backup_filename):                    # back up the original
                os.unlink(backup_filename)
            shutil.copy(self.filename, backup_filename)
            os.rename(changed_filename, self.filename)             # the swap
            logging.info("Edited file %s (%d changes)" %
                         (self.filename, changes))
    finally:
        if changed_file is not None:                               # failed, clean up
            changed_file.close()
            os.unlink(changed_filename)
    return changes