Patchwork [3,of,7] py3: make scmpoxis.systemrcpath() return bytes

login
register
mail settings
Submitter Pulkit Goyal
Date Nov. 2, 2016, 10:23 p.m.
Message ID <9d54c24d17daddf2ede7.1478125388@pulkit-goyal>
Download mbox | patch
Permalink /patch/17300/
State Accepted
Headers show

Comments

Pulkit Goyal - Nov. 2, 2016, 10:23 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1478119178 -19800
#      Thu Nov 03 02:09:38 2016 +0530
# Node ID 9d54c24d17daddf2ede7fe7ce58751ab9a1780a4
# Parent  6585e9c1d915818d5138f6cb4134707002d5d749
py3: make scmpoxis.systemrcpath() return bytes

The variable `p` is a str on Python 3 which is not bytes. We should convert
it to bytes because
1. root is bytes and we want the final output in bytes
2. to make the if condition works fine because in py3 its p != b'/'

So even if p is '/' but left as a str on py3, will make the condition false.
This patch ensures that scmposix.systemrcpath() return bytes and also
scmposix._rcfiles() returns and accepts bytes. The later is used in
scmposix.py only.
Yuya Nishihara - Nov. 4, 2016, 3:40 a.m.
On Thu, 03 Nov 2016 03:53:08 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1478119178 -19800
> #      Thu Nov 03 02:09:38 2016 +0530
> # Node ID 9d54c24d17daddf2ede7fe7ce58751ab9a1780a4
> # Parent  6585e9c1d915818d5138f6cb4134707002d5d749
> py3: make scmpoxis.systemrcpath() return bytes
> 
> The variable `p` is a str on Python 3 which is not bytes. We should convert
> it to bytes because
> 1. root is bytes and we want the final output in bytes
> 2. to make the if condition works fine because in py3 its p != b'/'
> 
> So even if p is '/' but left as a str on py3, will make the condition false.
> This patch ensures that scmposix.systemrcpath() return bytes and also
> scmposix._rcfiles() returns and accepts bytes. The later is used in
> scmposix.py only.
> 
> diff -r 6585e9c1d915 -r 9d54c24d17da mercurial/scmposix.py
> --- a/mercurial/scmposix.py	Thu Nov 03 01:55:44 2016 +0530
> +++ b/mercurial/scmposix.py	Thu Nov 03 02:09:38 2016 +0530
> @@ -27,6 +27,8 @@
>      # old mod_python does not set sys.argv
>      if len(getattr(sys, 'argv', [])) > 0:
>          p = os.path.dirname(os.path.dirname(sys.argv[0]))
> +        if not isinstance(p, bytes):
> +            p = p.encode('utf-8')

We can't assume external encoding is UTF-8. IIRC, Augie had some patches
to work around the sys.argv issue.
Pulkit Goyal - Nov. 5, 2016, 12:04 a.m.
> We can't assume external encoding is UTF-8. IIRC, Augie had some patches
> to work around the sys.argv issue.

Well this was the thread, the patch didn't went through
https://www.mercurial-scm.org/pipermail/mercurial-devel/2016-October/089207.html

I think Augie was talking of this link maybe -
http://bugs.python.org/issue8776#msg217416 and yeah quite helpful
comments to help us in this decoding and encoding thing.

Patch

diff -r 6585e9c1d915 -r 9d54c24d17da mercurial/scmposix.py
--- a/mercurial/scmposix.py	Thu Nov 03 01:55:44 2016 +0530
+++ b/mercurial/scmposix.py	Thu Nov 03 02:09:38 2016 +0530
@@ -27,6 +27,8 @@ 
     # old mod_python does not set sys.argv
     if len(getattr(sys, 'argv', [])) > 0:
         p = os.path.dirname(os.path.dirname(sys.argv[0]))
+        if not isinstance(p, bytes):
+            p = p.encode('utf-8')
         if p != '/':
             path.extend(_rcfiles(os.path.join(p, root)))
     path.extend(_rcfiles('/' + root))