]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
SF bug #476138: tempfile behavior across platforms
authorTim Peters <tim.peters@gmail.com>
Mon, 29 Oct 2001 21:46:08 +0000 (21:46 +0000)
committerTim Peters <tim.peters@gmail.com>
Mon, 29 Oct 2001 21:46:08 +0000 (21:46 +0000)
Ensure that a tempfile can be closed any number of times without error.
This wasn't true on Windows.

Lib/tempfile.py
Lib/test/test_tempfile.py [new file with mode: 0644]

index f1d53db1c49793cd3ee65db6b6bfa95248b85a13..96245d8c396f9d1470f2b0cf1d546276914cd81f 100644 (file)
@@ -131,14 +131,16 @@ class TemporaryFileWrapper:
     def __init__(self, file, path):
         self.file = file
         self.path = path
+        self.close_called = 0
 
     def close(self):
-        self.file.close()
-        os.unlink(self.path)
+        if not self.close_called:
+            self.close_called = 1
+            self.file.close()
+            os.unlink(self.path)
 
     def __del__(self):
-        try: self.close()
-        except: pass
+        self.close()
 
     def __getattr__(self, name):
         file = self.__dict__['file']
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
new file mode 100644 (file)
index 0000000..286e79c
--- /dev/null
@@ -0,0 +1,10 @@
+# SF bug #476138: tempfile behavior across platforms
+# Ensure that a temp file can be closed any number of times without error.
+
+import tempfile
+
+f = tempfile.TemporaryFile("w+b")
+f.write('abc\n')
+f.close()
+f.close()
+f.close()