[PATCH 2 of 7] stringutil: move _formatsetrepr() from smartset

Yuya Nishihara yuya at tcha.org
Thu Jul 5 10:14:27 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1528618771 -32400
#      Sun Jun 10 17:19:31 2018 +0900
# Node ID 6d9c3b65ffa5a397dfb163a392104349e97a22fe
# Parent  b67512d491f655c9602004420f8a5922341ba5cc
stringutil: move _formatsetrepr() from smartset

I'll add a matcher subclass wrapping a boolean function, which will use
buildrepr() to provide debugging information in a similar way to
smartset.filteredset.

diff --git a/mercurial/smartset.py b/mercurial/smartset.py
--- a/mercurial/smartset.py
+++ b/mercurial/smartset.py
@@ -13,29 +13,9 @@ from . import (
     pycompat,
     util,
 )
-
-def _formatsetrepr(r):
-    """Format an optional printable representation of a set
-
-    ========  =================================
-    type(r)   example
-    ========  =================================
-    tuple     ('<not %r>', other)
-    bytes     '<branch closed>'
-    callable  lambda: '<branch %r>' % sorted(b)
-    object    other
-    ========  =================================
-    """
-    if r is None:
-        return ''
-    elif isinstance(r, tuple):
-        return r[0] % pycompat.rapply(pycompat.maybebytestr, r[1:])
-    elif isinstance(r, bytes):
-        return r
-    elif callable(r):
-        return r()
-    else:
-        return pycompat.byterepr(r)
+from .utils import (
+    stringutil,
+)
 
 def _typename(o):
     return pycompat.sysbytes(type(o).__name__).lstrip('_')
@@ -392,7 +372,7 @@ class baseset(abstractsmartset):
     @encoding.strmethod
     def __repr__(self):
         d = {None: '', False: '-', True: '+'}[self._ascending]
-        s = _formatsetrepr(self._datarepr)
+        s = stringutil.buildrepr(self._datarepr)
         if not s:
             l = self._list
             # if _list has been built from a set, it might have a different
@@ -514,7 +494,7 @@ class filteredset(abstractsmartset):
     @encoding.strmethod
     def __repr__(self):
         xs = [pycompat.byterepr(self._subset)]
-        s = _formatsetrepr(self._condrepr)
+        s = stringutil.buildrepr(self._condrepr)
         if s:
             xs.append(s)
         return '<%s %s>' % (_typename(self), ', '.join(xs))
diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py
+++ b/mercurial/utils/stringutil.py
@@ -90,6 +90,29 @@ def prettyrepr(o):
         p0, p1 = q0, q1
     return '\n'.join('  ' * l + s for l, s in lines)
 
+def buildrepr(r):
+    """Format an optional printable representation from unexpanded bits
+
+    ========  =================================
+    type(r)   example
+    ========  =================================
+    tuple     ('<not %r>', other)
+    bytes     '<branch closed>'
+    callable  lambda: '<branch %r>' % sorted(b)
+    object    other
+    ========  =================================
+    """
+    if r is None:
+        return ''
+    elif isinstance(r, tuple):
+        return r[0] % pycompat.rapply(pycompat.maybebytestr, r[1:])
+    elif isinstance(r, bytes):
+        return r
+    elif callable(r):
+        return r()
+    else:
+        return pycompat.byterepr(r)
+
 def binary(s):
     """return true if a string is binary data"""
     return bool(s and '\0' in s)


More information about the Mercurial-devel mailing list