Patchwork lock: do not encode result of gethostname on Python 2

login
register
mail settings
Submitter Yuya Nishihara
Date March 12, 2017, 11:35 p.m.
Message ID <197708ab23f80bec7fd9.1489361742@mimosa>
Download mbox | patch
Permalink /patch/19258/
State Accepted
Headers show

Comments

Yuya Nishihara - March 12, 2017, 11:35 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1489361194 25200
#      Sun Mar 12 16:26:34 2017 -0700
# Node ID 197708ab23f80bec7fd9cf70beb96eb759f2e59d
# Parent  52ee1b5ac277bd5569a8d3e3ae3e11dff0543323
lock: do not encode result of gethostname on Python 2

If a hostname contained non-ascii character, str.encode() would first try
to decode it to a unicode and raise UnicodeDecodeError.
Augie Fackler - March 13, 2017, 12:12 a.m.
On Sun, Mar 12, 2017 at 04:35:42PM -0700, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1489361194 25200
> #      Sun Mar 12 16:26:34 2017 -0700
> # Node ID 197708ab23f80bec7fd9cf70beb96eb759f2e59d
> # Parent  52ee1b5ac277bd5569a8d3e3ae3e11dff0543323
> lock: do not encode result of gethostname on Python 2

Derp, queued, thanks.

>
> If a hostname contained non-ascii character, str.encode() would first try
> to decode it to a unicode and raise UnicodeDecodeError.
>
> diff --git a/mercurial/lock.py b/mercurial/lock.py
> --- a/mercurial/lock.py
> +++ b/mercurial/lock.py
> @@ -28,8 +28,9 @@ def _getlockprefix():
>      confidence. Typically it's just hostname. On modern linux, we include an
>      extra Linux-specific pid namespace identifier.
>      """
> -    result = socket.gethostname().encode(
> -        pycompat.sysstr(encoding.encoding), 'replace')
> +    result = socket.gethostname()
> +    if pycompat.ispy3:
> +        result = result.encode(pycompat.sysstr(encoding.encoding), 'replace')
>      if pycompat.sysplatform.startswith('linux'):
>          try:
>              result += '/%x' % os.stat('/proc/self/ns/pid').st_ino
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/lock.py b/mercurial/lock.py
--- a/mercurial/lock.py
+++ b/mercurial/lock.py
@@ -28,8 +28,9 @@  def _getlockprefix():
     confidence. Typically it's just hostname. On modern linux, we include an
     extra Linux-specific pid namespace identifier.
     """
-    result = socket.gethostname().encode(
-        pycompat.sysstr(encoding.encoding), 'replace')
+    result = socket.gethostname()
+    if pycompat.ispy3:
+        result = result.encode(pycompat.sysstr(encoding.encoding), 'replace')
     if pycompat.sysplatform.startswith('linux'):
         try:
             result += '/%x' % os.stat('/proc/self/ns/pid').st_ino