Patchwork [3,of,6] py3: replace os.sep with pycompat.ossep (part 1 of 4)

login
register
mail settings
Submitter Pulkit Goyal
Date Dec. 17, 2016, 3:41 p.m.
Message ID <4107888f9b89e178ab83.1481989302@pulkit-goyal>
Download mbox | patch
Permalink /patch/17945/
State Accepted
Headers show

Comments

Pulkit Goyal - Dec. 17, 2016, 3:41 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1481984790 -19800
#      Sat Dec 17 19:56:30 2016 +0530
# Node ID 4107888f9b89e178ab8369a6d63ebb2e17dc26b9
# Parent  32865b15b8417a3ad673b72ffc713dffdebcd04a
py3: replace os.sep with pycompat.ossep (part 1 of 4)

os.sep returns unicodes on Python 3. We have pycompat.ossep which returns
bytes. This patch is a part of 4 patch series which will replace all the
occurences of os.sep to pycompat.ossep
Yuya Nishihara - Dec. 18, 2016, 9:14 a.m.
On Sat, 17 Dec 2016 21:11:42 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1481984790 -19800
> #      Sat Dec 17 19:56:30 2016 +0530
> # Node ID 4107888f9b89e178ab8369a6d63ebb2e17dc26b9
> # Parent  32865b15b8417a3ad673b72ffc713dffdebcd04a
> py3: replace os.sep with pycompat.ossep (part 1 of 4)

>  def endswithsep(path):
>      '''Check path ends with os.sep or os.altsep.'''
> -    return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
> +    return path.endswith(pycompat.ossep) or os.altsep\
> +                        and path.endswith(os.altsep)

I want to keep the 'x and y' in the same line since it is stronger than 'or'.
Rewritten as:

    return (path.endswith(pycompat.ossep)
            or os.altsep and path.endswith(os.altsep))

BTW, we'll need pycompat.osaltsep.

Patch

diff -r 32865b15b841 -r 4107888f9b89 mercurial/commands.py
--- a/mercurial/commands.py	Sat Dec 17 19:47:17 2016 +0530
+++ b/mercurial/commands.py	Sat Dec 17 19:56:30 2016 +0530
@@ -2356,7 +2356,7 @@ 
     def complete(path, acceptable):
         dirstate = repo.dirstate
         spec = os.path.normpath(os.path.join(pycompat.getcwd(), path))
-        rootdir = repo.root + os.sep
+        rootdir = repo.root + pycompat.ossep
         if spec != repo.root and not spec.startswith(rootdir):
             return [], []
         if os.path.isdir(spec):
@@ -2364,7 +2364,7 @@ 
         spec = spec[len(rootdir):]
         fixpaths = pycompat.ossep != '/'
         if fixpaths:
-            spec = spec.replace(os.sep, '/')
+            spec = spec.replace(pycompat.ossep, '/')
         speclen = len(spec)
         fullpaths = opts['full']
         files, dirs = set(), set()
@@ -2372,11 +2372,11 @@ 
         for f, st in dirstate.iteritems():
             if f.startswith(spec) and st[0] in acceptable:
                 if fixpaths:
-                    f = f.replace('/', os.sep)
+                    f = f.replace('/', pycompat.ossep)
                 if fullpaths:
                     addfile(f)
                     continue
-                s = f.find(os.sep, speclen)
+                s = f.find(pycompat.ossep, speclen)
                 if s >= 0:
                     adddir(f[:s])
                 else:
diff -r 32865b15b841 -r 4107888f9b89 mercurial/util.py
--- a/mercurial/util.py	Sat Dec 17 19:47:17 2016 +0530
+++ b/mercurial/util.py	Sat Dec 17 19:56:30 2016 +0530
@@ -916,7 +916,7 @@ 
         a.pop()
         b.pop()
     b.reverse()
-    return os.sep.join((['..'] * len(a)) + b) or '.'
+    return pycompat.ossep.join((['..'] * len(a)) + b) or '.'
 
 def mainfrozen():
     """return True if we are a frozen executable.
@@ -1303,7 +1303,7 @@ 
     def _makefspathcacheentry(dir):
         return dict((normcase(n), n) for n in os.listdir(dir))
 
-    seps = os.sep
+    seps = pycompat.ossep
     if os.altsep:
         seps = seps + os.altsep
     # Protect backslashes. This gets silly very quickly.
@@ -1370,7 +1370,8 @@ 
 
 def endswithsep(path):
     '''Check path ends with os.sep or os.altsep.'''
-    return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
+    return path.endswith(pycompat.ossep) or os.altsep\
+                        and path.endswith(os.altsep)
 
 def splitpath(path):
     '''Split path by os.sep.
@@ -1378,7 +1379,7 @@ 
     an alternative of simple "xxx.split(os.sep)".
     It is recommended to use os.path.normpath() before using this
     function if need.'''
-    return path.split(os.sep)
+    return path.split(pycompat.ossep)
 
 def gui():
     '''Are we running in a GUI?'''