Patchwork lfs: ensure that the return of urlopener.open() is closed

login
register
mail settings
Submitter Matt Harbison
Date Nov. 17, 2018, 4:58 a.m.
Message ID <ed9409b7db5177897332.1542430738@Envy>
Download mbox | patch
Permalink /patch/36631/
State Accepted
Headers show

Comments

Matt Harbison - Nov. 17, 2018, 4:58 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1542338918 18000
#      Thu Nov 15 22:28:38 2018 -0500
# Node ID ed9409b7db51778973328e933e657e4fdf73f573
# Parent  93e5d18251d61b8abb2f6c6c9828a1c2f4f6fb57
lfs: ensure that the return of urlopener.open() is closed

No problem observed, just an oversight noticed while reading documentation.
Yuya Nishihara - Nov. 18, 2018, 1:53 a.m.
On Fri, 16 Nov 2018 23:58:58 -0500, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1542338918 18000
> #      Thu Nov 15 22:28:38 2018 -0500
> # Node ID ed9409b7db51778973328e933e657e4fdf73f573
> # Parent  93e5d18251d61b8abb2f6c6c9828a1c2f4f6fb57
> lfs: ensure that the return of urlopener.open() is closed

Queued, thanks.

Patch

diff --git a/hgext/lfs/blobstore.py b/hgext/lfs/blobstore.py
--- a/hgext/lfs/blobstore.py
+++ b/hgext/lfs/blobstore.py
@@ -7,6 +7,7 @@ 
 
 from __future__ import absolute_import
 
+import contextlib
 import errno
 import hashlib
 import json
@@ -297,8 +298,8 @@  class _gitlfsremote(object):
         batchreq.add_header('Accept', 'application/vnd.git-lfs+json')
         batchreq.add_header('Content-Type', 'application/vnd.git-lfs+json')
         try:
-            rsp = self.urlopener.open(batchreq)
-            rawjson = rsp.read()
+            with contextlib.closing(self.urlopener.open(batchreq)) as rsp:
+                rawjson = rsp.read()
         except util.urlerr.httperror as ex:
             hints = {
                 400: _('check that lfs serving is enabled on %s and "%s" is '
@@ -419,25 +420,27 @@  class _gitlfsremote(object):
 
         response = b''
         try:
-            req = self.urlopener.open(request)
-
-            if self.ui.debugflag:
-                self.ui.debug('Status: %d\n' % req.status)
-                # lfs-test-server and hg serve return headers in different order
-                self.ui.debug('%s\n'
-                              % '\n'.join(sorted(str(req.info()).splitlines())))
+            with contextlib.closing(self.urlopener.open(request)) as req:
+                ui = self.ui  # Shorten debug lines
+                if self.ui.debugflag:
+                    ui.debug('Status: %d\n' % req.status)
+                    # lfs-test-server and hg serve return headers in different
+                    # order
+                    ui.debug('%s\n'
+                             % '\n'.join(sorted(str(req.info()).splitlines())))
 
-            if action == 'download':
-                # If downloading blobs, store downloaded data to local blobstore
-                localstore.download(oid, req)
-            else:
-                while True:
-                    data = req.read(1048576)
-                    if not data:
-                        break
-                    response += data
-                if response:
-                    self.ui.debug('lfs %s response: %s' % (action, response))
+                if action == 'download':
+                    # If downloading blobs, store downloaded data to local
+                    # blobstore
+                    localstore.download(oid, req)
+                else:
+                    while True:
+                        data = req.read(1048576)
+                        if not data:
+                            break
+                        response += data
+                    if response:
+                        ui.debug('lfs %s response: %s' % (action, response))
         except util.urlerr.httperror as ex:
             if self.ui.debugflag:
                 self.ui.debug('%s: %s\n' % (oid, ex.read()))