changeset 36644:e2b87e19c6ef

pycompat: prevent encoding or decoding values if not required pycompat.py has functions strurl and bytesurl which decodes and encodes the url passed on Python 3 respectively. In some cases, strurl gets a url which is already str and bytesurl gets a url which is already bytes. Let's prevent encoding or decoding the values again if not required. Differential Revision: https://phab.mercurial-scm.org/D2472
author Pulkit Goyal <7895pulkit@gmail.com>
date Tue, 27 Feb 2018 00:33:46 +0530
parents 1e1c1bfb0be4
children 7bc33d677c0c
files mercurial/pycompat.py
diffstat 1 files changed, 6 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/pycompat.py	Sat Mar 03 10:39:48 2018 -0500
+++ b/mercurial/pycompat.py	Tue Feb 27 00:33:46 2018 +0530
@@ -192,11 +192,15 @@
 
     def strurl(url):
         """Converts a bytes url back to str"""
-        return url.decode(u'ascii')
+        if isinstance(url, bytes):
+            return url.decode(u'ascii')
+        return url
 
     def bytesurl(url):
         """Converts a str url to bytes by encoding in ascii"""
-        return url.encode(u'ascii')
+        if isinstance(url, str):
+            return url.encode(u'ascii')
+        return url
 
     def raisewithtb(exc, tb):
         """Raise exception with the given traceback"""