Commits

Yuya Nishihara committed 1c51bad

qscilib: close QFile explicitly in readFile/writeFile

It should be done by the destructor of QFile triggered by ref-couting, but we
shouldn't rely on it because Python doesn't have auto variables.

Note that this won't be the cause of the disappearance of hgrc content. Even
if QFile resource was leaked, win32.unlink() would fail, so the original hgrc
should be unchanged.

windows.rename(src, dst)
win32.unlink(dst) # raises WindowsError
os.rename(src, dst)

  • Participants
  • Parent commits ef778af
  • Branches stable

Comments (0)

Files changed (1)

tortoisehg/hgqt/qscilib.py

                             _('Could not open the specified file for reading.'),
                             f.errorString(), parent=editor)
         return False
-    earlybytes = f.read(4096)
-    if '\0' in earlybytes:
-        qtlib.WarningMsgBox(_('Unable to read file'),
-                            _('This appears to be a binary file.'),
-                            parent=editor)
-        return False
+    try:
+        earlybytes = f.read(4096)
+        if '\0' in earlybytes:
+            qtlib.WarningMsgBox(_('Unable to read file'),
+                                _('This appears to be a binary file.'),
+                                parent=editor)
+            return False
 
-    f.seek(0)
-    data = str(f.readAll())
-    if f.error():
-        qtlib.WarningMsgBox(_('Unable to read file'),
-                            _('An error occurred while reading the file.'),
-                            f.errorString(), parent=editor)
-        return False
+        f.seek(0)
+        data = str(f.readAll())
+        if f.error():
+            qtlib.WarningMsgBox(_('Unable to read file'),
+                                _('An error occurred while reading the file.'),
+                                f.errorString(), parent=editor)
+            return False
+    finally:
+        f.close()
+
     if encoding:
         try:
             text = data.decode(encoding)
                             _('Could not open the specified file for writing.'),
                             f.errorString(), parent=editor)
         return False
-    if f.write(data) < 0:
-        qtlib.WarningMsgBox(_('Unable to write file'),
-                            _('An error occurred while writing the file.'),
-                            f.errorString(), parent=editor)
-        return False
+    try:
+        if f.write(data) < 0:
+            qtlib.WarningMsgBox(_('Unable to write file'),
+                                _('An error occurred while writing the file.'),
+                                f.errorString(), parent=editor)
+            return False
+    finally:
+        f.close()
     return True
 
 def fileEditor(filename, **opts):