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

Yuya Nishihara yuya at tcha.org
Fri Feb 24 11:40:52 EST 2017


On Fri, 24 Feb 2017 20:17:44 +0530, Pulkit Goyal wrote:
> On Thu, Feb 23, 2017 at 8:03 PM, Yuya Nishihara <yuya at tcha.org> wrote:
> 
> > On Thu, 23 Feb 2017 17:29:01 +0530, Pulkit Goyal wrote:
> > > # HG changeset patch
> > > # User Pulkit Goyal <7895pulkit at 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?
> >
> 
> ​Yes we can but we need to implement windows.posixfile() in pycompat.py
> only otherwise we will end up with an import cycle. We can't import
> anything from mercurial into pycompat.py is a reason I decided to do it
> util.py.​

I mean:

 1. add pycompat.open() which is a wrapper of builtins.open()
 2. make posix.posixfile() use it so it can be bytes-free
 3. optionally we could automatically insert "from pycompat import open"


More information about the Mercurial-devel mailing list