Patchwork [2,of,5,py3] windows: make posixfile specify mode as a sysstr

login
register
mail settings
Submitter Augie Fackler
Date Nov. 10, 2016, 10:10 p.m.
Message ID <7ae2625413fd48679660.1478815840@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/17463/
State Changes Requested
Headers show

Comments

Augie Fackler - Nov. 10, 2016, 10:10 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1478815650 18000
#      Thu Nov 10 17:07:30 2016 -0500
# Node ID 7ae2625413fd48679660eeb6500b41fd2564d570
# Parent  ee65fbd6dde3b95c62708c34eec81c88a8d1fe1f
windows: make posixfile specify mode as a sysstr

This makes the behavior match open() on both Python 2 and Python 3,
which will make porting easier.
Yuya Nishihara - Nov. 11, 2016, 2:43 p.m.
On Thu, 10 Nov 2016 17:10:40 -0500, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1478815650 18000
> #      Thu Nov 10 17:07:30 2016 -0500
> # Node ID 7ae2625413fd48679660eeb6500b41fd2564d570
> # Parent  ee65fbd6dde3b95c62708c34eec81c88a8d1fe1f
> windows: make posixfile specify mode as a sysstr
> 
> This makes the behavior match open() on both Python 2 and Python 3,
> which will make porting easier.
> 
> diff --git a/mercurial/windows.py b/mercurial/windows.py
> --- a/mercurial/windows.py
> +++ b/mercurial/windows.py
> @@ -111,17 +111,18 @@ class mixedfilemodewrapper(object):
>          object.__setattr__(self, '_lastop', self.OPREAD)
>          return self._fp.readlines(*args, **kwargs)
>  
> -def posixfile(name, mode='r', buffering=-1):
> +def posixfile(name, mode=r'r', buffering=-1):
>      '''Open a file with even more POSIX-like semantics'''
>      try:
> +        mode = pycompat.sysstr(mode)
>          fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError

pure.osutil.posixfile() still expects bytes. I have no idea how we will handle
the bytes/unicode issue in C layer, though.

Patch

diff --git a/mercurial/windows.py b/mercurial/windows.py
--- a/mercurial/windows.py
+++ b/mercurial/windows.py
@@ -111,17 +111,18 @@  class mixedfilemodewrapper(object):
         object.__setattr__(self, '_lastop', self.OPREAD)
         return self._fp.readlines(*args, **kwargs)
 
-def posixfile(name, mode='r', buffering=-1):
+def posixfile(name, mode=r'r', buffering=-1):
     '''Open a file with even more POSIX-like semantics'''
     try:
+        mode = pycompat.sysstr(mode)
         fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError
 
         # The position when opening in append mode is implementation defined, so
         # make it consistent with other platforms, which position at EOF.
-        if 'a' in mode:
+        if r'a' in mode:
             fp.seek(0, os.SEEK_END)
 
-        if '+' in mode:
+        if r'+' in mode:
             return mixedfilemodewrapper(fp)
 
         return fp