[PATCH 2 of 4] statichttprepo: implement __enter__ and __exit__ on httprangeheader

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


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1451777581 28800
#      Sat Jan 02 15:33:01 2016 -0800
# Node ID 55459e98812bcec222fabe63bb60b5e5c922d3c1
# Parent  8a33bbee1ed445f61c825a789d7c5b9d93295eef
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.

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:


More information about the Mercurial-devel mailing list