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

Pulkit Goyal 7895pulkit at gmail.com
Fri Feb 24 09:47:44 EST 2017


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.​



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

Yes that can reduce the overhead which will be required to fix things like
indirect calls to inbuilt open() by replacing that with pycompat.open().
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mercurial-scm.org/pipermail/mercurial-devel/attachments/20170224/f6c48b14/attachment.html>


More information about the Mercurial-devel mailing list