Patchwork [2,of,2] py3: make util.posixfile compatible with py3

login
register
mail settings
Submitter Pulkit Goyal
Date Feb. 23, 2017, 11:59 a.m.
Message ID <4572027bbe6795e66758.1487851141@pulkit-goyal>
Download mbox | patch
Permalink /patch/18746/
State Accepted
Headers show

Comments

Pulkit Goyal - Feb. 23, 2017, 11:59 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1487768580 -19800
#      Wed Feb 22 18:33:00 2017 +0530
# Node ID 4572027bbe6795e66758120375f647c1c1804195
# Parent  80693cfda5a63dee28cf9e63cf0e8cef3f27f23a
py3: make util.posixfile compatible with py3

On python 3, python's inbuilt open function accepts unicodes as its mode
argument whereas we need to pass bytes to windows.posixfile. So this patch
adds that support.
Yuya Nishihara - Feb. 23, 2017, 2:33 p.m.
On Thu, 23 Feb 2017 17:29:01 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1487768580 -19800
> #      Wed Feb 22 18:33:00 2017 +0530
> # Node ID 4572027bbe6795e66758120375f647c1c1804195
> # Parent  80693cfda5a63dee28cf9e63cf0e8cef3f27f23a
> py3: make util.posixfile compatible with py3
> 
> On python 3, python's inbuilt open function accepts unicodes as its mode
> argument whereas we need to pass bytes to windows.posixfile. So this patch
> adds that support.
> 
> diff -r 80693cfda5a6 -r 4572027bbe67 mercurial/util.py
> --- a/mercurial/util.py	Mon Feb 20 18:40:42 2017 +0530
> +++ b/mercurial/util.py	Wed Feb 22 18:33:00 2017 +0530
> @@ -114,7 +114,6 @@
>  pconvert = platform.pconvert
>  poll = platform.poll
>  popen = platform.popen
> -posixfile = platform.posixfile
>  quotecommand = platform.quotecommand
>  readpipe = platform.readpipe
>  rename = platform.rename
> @@ -147,6 +146,15 @@
>  # libraries, and sure enough Mercurial is not a library.)
>  os.stat_float_times(False)
>  
> +def posixfile(name, mode='r', buffering=-1):
> +    if pycompat.ispy3:
> +        if pycompat.osname == 'nt':
> +            return platform.posixfile(name, mode, buffering)
> +        else:
> +            return platform.posixfile(name, pycompat.sysstr(mode), buffering)
> +    else:
> +        return platform.posixfile(name, mode, buffering)

Can we make a pycompat.open() wrapper that accepts bytes?

Optionally it could be auto-imported in the same manner as getattr().

Patch

diff -r 80693cfda5a6 -r 4572027bbe67 mercurial/util.py
--- a/mercurial/util.py	Mon Feb 20 18:40:42 2017 +0530
+++ b/mercurial/util.py	Wed Feb 22 18:33:00 2017 +0530
@@ -114,7 +114,6 @@ 
 pconvert = platform.pconvert
 poll = platform.poll
 popen = platform.popen
-posixfile = platform.posixfile
 quotecommand = platform.quotecommand
 readpipe = platform.readpipe
 rename = platform.rename
@@ -147,6 +146,15 @@ 
 # libraries, and sure enough Mercurial is not a library.)
 os.stat_float_times(False)
 
+def posixfile(name, mode='r', buffering=-1):
+    if pycompat.ispy3:
+        if pycompat.osname == 'nt':
+            return platform.posixfile(name, mode, buffering)
+        else:
+            return platform.posixfile(name, pycompat.sysstr(mode), buffering)
+    else:
+        return platform.posixfile(name, mode, buffering)
+
 def safehasattr(thing, attr):
     return getattr(thing, attr, _notset) is not _notset