Patchwork [3,of,5,V2] statichttprepo: implement __enter__ and __exit__ on httprangeheader

login
register
mail settings
Submitter Gregory Szorc
Date Jan. 3, 2016, 12:45 a.m.
Message ID <bb932201352783ce4863.1451781931@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/12497/
State Accepted
Delegated to: Matt Mackall
Headers show

Comments

Gregory Szorc - Jan. 3, 2016, 12:45 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1451777581 28800
#      Sat Jan 02 15:33:01 2016 -0800
# Node ID bb932201352783ce4863e49f2c5ed43734505d34
# Parent  3481c6e7a3ee0816aa6fc78a8a0c9c33d3ce4a59
statichttprepo: implement __enter__ and __exit__ on httprangeheader

httprangeheader behaves like a file object. Implement __enter__ and
__exit__ so it can be used as a context manager, just like file objects.

Patch

diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py
--- a/mercurial/statichttprepo.py
+++ b/mercurial/statichttprepo.py
@@ -30,16 +30,23 @@  from . import (
 
 class httprangereader(object):
     def __init__(self, url, opener):
         # we assume opener has HTTPRangeHandler
         self.url = url
         self.pos = 0
         self.opener = opener
         self.name = url
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.close()
+
     def seek(self, pos):
         self.pos = pos
     def read(self, bytes=None):
         req = urllib2.Request(self.url)
         end = ''
         if bytes:
             end = self.pos + bytes - 1
         if self.pos or end: