py3: add a bytes version of os.name
authorPulkit Goyal <7895pulkit@gmail.com>
Sun, 06 Nov 2016 03:33:22 +0530
changeset 30302 3874ddba1ab4
parent 30301 8321b083a83d
child 30303 ad40d307a9f0
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.
mercurial/pycompat.py
--- a/mercurial/pycompat.py	Sun Nov 06 12:18:23 2016 +0900
+++ 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