Patchwork util: make new timedcmstats class Python 3 compatible

login
register
mail settings
Submitter Martijn Pieters
Date Aug. 2, 2018, 7:58 p.m.
Message ID <d7c68ee641ff02542680.1533239881@Martijns-MacBook-Pro.local>
Download mbox | patch
Permalink /patch/33122/
State Accepted
Headers show

Comments

Martijn Pieters - Aug. 2, 2018, 7:58 p.m.
# HG changeset patch
# User Martijn Pieters <mj@zopatista.com>
# Date 1533239583 -3600
#      Thu Aug 02 20:53:03 2018 +0100
# Branch stable
# Node ID d7c68ee641ff02542680bdaa4bff95a348db688e
# Parent  07ca3b8354d59c70db5f10448e53d4bbfd50e72e
# EXP-Topic debugextensions
util: make new timedcmstats class Python 3 compatible
Yuya Nishihara - Aug. 3, 2018, 12:51 p.m.
On Thu, 02 Aug 2018 20:58:01 +0100, Martijn Pieters wrote:
> # HG changeset patch
> # User Martijn Pieters <mj@zopatista.com>
> # Date 1533239583 -3600
> #      Thu Aug 02 20:53:03 2018 +0100
> # Branch stable
> # Node ID d7c68ee641ff02542680bdaa4bff95a348db688e
> # Parent  07ca3b8354d59c70db5f10448e53d4bbfd50e72e
> # EXP-Topic debugextensions
> util: make new timedcmstats class Python 3 compatible

Queued, thanks.

Patch

diff -r 07ca3b8354d5 -r d7c68ee641ff mercurial/util.py
--- a/mercurial/util.py	Wed Aug 01 16:06:53 2018 +0200
+++ b/mercurial/util.py	Thu Aug 02 20:53:03 2018 +0100
@@ -2890,9 +2890,11 @@ 
     # the number of nested timedcm context managers.
     level = attr.ib(default=1)
 
-    def __str__(self):
+    def __bytes__(self):
         return timecount(self.elapsed) if self.elapsed else '<unknown>'
 
+    __str__ = encoding.strmethod(__bytes__)
+
 @contextlib.contextmanager
 def timedcm():
     """A context manager that produces timing information for a given context.
@@ -2929,7 +2931,8 @@ 
             result = func(*args, **kwargs)
         stderr = procutil.stderr
         stderr.write('%s%s: %s\n' % (
-            ' ' * time_stats.level * 2, func.__name__, time_stats))
+            ' ' * time_stats.level * 2, pycompat.bytestr(func.__name__),
+            time_stats))
         return result
     return wrapper
 
diff -r 07ca3b8354d5 -r d7c68ee641ff tests/test-util.py
--- a/tests/test-util.py	Wed Aug 01 16:06:53 2018 +0200
+++ b/tests/test-util.py	Thu Aug 02 20:53:03 2018 +0100
@@ -70,8 +70,10 @@ 
     def testtimedcmstatsstr(self):
         stats = util.timedcmstats()
         self.assertEqual(str(stats), '<unknown>')
+        self.assertEqual(bytes(stats), b'<unknown>')
         stats.elapsed = 12.34
-        self.assertEqual(str(stats), util.timecount(12.34))
+        self.assertEqual(str(stats), pycompat.sysstr(util.timecount(12.34)))
+        self.assertEqual(bytes(stats), util.timecount(12.34))
 
     def testtimedcmcleanexit(self):
         # timestamps 1, 4, elapsed time of 4 - 1 = 3