D5010: py3: encode str to bytes

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Fri Oct 12 17:24:53 UTC 2018


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

REVISION SUMMARY
  These fields are str on Python 2 and 3. This module doesn't import any Mercurial
  modules. So I just did the str -> bytes inline.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/lsprof.py

CHANGE DETAILS

diff --git a/mercurial/lsprof.py b/mercurial/lsprof.py
--- a/mercurial/lsprof.py
+++ b/mercurial/lsprof.py
@@ -91,6 +91,8 @@
 
 def label(code):
     if isinstance(code, str):
+        if sys.version_info.major >= 3:
+            code = code.encode('latin-1')
         return code
     try:
         mname = _fn2mod[code.co_filename]
@@ -104,10 +106,14 @@
                 mname = _fn2mod[code.co_filename] = k
                 break
         else:
-            mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename
+            mname = _fn2mod[code.co_filename] = r'<%s>' % code.co_filename
+
+    res = r'%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
 
-    return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
+    if sys.version_info.major >= 3:
+        res = res.encode('latin-1')
 
+    return res
 
 if __name__ == '__main__':
     import os



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


More information about the Mercurial-devel mailing list