[PATCH] util: rewrite sortdict using Python 2.7's OrderedDict

Martin von Zweigbergk martinvonz at google.com
Thu May 18 16:38:20 EDT 2017


On Thu, May 18, 2017 at 1:28 PM, Martin von Zweigbergk
<martinvonz at google.com> wrote:
> On Thu, May 18, 2017 at 1:24 PM, Martin von Zweigbergk
> <martinvonz at google.com> wrote:
>> On Thu, May 18, 2017 at 1:15 PM, Jun Wu <quark at fb.com> wrote:
>>> I got the following error when trying to test Python 3:
>>>
>>>   Traceback (most recent call last):
>>>     File "hg", line 45, in <module>
>>>       mercurial.dispatch.run()
>>>     File "/home/quark/hg-committed/mercurial/dispatch.py", line 83, in run
>>>       status = (dispatch(req) or 0) & 255
>>>     File "/home/quark/hg-committed/mercurial/dispatch.py", line 164, in dispatch
>>>       ret = _runcatch(req)
>>>     File "/home/quark/hg-committed/mercurial/dispatch.py", line 299, in _runcatch
>>>       return _callcatch(ui, _runcatchfunc)
>>>     File "/home/quark/hg-committed/mercurial/dispatch.py", line 307, in _callcatch
>>>       return scmutil.callcatch(ui, func)
>>>     File "/home/quark/hg-committed/mercurial/scmutil.py", line 141, in callcatch
>>>       return func()
>>>     File "/home/quark/hg-committed/mercurial/dispatch.py", line 289, in _runcatchfunc
>>>       return _dispatch(req)
>>>     File "/home/quark/hg-committed/mercurial/dispatch.py", line 746, in _dispatch
>>>       path, lui = _getlocal(ui, rpath)
>>>     File "/home/quark/hg-committed/mercurial/dispatch.py", line 680, in _getlocal
>>>       lui.readconfig(os.path.join(path, ".hg", "hgrc"), path)
>>>     File "/home/quark/hg-committed/mercurial/ui.py", line 336, in readconfig
>>>       self.fixconfig(root=root)
>>>     File "/home/quark/hg-committed/mercurial/ui.py", line 344, in fixconfig
>>>       for n, p in c.items('paths'):
>>
>> Any idea what a 'c' is?
>
> Figured it out: it's an instance of config.config

I'll send a patch. It takes 30 minutes to get delivered.

>
>>
>>>   RuntimeError: OrderedDict mutated during iteration
>>>
>>> Excerpts from Martin von Zweigbergk's message of 2017-05-16 22:08:35 -0700:
>>>> # HG changeset patch
>>>> # User Martin von Zweigbergk <martinvonz at google.com>
>>>> # Date 1494997046 25200
>>>> #      Tue May 16 21:57:26 2017 -0700
>>>> # Node ID eccddf592f672910ea22e13ffa9f5fe9fb757591
>>>> # Parent  779a1ae6d0d9eeb487636f665747e92195eb234e
>>>> util: rewrite sortdict using Python 2.7's OrderedDict
>>>>
>>>> Pattern copied from
>>>> https://docs.python.org/dev/library/collections.html#ordereddict-examples-and-recipes .
>>>>
>>>> diff --git a/mercurial/util.py b/mercurial/util.py
>>>> --- a/mercurial/util.py
>>>> +++ b/mercurial/util.py
>>>> @@ -567,54 +567,14 @@
>>>>
>>>>      return f
>>>>
>>>> -class sortdict(dict):
>>>> +class sortdict(collections.OrderedDict):
>>>>      '''a simple sorted dictionary'''
>>>> -    def __init__(self, data=None):
>>>> -        self._list = []
>>>> -        if data:
>>>> -            self.update(data)
>>>> +    def __setitem__(self, key, value):
>>>> +        if key in self:
>>>> +            del self[key]
>>>> +        collections.OrderedDict.__setitem__(self, key, value)
>>>>      def copy(self):
>>>>          return sortdict(self)
>>>> -    def __setitem__(self, key, val):
>>>> -        if key in self:
>>>> -            self._list.remove(key)
>>>> -        self._list.append(key)
>>>> -        dict.__setitem__(self, key, val)
>>>> -    def __iter__(self):
>>>> -        return self._list.__iter__()
>>>> -    def update(self, src):
>>>> -        if isinstance(src, dict):
>>>> -            src = src.iteritems()
>>>> -        for k, v in src:
>>>> -            self[k] = v
>>>> -    def clear(self):
>>>> -        dict.clear(self)
>>>> -        self._list = []
>>>> -    def items(self):
>>>> -        return [(k, self[k]) for k in self._list]
>>>> -    def __delitem__(self, key):
>>>> -        dict.__delitem__(self, key)
>>>> -        self._list.remove(key)
>>>> -    def pop(self, key, *args, **kwargs):
>>>> -        try:
>>>> -            self._list.remove(key)
>>>> -        except ValueError:
>>>> -            pass
>>>> -        return dict.pop(self, key, *args, **kwargs)
>>>> -    def keys(self):
>>>> -        return self._list[:]
>>>> -    def iterkeys(self):
>>>> -        return self._list.__iter__()
>>>> -    def iteritems(self):
>>>> -        for k in self._list:
>>>> -            yield k, self[k]
>>>> -    def insert(self, index, key, val):
>>>> -        self._list.insert(index, key)
>>>> -        dict.__setitem__(self, key, val)
>>>> -    def __repr__(self):
>>>> -        if not self:
>>>> -            return '%s()' % self.__class__.__name__
>>>> -        return '%s(%r)' % (self.__class__.__name__, self.items())
>>>>
>>>>  class _lrucachenode(object):
>>>>      """A node in a doubly linked list.


More information about the Mercurial-devel mailing list