Mercurial > public > mercurial-scm > hg
comparison mercurial/wireprotoframing.py @ 52644: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 | 5cc8deb96b48 |
children | 1a612f9ec2c4 |
comparison
equal
deleted
inserted
replaced
52643:5cc8deb96b48 | 52644:e627cc25b6f3 |
---|---|
625 If the special input ``None`` is received, flushes all buffered | 625 If the special input ``None`` is received, flushes all buffered |
626 data to frames. | 626 data to frames. |
627 """ | 627 """ |
628 | 628 |
629 if data is None: | 629 if data is None: |
630 for frame in self._flush(): | 630 yield from self._flush() |
631 yield frame | |
632 return | 631 return |
633 | 632 |
634 data = self._stream.encode(data) | 633 data = self._stream.encode(data) |
635 | 634 |
636 # There is a ton of potential to do more complicated things here. | 635 # There is a ton of potential to do more complicated things here. |
647 | 646 |
648 if not data: | 647 if not data: |
649 return | 648 return |
650 | 649 |
651 if len(data) > self._maxsize: | 650 if len(data) > self._maxsize: |
652 for frame in self._flush(): | 651 yield from self._flush() |
653 yield frame | |
654 | 652 |
655 # Now emit frames for the big chunk. | 653 # Now emit frames for the big chunk. |
656 offset = 0 | 654 offset = 0 |
657 while True: | 655 while True: |
658 chunk = data[offset : offset + self._maxsize] | 656 chunk = data[offset : offset + self._maxsize] |
677 return | 675 return |
678 | 676 |
679 # Else flush what we have and buffer the new chunk. We could do | 677 # Else flush what we have and buffer the new chunk. We could do |
680 # something more intelligent here, like break the chunk. Let's | 678 # something more intelligent here, like break the chunk. Let's |
681 # keep things simple for now. | 679 # keep things simple for now. |
682 for frame in self._flush(): | 680 yield from self._flush() |
683 yield frame | |
684 | 681 |
685 self._chunks.append(data) | 682 self._chunks.append(data) |
686 self._chunkssize = len(data) | 683 self._chunkssize = len(data) |
687 | 684 |
688 def _flush(self): | 685 def _flush(self): |
1301 return b'noop', {} | 1298 return b'noop', {} |
1302 | 1299 |
1303 # If we buffered all our responses, emit those. | 1300 # If we buffered all our responses, emit those. |
1304 def makegen(): | 1301 def makegen(): |
1305 for gen in self._bufferedframegens: | 1302 for gen in self._bufferedframegens: |
1306 for frame in gen: | 1303 yield from gen |
1307 yield frame | |
1308 | 1304 |
1309 return b'sendframes', { | 1305 return b'sendframes', { |
1310 b'framegen': makegen(), | 1306 b'framegen': makegen(), |
1311 } | 1307 } |
1312 | 1308 |
1321 | 1317 |
1322 def onservererror(self, stream, requestid, msg): | 1318 def onservererror(self, stream, requestid, msg): |
1323 ensureserverstream(stream) | 1319 ensureserverstream(stream) |
1324 | 1320 |
1325 def sendframes(): | 1321 def sendframes(): |
1326 for frame in createerrorframe( | 1322 yield from createerrorframe( |
1327 stream, requestid, msg, errtype=b'server' | 1323 stream, requestid, msg, errtype=b'server' |
1328 ): | 1324 ) |
1329 yield frame | |
1330 | 1325 |
1331 self._activecommands.remove(requestid) | 1326 self._activecommands.remove(requestid) |
1332 | 1327 |
1333 return self._handlesendframes(sendframes()) | 1328 return self._handlesendframes(sendframes()) |
1334 | 1329 |
1335 def oncommanderror(self, stream, requestid, message, args=None): | 1330 def oncommanderror(self, stream, requestid, message, args=None): |
1336 """Called when a command encountered an error before sending output.""" | 1331 """Called when a command encountered an error before sending output.""" |
1337 ensureserverstream(stream) | 1332 ensureserverstream(stream) |
1338 | 1333 |
1339 def sendframes(): | 1334 def sendframes(): |
1340 for frame in createcommanderrorresponse( | 1335 yield from createcommanderrorresponse( |
1341 stream, requestid, message, args | 1336 stream, requestid, message, args |
1342 ): | 1337 ) |
1343 yield frame | |
1344 | 1338 |
1345 self._activecommands.remove(requestid) | 1339 self._activecommands.remove(requestid) |
1346 | 1340 |
1347 return self._handlesendframes(sendframes()) | 1341 return self._handlesendframes(sendframes()) |
1348 | 1342 |
1854 self._cansend = False | 1848 self._cansend = False |
1855 | 1849 |
1856 def makeframes(): | 1850 def makeframes(): |
1857 while self._pendingrequests: | 1851 while self._pendingrequests: |
1858 request = self._pendingrequests.popleft() | 1852 request = self._pendingrequests.popleft() |
1859 for frame in self._makecommandframes(request): | 1853 yield from self._makecommandframes(request) |
1860 yield frame | |
1861 | 1854 |
1862 return b'sendframes', { | 1855 return b'sendframes', { |
1863 b'framegen': makeframes(), | 1856 b'framegen': makeframes(), |
1864 } | 1857 } |
1865 | 1858 |
1897 request.args, | 1890 request.args, |
1898 datafh=request.datafh, | 1891 datafh=request.datafh, |
1899 redirect=request.redirect, | 1892 redirect=request.redirect, |
1900 ) | 1893 ) |
1901 | 1894 |
1902 for frame in res: | 1895 yield from res |
1903 yield frame | |
1904 | 1896 |
1905 request.state = b'sent' | 1897 request.state = b'sent' |
1906 | 1898 |
1907 def onframerecv(self, frame): | 1899 def onframerecv(self, frame): |
1908 """Process a frame that has been received off the wire. | 1900 """Process a frame that has been received off the wire. |