bundle2: move the fromheader closure into the class itself
authorPierre-Yves David <pierre-yves.david@fb.com>
Fri, 11 Apr 2014 15:46:09 -0400
changeset 21015 14dd49260246
parent 21014 a6246bba7b9e
child 21016 b477afb1c81e
bundle2: move the fromheader closure into the class itself The class is now directly related to this header data. We can sanely move it on the class. I do not like closures very much...
mercurial/bundle2.py
--- a/mercurial/bundle2.py	Fri Apr 11 15:43:16 2014 -0400
+++ b/mercurial/bundle2.py	Fri Apr 11 15:46:09 2014 -0400
@@ -543,6 +543,7 @@
         self.ui = ui
         # unbundle state attr
         self._headerdata = header
+        self._headeroffset = 0
         # part data
         self.id = None
         self.type = None
@@ -551,26 +552,25 @@
         self.data = None
         self._readdata()
 
+    def _fromheader(self, size):
+        """return the next <size> byte from the header"""
+        offset = self._headeroffset
+        data = self._headerdata[offset:(offset + size)]
+        self._headeroffset += size
+        return data
+
     def _readdata(self):
         """read the header and setup the object"""
         # some utility to help reading from the header block
-        headerblock = self._headerdata
-        self._offset = 0 # layer violation to have something easy to understand
-        def fromheader(size):
-            """return the next <size> byte from the header"""
-            offset = self._offset
-            data = headerblock[offset:(offset + size)]
-            self._offset = offset + size
-            return data
         def unpackheader(format):
             """read given format from header
 
             This automatically compute the size of the format to read."""
-            data = fromheader(struct.calcsize(format))
+            data = self._fromheader(struct.calcsize(format))
             return _unpack(format, data)
 
         typesize = unpackheader(_fparttypesize)[0]
-        self.type = fromheader(typesize)
+        self.type = self._fromheader(typesize)
         self.ui.debug('part type: "%s"\n' % self.type)
         self.id = unpackheader(_fpartid)[0]
         self.ui.debug('part id: "%s"\n' % self.id)
@@ -588,11 +588,10 @@
         # retrive param value
         manparams = []
         for key, value in mansizes:
-            manparams.append((fromheader(key), fromheader(value)))
+            manparams.append((self._fromheader(key), self._fromheader(value)))
         advparams = []
         for key, value in advsizes:
-            advparams.append((fromheader(key), fromheader(value)))
-        del self._offset # clean up layer, nobody saw anything.
+            advparams.append((self._fromheader(key), self._fromheader(value)))
         self.mandatoryparams = manparams
         self.advisoryparams  = advparams
         ## part payload