Patchwork [3,of,8] bundle2: move the fromheader closure on the class itself

login
register
mail settings
Submitter Pierre-Yves David
Date April 12, 2014, 10:08 p.m.
Message ID <e214c047eeb7227e9875.1397340522@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/4305/
State Accepted
Headers show

Comments

Pierre-Yves David - April 12, 2014, 10:08 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1397245569 14400
#      Fri Apr 11 15:46:09 2014 -0400
# Node ID e214c047eeb7227e987558dc97961fac08942c18
# Parent  5259d2fac78125ee7e5417566c7ff04f8efcc6fc
bundle2: move the fromheader closure on the class itself

The class is now directly related to those header data. We can sanely move it on
the class.

I do not like closure very much...

Patch

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -541,38 +541,38 @@  class unbundlepart(unpackermixing):
     def __init__(self, ui, header, fp):
         super(unbundlepart, self).__init__(fp)
         self.ui = ui
         # unbundle state attr
         self._headerdata = header
+        self._headeroffset = 0
         # part data
         self.id = None
         self.type = None
         self.mandatoryparams = None
         self.advisoryparams = None
         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)
         ## reading parameters
         # param count
@@ -586,15 +586,14 @@  class unbundlepart(unpackermixing):
         mansizes = paramsizes[:mancount]
         advsizes = paramsizes[mancount:]
         # 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
         payload = []
         payloadsize = self._unpack(_fpayloadsize)[0]