Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 38812: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 | 8751d1e2a7ff |
children | ed8160e4fea0 |
comparison
equal
deleted
inserted
replaced
38811:64535d43c103 | 38812:9d49bb117dde |
---|---|
2888 # the context is exited. | 2888 # the context is exited. |
2889 elapsed = attr.ib(default=0) | 2889 elapsed = attr.ib(default=0) |
2890 # the number of nested timedcm context managers. | 2890 # the number of nested timedcm context managers. |
2891 level = attr.ib(default=1) | 2891 level = attr.ib(default=1) |
2892 | 2892 |
2893 def __str__(self): | 2893 def __bytes__(self): |
2894 return timecount(self.elapsed) if self.elapsed else '<unknown>' | 2894 return timecount(self.elapsed) if self.elapsed else '<unknown>' |
2895 | |
2896 __str__ = encoding.strmethod(__bytes__) | |
2895 | 2897 |
2896 @contextlib.contextmanager | 2898 @contextlib.contextmanager |
2897 def timedcm(): | 2899 def timedcm(): |
2898 """A context manager that produces timing information for a given context. | 2900 """A context manager that produces timing information for a given context. |
2899 | 2901 |
2927 def wrapper(*args, **kwargs): | 2929 def wrapper(*args, **kwargs): |
2928 with timedcm() as time_stats: | 2930 with timedcm() as time_stats: |
2929 result = func(*args, **kwargs) | 2931 result = func(*args, **kwargs) |
2930 stderr = procutil.stderr | 2932 stderr = procutil.stderr |
2931 stderr.write('%s%s: %s\n' % ( | 2933 stderr.write('%s%s: %s\n' % ( |
2932 ' ' * time_stats.level * 2, func.__name__, time_stats)) | 2934 ' ' * time_stats.level * 2, pycompat.bytestr(func.__name__), |
2935 time_stats)) | |
2933 return result | 2936 return result |
2934 return wrapper | 2937 return wrapper |
2935 | 2938 |
2936 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), | 2939 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), |
2937 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) | 2940 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) |