[PATCH] largefiles: replace usage of tempfile.NamedTemporaryFile with tempfile.mkstemp

Hao Lian hao at fogcreek.com
Thu Oct 20 16:31:50 CDT 2011


# HG changeset patch
# User Hao Lian <hao at fogcreek.com>
# Date 1319145899 14400
# Branch stable
# Node ID 569feddf9101067ced5a3375a9a9d844ee5dc64a
# Parent  3eb1a90ea40941ad7a920daad665e27ffdb6f291
largefiles: replace usage of tempfile.NamedTemporaryFile with tempfile.mkstemp

This is consistent with the rest of Mercurial's code, mirroring the try-finally-unlink structure elsewhere. Furthermore, it fixes the case where largefiles throws an IOError on Windows when the temporary file is opened a second time by copytocacheabsolute.

diff --git a/hgext/largefiles/proto.py b/hgext/largefiles/proto.py
--- a/hgext/largefiles/proto.py
+++ b/hgext/largefiles/proto.py
@@ -19,23 +19,26 @@
 def putlfile(repo, proto, sha):
     '''Put a largefile into a repository's local cache and into the
     system cache.'''
-    f = None
+
     proto.redirect()
+
+    fd, tmpname = tempfile.mkstemp(prefix='hg-putlfile-')
+    tmpfp = os.fdopen(fd, 'wb+')
     try:
-        try:
-            f = tempfile.NamedTemporaryFile(mode='wb+', prefix='hg-putlfile-')
-            proto.getfile(f)
-            f.seek(0)
-            if sha != lfutil.hexsha1(f):
-                return wireproto.pushres(1)
-            lfutil.copytocacheabsolute(repo, f.name, sha)
-        except IOError:
-            repo.ui.warn(
-                _('error: could not put received data into largefile store'))
+        proto.getfile(tmpfp)
+        tmpfp.seek(0)
+        if sha != lfutil.hexsha1(tmpfp):
             return wireproto.pushres(1)
+        tmpfp.close()
+
+        lfutil.copytocacheabsolute(repo, tmpname, sha)
+    except IOError:
+        repo.ui.warn(
+            _('error: could not put received data into largefile store'))
+        return wireproto.pushres(1)
     finally:
-        if f:
-            f.close()
+        tmpfp.close()
+        os.unlink(tmpname)
 
     return wireproto.pushres(0)
 


More information about the Mercurial-devel mailing list