D5210: statprof: clean up unicode/bytes a little

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Thu Nov 1 21:17:17 UTC 2018


durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  I'm not really sure how this worked before, but something perturbed it
  and what I've got in this change I believe is a little tidier. This
  fixes test-profile.t on Python 3.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/statprof.py

CHANGE DETAILS

diff --git a/mercurial/statprof.py b/mercurial/statprof.py
--- a/mercurial/statprof.py
+++ b/mercurial/statprof.py
@@ -238,7 +238,7 @@
             lineno = self.lineno - 1
             fp = None
             try:
-                fp = open(self.path)
+                fp = open(self.path, 'rb')
                 for i, line in enumerate(fp):
                     if i == lineno:
                         self.source = line.strip()
@@ -274,8 +274,10 @@
         stack = []
 
         while frame:
-            stack.append(CodeSite.get(frame.f_code.co_filename, frame.f_lineno,
-                                      frame.f_code.co_name))
+            stack.append(CodeSite.get(
+                pycompat.sysbytes(frame.f_code.co_filename),
+                frame.f_lineno,
+                pycompat.sysbytes(frame.f_code.co_name)))
             frame = frame.f_back
 
         return Sample(stack, time)
@@ -372,7 +374,7 @@
             file.write("%d\0%s\n" % (time, '\0'.join(sites)))
 
 def load_data(path):
-    lines = open(path, 'r').read().splitlines()
+    lines = open(path, 'rb').read().splitlines()
 
     state.accumulated_time = [float(value) for value in lines[0].split()]
     state.samples = []
@@ -512,9 +514,9 @@
 
     for stat in stats:
         site = stat.site
-        sitelabel = '%s:%d:%s' % (pycompat.fsencode(site.filename()),
+        sitelabel = '%s:%d:%s' % (site.filename(),
                                   site.lineno,
-                                  pycompat.sysbytes(site.function))
+                                  site.function)
         fp.write(b'%6.2f %9.2f %9.2f  %s\n' % (
             stat.selfpercent(), stat.totalseconds(),
             stat.selfseconds(), sitelabel))
@@ -532,7 +534,7 @@
 
     grouped = defaultdict(list)
     for stat in stats:
-        grouped[stat.site.filename() + r":" + stat.site.function].append(stat)
+        grouped[stat.site.filename() + b":" + stat.site.function].append(stat)
 
     # compute sums for each function
     functiondata = []
@@ -561,7 +563,7 @@
             function[3], # total percent
             function[1], # total cum sec
             function[2], # total self sec
-            pycompat.sysbytes(function[0]))) # file:function
+            function[0])) # file:function
 
         function[4].sort(reverse=True, key=lambda i: i.selfseconds())
         for stat in function[4]:
@@ -696,7 +698,7 @@
                           ' %4.1f%%  %s %s'
             liststring = listpattern % (node.count / root.count * 100,
                                         filename, function)
-            codepattern = '%' + str(55 - len(liststring)) + 's %s:  %s'
+            codepattern = '%' + ('%d' % (55 - len(liststring))) + 's %d:  %s'
             codestring = codepattern % ('line', site.lineno, site.getsource(30))
 
             finalstring = liststring + codestring
@@ -777,7 +779,10 @@
         stack = []
 
         for frame in sample.stack:
-            stack.append((frame.path, frame.lineno, frame.function))
+            stack.append(
+                (pycompat.sysstr(frame.path),
+                 frame.lineno,
+                 pycompat.sysstr(frame.function)))
 
         samples.append((sample.time, stack))
 



To: durin42, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list