Patchwork [2,of,2] py3: document why os.fsencode() can be used to get back bytes argv

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 9, 2016, 2:17 p.m.
Message ID <29fff4afc30fe44c5a85.1478701022@mimosa>
Download mbox | patch
Permalink /patch/17414/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 9, 2016, 2:17 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1478696769 -32400
#      Wed Nov 09 22:06:09 2016 +0900
# Node ID 29fff4afc30fe44c5a856b2d3092e2ac985bd60a
# Parent  a08b40ca7b6427d1dee9f52c34df9d16af440592
py3: document why os.fsencode() can be used to get back bytes argv

And a possible Windows issue. I'm sad we have to do such ugly hack, but
that's the unicode on Python 3.
Augie Fackler - Nov. 9, 2016, 4:28 p.m.
Queued these, thanks.

> On Nov 9, 2016, at 09:17, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1478696769 -32400
> #      Wed Nov 09 22:06:09 2016 +0900
> # Node ID 29fff4afc30fe44c5a856b2d3092e2ac985bd60a
> # Parent  a08b40ca7b6427d1dee9f52c34df9d16af440592
> py3: document why os.fsencode() can be used to get back bytes argv
> 
> And a possible Windows issue. I'm sad we have to do such ugly hack, but
> that's the unicode on Python 3.
> 
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -43,6 +43,14 @@ if ispy3:
>     osname = os.name.encode('ascii')
>     ospathsep = os.pathsep.encode('ascii')
>     ossep = os.sep.encode('ascii')
> +
> +    # Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix,
> +    # we can use os.fsencode() to get back bytes argv.
> +    #
> +    # https://hg.python.org/cpython/file/v3.5.1/Programs/python.c#l55
> +    #
> +    # TODO: On Windows, the native argv is wchar_t, so we'll need a different
> +    # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
>     sysargv = list(map(os.fsencode, sys.argv))
> 
>     def sysstr(s):
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -43,6 +43,14 @@  if ispy3:
     osname = os.name.encode('ascii')
     ospathsep = os.pathsep.encode('ascii')
     ossep = os.sep.encode('ascii')
+
+    # Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix,
+    # we can use os.fsencode() to get back bytes argv.
+    #
+    # https://hg.python.org/cpython/file/v3.5.1/Programs/python.c#l55
+    #
+    # TODO: On Windows, the native argv is wchar_t, so we'll need a different
+    # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
     sysargv = list(map(os.fsencode, sys.argv))
 
     def sysstr(s):