D6781: py3: for statprof's Chrome output, write json to string, then encode to bytes

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Sat Aug 31 13:35:19 EDT 2019


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

REVISION SUMMARY
  `json.dump(obj, fp)` requires `fp.write()` to accept str output, and
  since the file pointer we have there only accepts bytes, we need to
  change to json.dumps() and then encode as utf-8. We have already done
  the same thing for the json (non-Chrome) format in 4b7eb862692e <https://phab.mercurial-scm.org/rHG4b7eb862692eb29b7997780ff1e78877e0cd839c> (py3:
  encode json output to bytes and use write(), 2018-10-12).

REPOSITORY
  rHG Mercurial

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

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
@@ -875,7 +875,10 @@
               if idx not in blacklist]
     frames = collections.OrderedDict((str(k), v)
                                      for (k,v) in enumerate(id2stack))
-    json.dump(dict(traceEvents=events, stackFrames=frames), fp, indent=1)
+    data = json.dumps(dict(traceEvents=events, stackFrames=frames), indent=1)
+    if not isinstance(data, bytes):
+        data = data.encode('utf-8')
+    fp.write(data)
     fp.write('\n')
 
 def printusage():



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


More information about the Mercurial-devel mailing list