D2985: wireproto: implement custom __repr__ for frame

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Tue Apr 3 16:51:54 UTC 2018


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

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()
 
- at attr.s(slots=True)
+ at 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.



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


More information about the Mercurial-devel mailing list