D7606: fuzz: fix mpatch_corpus to not have an overridden __repr__ on py3

spectral (Kyle Lippincott) phabricator at mercurial-scm.org
Wed Dec 11 18:27:01 EST 2019


spectral created this revision.
Herald added subscribers: mercurial-devel, mjpieters.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  contrib/fuzz/mpatch_corpus.py

CHANGE DETAILS

diff --git a/contrib/fuzz/mpatch_corpus.py b/contrib/fuzz/mpatch_corpus.py
--- a/contrib/fuzz/mpatch_corpus.py
+++ b/contrib/fuzz/mpatch_corpus.py
@@ -2,6 +2,7 @@
 
 import argparse
 import struct
+import sys
 import zipfile
 
 from mercurial import (
@@ -14,16 +15,22 @@
 args = ap.parse_args()
 
 
-class deltafrag(object):
+if sys.version_info[0] < 3:
+    class py2reprhack(object):
+        def __repr__(self):
+            """Py2 calls __repr__ for `bytes(foo)`, forward to __bytes__"""
+            return self.__bytes__()
+else:
+    class py2reprhack(object):
+        """Not needed on py3."""
+
+
+class deltafrag(py2reprhack):
     def __init__(self, start, end, data):
         self.start = start
         self.end = end
         self.data = data
 
-    def __repr__(self):
-        # py2 calls __repr__ when you do `bytes(foo)`
-        return self.__bytes__()
-
     def __bytes__(self):
         return (
             struct.pack(">lll", self.start, self.end, len(self.data))
@@ -31,27 +38,19 @@
         )
 
 
-class delta(object):
+class delta(py2reprhack):
     def __init__(self, frags):
         self.frags = frags
 
-    def __repr__(self):
-        # py2 calls __repr__ when you do `bytes(foo)`
-        return self.__bytes__()
-
     def __bytes__(self):
         return b''.join(bytes(f) for f in self.frags)
 
 
-class corpus(object):
+class corpus(py2reprhack):
     def __init__(self, base, deltas):
         self.base = base
         self.deltas = deltas
 
-    def __repr__(self):
-        # py2 calls __repr__ when you do `bytes(foo)`
-        return self.__bytes__()
-
     def __bytes__(self):
         deltas = [bytes(d) for d in self.deltas]
         parts = (



To: spectral, #hg-reviewers
Cc: mjpieters, mercurial-devel


More information about the Mercurial-devel mailing list