comparison mercurial/utils/cborutil.py @ 52669:e627cc25b6f3

pyupgrade: rewrite `yield` statements in a loop to `yield from` This is the `legacy` fixer in `pyupgrade`, with the `yield` statement yielding loop commented back in. This seems to help pytype in some cases, and hurt it in others. But that can be manually fixed later. Note that it's possibly buggy in that it aggressively changed `import-checker.py` to `yield from 'fcntl', 'grp', 'pwd', 'select', 'termios': # Unix only`, which is invalid syntax. Possibly it needed help from the token fixer that I've disabled locally (because that wants to make a bunch of unrelated changes). Just change those few places to yield from a list, to avoid having to constantly revert that.
author Matt Harbison <matt_harbison@yahoo.com>
date Sun, 05 Jan 2025 22:26:16 -0500
parents fb8932685031
children 279e217d6041
comparison
equal deleted inserted replaced
52668:5cc8deb96b48 52669:e627cc25b6f3
135 """Encode a known size iterable to an array.""" 135 """Encode a known size iterable to an array."""
136 136
137 yield encodelength(MAJOR_TYPE_ARRAY, len(l)) 137 yield encodelength(MAJOR_TYPE_ARRAY, len(l))
138 138
139 for i in l: 139 for i in l:
140 for chunk in streamencode(i): 140 yield from streamencode(i)
141 yield chunk
142 141
143 142
144 def streamencodearrayfromiter(it): 143 def streamencodearrayfromiter(it):
145 """Encode an iterator of items to an indefinite length array.""" 144 """Encode an iterator of items to an indefinite length array."""
146 145
147 yield BEGIN_INDEFINITE_ARRAY 146 yield BEGIN_INDEFINITE_ARRAY
148 147
149 for i in it: 148 for i in it:
150 for chunk in streamencode(i): 149 yield from streamencode(i)
151 yield chunk
152 150
153 yield BREAK 151 yield BREAK
154 152
155 153
156 def _mixedtypesortkey(v): 154 def _mixedtypesortkey(v):
160 def streamencodeset(s): 158 def streamencodeset(s):
161 # https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml defines 159 # https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml defines
162 # semantic tag 258 for finite sets. 160 # semantic tag 258 for finite sets.
163 yield encodelength(MAJOR_TYPE_SEMANTIC, SEMANTIC_TAG_FINITE_SET) 161 yield encodelength(MAJOR_TYPE_SEMANTIC, SEMANTIC_TAG_FINITE_SET)
164 162
165 for chunk in streamencodearray(sorted(s, key=_mixedtypesortkey)): 163 yield from streamencodearray(sorted(s, key=_mixedtypesortkey))
166 yield chunk
167 164
168 165
169 def streamencodemap(d): 166 def streamencodemap(d):
170 """Encode dictionary to a generator. 167 """Encode dictionary to a generator.
171 168
172 Does not supporting indefinite length dictionaries. 169 Does not supporting indefinite length dictionaries.
173 """ 170 """
174 yield encodelength(MAJOR_TYPE_MAP, len(d)) 171 yield encodelength(MAJOR_TYPE_MAP, len(d))
175 172
176 for key, value in sorted(d.items(), key=lambda x: _mixedtypesortkey(x[0])): 173 for key, value in sorted(d.items(), key=lambda x: _mixedtypesortkey(x[0])):
177 for chunk in streamencode(key): 174 yield from streamencode(key)
178 yield chunk 175 yield from streamencode(value)
179 for chunk in streamencode(value):
180 yield chunk
181 176
182 177
183 def streamencodemapfromiter(it): 178 def streamencodemapfromiter(it):
184 """Given an iterable of (key, value), encode to an indefinite length map.""" 179 """Given an iterable of (key, value), encode to an indefinite length map."""
185 yield BEGIN_INDEFINITE_MAP 180 yield BEGIN_INDEFINITE_MAP
186 181
187 for key, value in it: 182 for key, value in it:
188 for chunk in streamencode(key): 183 yield from streamencode(key)
189 yield chunk 184 yield from streamencode(value)
190 for chunk in streamencode(value):
191 yield chunk
192 185
193 yield BREAK 186 yield BREAK
194 187
195 188
196 def streamencodebool(b): 189 def streamencodebool(b):