[PATCH 1 of 2] encoding: teach unitolocal() and unifromlocal() to handle None

Matt Harbison mharbison72 at gmail.com
Sun Sep 23 01:18:52 EDT 2018


# HG changeset patch
# User Matt Harbison <matt_harbison at yahoo.com>
# Date 1537667864 14400
#      Sat Sep 22 21:57:44 2018 -0400
# Node ID 10b1e61357ac546e70e590cc79889c0889e7bc22
# Parent  ca417c9464d378661e9e962be1447647006297f3
encoding: teach unitolocal() and unifromlocal() to handle None

These are aliased to str{to,from}local(), along with pycompat.identity(),
depending on the platform.  It's inconsistent that None works on py2 because of
identity(), but not on py3.  This will be needed in the next patch.

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -202,10 +202,14 @@ def fromlocal(s):
 
 def unitolocal(u):
     """Convert a unicode string to a byte string of local encoding"""
+    if u is None:
+        return None
     return tolocal(u.encode('utf-8'))
 
 def unifromlocal(s):
     """Convert a byte string of local encoding to a unicode string"""
+    if s is None:
+        return None
     return fromlocal(s).decode('utf-8')
 
 def unimethod(bytesfunc):


More information about the Mercurial-devel mailing list