changeset 38852:9d49bb117dde

util: make new timedcmstats class Python 3 compatible
author Martijn Pieters <mj@zopatista.com>
date Thu, 02 Aug 2018 20:53:03 +0100
parents 64535d43c103
children 4ca5932065ca
files mercurial/util.py tests/test-util.py
diffstat 2 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Wed Aug 01 16:03:32 2018 -0700
+++ 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
 
--- a/tests/test-util.py	Wed Aug 01 16:03:32 2018 -0700
+++ 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