Patchwork py3: add os.getcwdb() to have bytes path

login
register
mail settings
Submitter Pulkit Goyal
Date Nov. 22, 2016, 2:40 p.m.
Message ID <19990605d51975dcbf06.1479825608@pulkit-goyal>
Download mbox | patch
Permalink /patch/17699/
State Accepted
Headers show

Comments

Pulkit Goyal - Nov. 22, 2016, 2:40 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1479820610 -19800
#      Tue Nov 22 18:46:50 2016 +0530
# Node ID 19990605d51975dcbf06e6faa74832249d215200
# Parent  01d8600955ccbc8cd53db2c1613ec7c3f7913ee2
py3: add os.getcwdb() to have bytes path

Following the behaviour of Python 3, os.getcwd() return unicodes. We need
bytes version as path variables are bytes in UNIX. Python 3 has os.getcwdb()
which returns current working directory in bytes.

Like rest of the things there in pycompat, like osname, ossep, we need to
rewrite every instance of os.getcwd to pycompat.getcwd to make them work
correctly on Python 3.
Augie Fackler - Nov. 22, 2016, 3:55 p.m.
On Tue, Nov 22, 2016 at 08:10:08PM +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1479820610 -19800
> #      Tue Nov 22 18:46:50 2016 +0530
> # Node ID 19990605d51975dcbf06e6faa74832249d215200
> # Parent  01d8600955ccbc8cd53db2c1613ec7c3f7913ee2
> py3: add os.getcwdb() to have bytes path

Queued this, thanks. Should I expect a series of trivial updates for
s/os/pycompat on these getcwd calls?

>
> Following the behaviour of Python 3, os.getcwd() return unicodes. We need
> bytes version as path variables are bytes in UNIX. Python 3 has os.getcwdb()
> which returns current working directory in bytes.
>
> Like rest of the things there in pycompat, like osname, ossep, we need to
> rewrite every instance of os.getcwd to pycompat.getcwd to make them work
> correctly on Python 3.
>
> diff -r 01d8600955cc -r 19990605d519 mercurial/dispatch.py
> --- a/mercurial/dispatch.py	Mon Nov 21 21:36:46 2016 -0500
> +++ b/mercurial/dispatch.py	Tue Nov 22 18:46:50 2016 +0530
> @@ -667,7 +667,7 @@
>      """
>      if wd is None:
>          try:
> -            wd = os.getcwd()
> +            wd = pycompat.getcwd()
>          except OSError as e:
>              raise error.Abort(_("error getting current working directory: %s") %
>                                e.strerror)
> diff -r 01d8600955cc -r 19990605d519 mercurial/pycompat.py
> --- a/mercurial/pycompat.py	Mon Nov 21 21:36:46 2016 -0500
> +++ b/mercurial/pycompat.py	Tue Nov 22 18:46:50 2016 +0530
> @@ -43,6 +43,9 @@
>      osname = os.name.encode('ascii')
>      ospathsep = os.pathsep.encode('ascii')
>      ossep = os.sep.encode('ascii')
> +    # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which
> +    # returns bytes.
> +    getcwd = os.getcwdb
>
>      # TODO: .buffer might not exist if std streams were replaced; we'll need
>      # a silly wrapper to make a bytes stream backed by a unicode one.
> @@ -110,6 +113,7 @@
>      stdout = sys.stdout
>      stderr = sys.stderr
>      sysargv = sys.argv
> +    getcwd = os.getcwd
>
>  stringio = io.StringIO
>  empty = _queue.Empty
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Pulkit Goyal - Nov. 22, 2016, 4:23 p.m.
On Tue, Nov 22, 2016 at 9:25 PM, Augie Fackler <raf@durin42.com> wrote:
> On Tue, Nov 22, 2016 at 08:10:08PM +0530, Pulkit Goyal wrote:
>> # HG changeset patch
>> # User Pulkit Goyal <7895pulkit@gmail.com>
>> # Date 1479820610 -19800
>> #      Tue Nov 22 18:46:50 2016 +0530
>> # Node ID 19990605d51975dcbf06e6faa74832249d215200
>> # Parent  01d8600955ccbc8cd53db2c1613ec7c3f7913ee2
>> py3: add os.getcwdb() to have bytes path
>
> Queued this, thanks. Should I expect a series of trivial updates for
> s/os/pycompat on these getcwd calls?

Well yes, I am still thinking whether to change every occurrence or
just use the transformer for all related changes. Suggestions!
Augie Fackler - Nov. 22, 2016, 4:29 p.m.
On Tue, Nov 22, 2016 at 11:23 AM, Pulkit Goyal <7895pulkit@gmail.com> wrote:
> On Tue, Nov 22, 2016 at 9:25 PM, Augie Fackler <raf@durin42.com> wrote:
>> On Tue, Nov 22, 2016 at 08:10:08PM +0530, Pulkit Goyal wrote:
>>> # HG changeset patch
>>> # User Pulkit Goyal <7895pulkit@gmail.com>
>>> # Date 1479820610 -19800
>>> #      Tue Nov 22 18:46:50 2016 +0530
>>> # Node ID 19990605d51975dcbf06e6faa74832249d215200
>>> # Parent  01d8600955ccbc8cd53db2c1613ec7c3f7913ee2
>>> py3: add os.getcwdb() to have bytes path
>>
>> Queued this, thanks. Should I expect a series of trivial updates for
>> s/os/pycompat on these getcwd calls?
>
> Well yes, I am still thinking whether to change every occurrence or
> just use the transformer for all related changes. Suggestions!

My inclination is to just do the edit. Let's not make the transformer
too magical.

Patch

diff -r 01d8600955cc -r 19990605d519 mercurial/dispatch.py
--- a/mercurial/dispatch.py	Mon Nov 21 21:36:46 2016 -0500
+++ b/mercurial/dispatch.py	Tue Nov 22 18:46:50 2016 +0530
@@ -667,7 +667,7 @@ 
     """
     if wd is None:
         try:
-            wd = os.getcwd()
+            wd = pycompat.getcwd()
         except OSError as e:
             raise error.Abort(_("error getting current working directory: %s") %
                               e.strerror)
diff -r 01d8600955cc -r 19990605d519 mercurial/pycompat.py
--- a/mercurial/pycompat.py	Mon Nov 21 21:36:46 2016 -0500
+++ b/mercurial/pycompat.py	Tue Nov 22 18:46:50 2016 +0530
@@ -43,6 +43,9 @@ 
     osname = os.name.encode('ascii')
     ospathsep = os.pathsep.encode('ascii')
     ossep = os.sep.encode('ascii')
+    # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which
+    # returns bytes.
+    getcwd = os.getcwdb
 
     # TODO: .buffer might not exist if std streams were replaced; we'll need
     # a silly wrapper to make a bytes stream backed by a unicode one.
@@ -110,6 +113,7 @@ 
     stdout = sys.stdout
     stderr = sys.stderr
     sysargv = sys.argv
+    getcwd = os.getcwd
 
 stringio = io.StringIO
 empty = _queue.Empty