comparison mercurial/utils/cborutil.py @ 37898:2ae6a3134362

cborutil: port to Python 3 The only problem lurking in here was sorts of mismatched types. The sorts are only for output stability in our tests (sigh), so we just build a phony sort key using the __name__ of types so that we only compare like types against each other. By pure luck, my awful sort key matches the behavior we get "for free" in Python 2, so no test output changes. Differential Revision: https://phab.mercurial-scm.org/D3504
author Augie Fackler <augie@google.com>
date Fri, 27 Apr 2018 11:06:49 -0400
parents 65a23cc8e75b
children aeb551a3bb8a
comparison
equal deleted inserted replaced
37897:2efefde3af70 37898:2ae6a3134362
138 for chunk in streamencode(i): 138 for chunk in streamencode(i):
139 yield chunk 139 yield chunk
140 140
141 yield BREAK 141 yield BREAK
142 142
143 def _mixedtypesortkey(v):
144 return type(v).__name__, v
145
143 def streamencodeset(s): 146 def streamencodeset(s):
144 # https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml defines 147 # https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml defines
145 # semantic tag 258 for finite sets. 148 # semantic tag 258 for finite sets.
146 yield encodelength(MAJOR_TYPE_SEMANTIC, 258) 149 yield encodelength(MAJOR_TYPE_SEMANTIC, 258)
147 150
148 for chunk in streamencodearray(sorted(s)): 151 for chunk in streamencodearray(sorted(s, key=_mixedtypesortkey)):
149 yield chunk 152 yield chunk
150 153
151 def streamencodemap(d): 154 def streamencodemap(d):
152 """Encode dictionary to a generator. 155 """Encode dictionary to a generator.
153 156
154 Does not supporting indefinite length dictionaries. 157 Does not supporting indefinite length dictionaries.
155 """ 158 """
156 yield encodelength(MAJOR_TYPE_MAP, len(d)) 159 yield encodelength(MAJOR_TYPE_MAP, len(d))
157 160
158 for key, value in sorted(d.iteritems()): 161 for key, value in sorted(d.iteritems(),
162 key=lambda x: _mixedtypesortkey(x[0])):
159 for chunk in streamencode(key): 163 for chunk in streamencode(key):
160 yield chunk 164 yield chunk
161 for chunk in streamencode(value): 165 for chunk in streamencode(value):
162 yield chunk 166 yield chunk
163 167