[PATCH 6 of 6] pycompat: use unicode literals to appease Python 3

Yuya Nishihara yuya at tcha.org
Sat Jun 25 23:08:21 EDT 2016


On Sat, 25 Jun 2016 08:16:37 -0700, Gregory Szorc wrote:
> On Thu, Jun 9, 2016 at 7:53 AM, Yuya Nishihara <yuya at tcha.org> wrote:
> 
> > On Tue, 31 May 2016 22:53:03 -0700, Gregory Szorc wrote:
> > > # HG changeset patch
> > > # User Gregory Szorc <gregory.szorc at gmail.com>
> > > # Date 1464759504 25200
> > > #      Tue May 31 22:38:24 2016 -0700
> > > # Node ID dc6956f6ec9efe421e7d023db24687b127291318
> > > # Parent  856a1794f165a93ec8eb6fe1963b0ca1a68c647b
> > > pycompat: use unicode literals to appease Python 3
> > >
> > > More of the same.
> > >
> > > diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> > > --- a/mercurial/pycompat.py
> > > +++ b/mercurial/pycompat.py
> > > @@ -29,17 +29,17 @@ class _pycompatstub(object):
> > >      pass
> > >
> > >  def _alias(alias, origin, items):
> > >      """ populate a _pycompatstub
> > >
> > >      copies items from origin to alias
> > >      """
> > >      def hgcase(item):
> > > -        return item.replace('_', '').lower()
> > > +        return item.replace(u'_', u'').lower()
> > >      for item in items:
> > >          try:
> > >              setattr(alias, hgcase(item), getattr(origin, item))
> >
> > Can we have bytes setattr() and getattr() and rewrite them by importer?
> >
> 
> In theory, yes.
> 
> We could also likely scan for string literals passed to inline getattr()
> and setattr() and transform to u'' by the importer. That would remove the
> run-time penalty of calling an extra function. Although it wouldn't find
> the more complicated callers of getattr() and setattr() such as in this
> file.
> 
> > getattr() is frequently used. Converting all arguments to unicode would be
> > a pain and likely to cause unicode bugs.
> 
> I'm not sure what unicode bugs this would hit as hitting a unicode bug
> would require a non-ascii attribute name and I don't think we have any of
> those.

This getattr() is okay as it's obvious that the unicode object is passed only
to getattr(). That is sometimes unclear. For example, wireproto keeps a list
of calls, which is fed to getattr(x, name). And if we had
ui.debug('calling %s\n' % name), exception would be raised.


More information about the Mercurial-devel mailing list