瀏覽代碼

Warn when _GitFile is not closed

Jelmer Vernooij 2 年之前
父節點
當前提交
285836e91a
共有 1 個文件被更改,包括 6 次插入0 次删除
  1. 6 0
      dulwich/file.py

+ 6 - 0
dulwich/file.py

@@ -22,6 +22,7 @@
 
 import os
 import sys
+import warnings
 
 
 def ensure_dir_exists(dirname):
@@ -202,6 +203,11 @@ class _GitFile:
         finally:
             self.abort()
 
+    def __del__(self):
+        if not getattr(self, '_closed', True):
+            warnings.warn('unclosed %r' % self, ResourceWarning, stacklevel=2)
+            self.abort()
+
     def __enter__(self):
         return self