Patchwork [02,of,11] pycompat: forward encoding kwarg on open()

login
register
mail settings
Submitter Augie Fackler
Date March 26, 2017, 10:36 p.m.
Message ID <28149aad82cb12522f1b.1490567796@imladris.local>
Download mbox | patch
Permalink /patch/19700/
State Accepted
Headers show

Comments

Augie Fackler - March 26, 2017, 10:36 p.m.
# HG changeset patch
# User Augie Fackler <raf@durin42.com>
# Date 1490562287 14400
#      Sun Mar 26 17:04:47 2017 -0400
# Node ID 28149aad82cb12522f1ba50b0bb184d1f960a9c9
# Parent  1250b7d77ba41c7ce1500d4c8e5fef921a14f683
pycompat: forward encoding kwarg on open()
Yuya Nishihara - March 27, 2017, 1:49 p.m.
On Sun, 26 Mar 2017 18:36:36 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <raf@durin42.com>
> # Date 1490562287 14400
> #      Sun Mar 26 17:04:47 2017 -0400
> # Node ID 28149aad82cb12522f1ba50b0bb184d1f960a9c9
> # Parent  1250b7d77ba41c7ce1500d4c8e5fef921a14f683
> pycompat: forward encoding kwarg on open()
> 
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -163,8 +163,8 @@ if ispy3:
>      setattr = _wrapattrfunc(builtins.setattr)
>      xrange = builtins.range
>  
> -    def open(name, mode='r', buffering=-1):
> -        return builtins.open(name, sysstr(mode), buffering)
> +    def open(name, mode='r', buffering=-1, encoding=None):
> +        return builtins.open(name, sysstr(mode), buffering, encoding)

I have mixed feeling about this. open() should work on both Python 2 and 3,
but encoding isn't supported on Python 2. However, we would have to use
builtins.open() without this change.

I'll play with this and the fat-bytes patch to see if they can be factored
out to utility functions.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -163,8 +163,8 @@  if ispy3:
     setattr = _wrapattrfunc(builtins.setattr)
     xrange = builtins.range
 
-    def open(name, mode='r', buffering=-1):
-        return builtins.open(name, sysstr(mode), buffering)
+    def open(name, mode='r', buffering=-1, encoding=None):
+        return builtins.open(name, sysstr(mode), buffering, encoding)
 
     # getopt.getopt() on Python 3 deals with unicodes internally so we cannot
     # pass bytes there. Passing unicodes will result in unicodes as return