Patchwork D2985: wireproto: implement custom __repr__ for frame

login
register
mail settings
Submitter phabricator
Date April 3, 2018, 4:51 p.m.
Message ID <d08d096273ee372c927527f19a0792d8@localhost.localdomain>
Download mbox | patch
Permalink /patch/30191/
State Not Applicable
Headers show

Comments

phabricator - April 3, 2018, 4:51 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG5ef2da00e935: wireproto: implement custom __repr__ for frame (authored by indygreg, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D2985?vs=7448&id=7559

REVISION DETAIL
  https://phab.mercurial-scm.org/D2985

AFFECTED FILES
  mercurial/wireprotoframing.py

CHANGE DETAILS




To: indygreg, #hg-reviewers, durin42
Cc: mercurial-devel

Patch

diff --git a/mercurial/wireprotoframing.py b/mercurial/wireprotoframing.py
--- a/mercurial/wireprotoframing.py
+++ b/mercurial/wireprotoframing.py
@@ -106,6 +106,15 @@ 
 
 ARGUMENT_RECORD_HEADER = struct.Struct(r'<HH')
 
+def humanflags(mapping, value):
+    """Convert a numeric flags value to a human value, using a mapping table."""
+    flags = []
+    for val, name in sorted({v: k for k, v in mapping.iteritems()}.iteritems()):
+        if value & val:
+            flags.append(name)
+
+    return b'|'.join(flags)
+
 @attr.s(slots=True)
 class frameheader(object):
     """Represents the data in a frame header."""
@@ -117,7 +126,7 @@ 
     typeid = attr.ib()
     flags = attr.ib()
 
-@attr.s(slots=True)
+@attr.s(slots=True, repr=False)
 class frame(object):
     """Represents a parsed frame."""
 
@@ -128,6 +137,19 @@ 
     flags = attr.ib()
     payload = attr.ib()
 
+    def __repr__(self):
+        typename = '<unknown>'
+        for name, value in FRAME_TYPES.iteritems():
+            if value == self.typeid:
+                typename = name
+                break
+
+        return ('frame(size=%d; request=%d; stream=%d; streamflags=%s; '
+                'type=%s; flags=%s)' % (
+            len(self.payload), self.requestid, self.streamid,
+            humanflags(STREAM_FLAGS, self.streamflags), typename,
+            humanflags(FRAME_TYPE_FLAGS[self.typeid], self.flags)))
+
 def makeframe(requestid, streamid, streamflags, typeid, flags, payload):
     """Assemble a frame into a byte array."""
     # TODO assert size of payload.