[PATCH 4 of 4] streamclone: use context manager for writing files

Gregory Szorc gregory.szorc at gmail.com
Sat Jan 2 17:38:48 CST 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1451776198 28800
#      Sat Jan 02 15:09:58 2016 -0800
# Node ID 7f2365c369b61d6a234d0089b9845047b3dc3cc5
# Parent  7626e49bc8d37409c6ef173eff3ddc1dc925413d
streamclone: use context manager for writing files

These are the file writes that have the most to gain from background
I/O. Plug in a context manager so I can design the background I/O
mechanism with context managers in mind.

diff --git a/mercurial/streamclone.py b/mercurial/streamclone.py
--- a/mercurial/streamclone.py
+++ b/mercurial/streamclone.py
@@ -314,22 +314,22 @@ def consumev1(repo, fp, filecount, bytec
                     size = int(size)
                 except (ValueError, TypeError):
                     raise error.ResponseError(
                         _('unexpected response from remote server:'), l)
                 if repo.ui.debugflag:
                     repo.ui.debug('adding %s (%s)\n' %
                                   (name, util.bytecount(size)))
                 # for backwards compat, name was partially encoded
-                ofp = repo.svfs(store.decodedir(name), 'w')
-                for chunk in util.filechunkiter(fp, limit=size):
-                    handled_bytes += len(chunk)
-                    repo.ui.progress(_('clone'), handled_bytes, total=bytecount)
-                    ofp.write(chunk)
-                ofp.close()
+                with repo.svfs(store.decodedir(name), 'w') as ofp:
+                    for chunk in util.filechunkiter(fp, limit=size):
+                        handled_bytes += len(chunk)
+                        repo.ui.progress(_('clone'), handled_bytes,
+                                         total=bytecount)
+                        ofp.write(chunk)
             tr.close()
         finally:
             tr.release()
 
         # Writing straight to files circumvented the inmemory caches
         repo.invalidate()
 
         elapsed = time.time() - start


More information about the Mercurial-devel mailing list