@contextlib.contextmanager
def savefile_open(filename, binary=False, encoding='utf-8'):
 f = QSaveFile(filename)
 cancelled = False
 try:
  open_ok = f.open(QIODevice.WriteOnly)
  if (not open_ok):
   raise QtOSError(f)
  if binary:
   new_f = PyQIODevice(f)
  else:
   new_f = io.TextIOWrapper(PyQIODevice(f), encoding=encoding)
  (yield new_f)
  new_f.flush()
 except:
  f.cancelWriting()
  cancelled = True
  raise
 finally:
  commit_ok = f.commit()
  if ((not commit_ok) and (not cancelled)):
   raise QtOSError(f, msg='Commit failed!')

@ contextlib . contextmanager def savefile_open ( filename , binary = False , encoding = 'utf-8' ) : f = QSaveFile ( filename ) cancelled = False try : open_ok = f . open ( QIODevice . WriteOnly ) if ( not open_ok ) : raise QtOSError ( f ) if binary : new_f = PyQIODevice ( f ) else : new_f = io . TextIOWrapper ( PyQIODevice ( f ) , encoding = encoding ) ( yield new_f ) new_f . flush ( ) except : f . cancelWriting ( ) cancelled = True raise finally : commit_ok = f . commit ( ) if ( ( not commit_ok ) and ( not cancelled ) ) : raise QtOSError ( f , msg = 'Commit failed!' )
context manager to easily use a qsavefile .