Patchwork [3,of,8] py3: add a bytes version of os.name

login
register
mail settings
Submitter Pulkit Goyal
Date Nov. 5, 2016, 11:16 p.m.
Message ID <20ee6f9c56233f7c43ce.1478387780@pulkit-goyal>
Download mbox | patch
Permalink /patch/17355/
State Accepted
Headers show

Comments

Pulkit Goyal - Nov. 5, 2016, 11:16 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1478383402 -19800
#      Sun Nov 06 03:33:22 2016 +0530
# Node ID 20ee6f9c56233f7c43ce73d6d976ef6817fb0695
# Parent  972b0c0175fc402ffe8beac276b737b32ce0df0b
py3: add a bytes version of os.name

os.name returns unicodes on py3. Most of our checks are like
    os.name == 'nt'

Because of the transformer, on the right hand side we have b'nt'. The
condition will never satisfy even if os.name returns 'nt' as that will be an
unicode.
We either need to encode every occurence of os.name or have a
new variable which is much cleaner. Now we have pycompat.osname.
There are around 53 occurences of os.name in the codebase which needs to
be replaced by pycompat.osname to support Python 3.
Yuya Nishihara - Nov. 6, 2016, 3:27 a.m.
On Sun, 06 Nov 2016 04:46:20 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1478383402 -19800
> #      Sun Nov 06 03:33:22 2016 +0530
> # Node ID 20ee6f9c56233f7c43ce73d6d976ef6817fb0695
> # Parent  972b0c0175fc402ffe8beac276b737b32ce0df0b
> py3: add a bytes version of os.name

Queued 3, 4, 6, and 7, thanks.

> We either need to encode every occurence of os.name or have a
> new variable which is much cleaner. Now we have pycompat.osname.
> There are around 53 occurences of os.name in the codebase which needs to
> be replaced by pycompat.osname to support Python 3.

We might want util.iswindows (and/or isposix) constants. That's what we
have to care.

Patch

diff -r 972b0c0175fc -r 20ee6f9c5623 mercurial/pycompat.py
--- a/mercurial/pycompat.py	Sun Nov 06 03:12:40 2016 +0530
+++ b/mercurial/pycompat.py	Sun Nov 06 03:33:22 2016 +0530
@@ -10,6 +10,7 @@ 
 
 from __future__ import absolute_import
 
+import os
 import sys
 
 ispy3 = (sys.version_info[0] >= 3)
@@ -34,9 +35,10 @@ 
 if ispy3:
     import builtins
     import functools
-    import os
     fsencode = os.fsencode
     fsdecode = os.fsdecode
+    #A bytes version of os.name.
+    osname = os.name.encode('ascii')
 
     def sysstr(s):
         """Return a keyword str to be passed to Python functions such as
@@ -82,6 +84,8 @@ 
     def fsdecode(filename):
         return filename
 
+    osname = os.name
+
 stringio = io.StringIO
 empty = _queue.Empty
 queue = _queue.Queue