Patchwork [2,of,7] py3: make sure osutil.listdir() returns what it gets

login
register
mail settings
Submitter Pulkit Goyal
Date Nov. 2, 2016, 10:15 p.m.
Message ID <f87503dd04ee704ed0c1.1478124916@pulkit-goyal>
Download mbox | patch
Permalink /patch/17292/
State Not Applicable
Headers show

Comments

Pulkit Goyal - Nov. 2, 2016, 10:15 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1478118344 -19800
#      Thu Nov 03 01:55:44 2016 +0530
# Node ID f87503dd04ee704ed0c13d6ec9673fe7d946fd6c
# Parent  e23132d2c665e788646000318a9880bf9c5a177f
py3: make sure osutil.listdir() returns what it gets

osutil.listdir() on py3 was having problems with bytes. Since
os.sep is a str in Py3, we need to check if what passed is bytes and then
convert os.sep to bytes. On python 2, doing os.sep.encode() is okay.
After this patch, osutil.listdir() argument will return what is gets as an
argument on Python 3.5.

Patch

diff -r e23132d2c665 -r f87503dd04ee mercurial/pure/osutil.py
--- a/mercurial/pure/osutil.py	Thu Nov 03 00:28:33 2016 +0530
+++ b/mercurial/pure/osutil.py	Thu Nov 03 01:55:44 2016 +0530
@@ -51,8 +51,11 @@ 
     '''
     result = []
     prefix = path
-    if not prefix.endswith(os.sep):
-        prefix += os.sep
+    sep = os.sep
+    if isinstance(path, bytes):
+        sep = sep.encode('ascii')
+    if not prefix.endswith(sep):
+        prefix += sep
     names = os.listdir(path)
     names.sort()
     for fn in names: