Mercurial > hg
comparison mercurial/util.py @ 43907:68af0228fedc
util: implement sortdict.insert()
As flagged by pytype (reported via Matt Harbison, thanks). This was
broken by bd0fd3ff9916 (util: rewrite sortdict using Python 2.7's
OrderedDict, 2017-05-16). We actually call insert() on
namespaces.py:100, but we clearly don't have test coverage of that an
no users have reported it AFAIK.
Differential Revision: https://phab.mercurial-scm.org/D7680
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Mon, 16 Dec 2019 15:58:47 -0800 |
parents | 09bcbeacedc7 |
children | 4222b9d5d4fb |
comparison
equal
deleted
inserted
replaced
43906:727cf6acadfe | 43907:68af0228fedc |
---|---|
1251 >>> d2 | 1251 >>> d2 |
1252 sortdict([('a', 0), ('b', 1)]) | 1252 sortdict([('a', 0), ('b', 1)]) |
1253 >>> d2.update([(b'a', 2)]) | 1253 >>> d2.update([(b'a', 2)]) |
1254 >>> list(d2.keys()) # should still be in last-set order | 1254 >>> list(d2.keys()) # should still be in last-set order |
1255 ['b', 'a'] | 1255 ['b', 'a'] |
1256 >>> d1.insert(1, b'a.5', 0.5) | |
1257 >>> d1 | |
1258 sortdict([('a', 0), ('a.5', 0.5), ('b', 1)]) | |
1256 ''' | 1259 ''' |
1257 | 1260 |
1258 def __setitem__(self, key, value): | 1261 def __setitem__(self, key, value): |
1259 if key in self: | 1262 if key in self: |
1260 del self[key] | 1263 del self[key] |
1264 # __setitem__() isn't called as of PyPy 5.8.0 | 1267 # __setitem__() isn't called as of PyPy 5.8.0 |
1265 def update(self, src): | 1268 def update(self, src): |
1266 if isinstance(src, dict): | 1269 if isinstance(src, dict): |
1267 src = pycompat.iteritems(src) | 1270 src = pycompat.iteritems(src) |
1268 for k, v in src: | 1271 for k, v in src: |
1272 self[k] = v | |
1273 | |
1274 def insert(self, position, key, value): | |
1275 for (i, (k, v)) in enumerate(list(self.items())): | |
1276 if i == position: | |
1277 self[key] = value | |
1278 if i >= position: | |
1279 del self[k] | |
1269 self[k] = v | 1280 self[k] = v |
1270 | 1281 |
1271 | 1282 |
1272 class cowdict(cow, dict): | 1283 class cowdict(cow, dict): |
1273 """copy-on-write dict | 1284 """copy-on-write dict |