Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/patch.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 | 5cc8deb96b48 |
children | 279e217d6041 |
comparison
equal
deleted
inserted
replaced
52668:5cc8deb96b48 | 52669:e627cc25b6f3 |
---|---|
92 yield chunk(cur) | 92 yield chunk(cur) |
93 | 93 |
94 def mboxsplit(stream, cur): | 94 def mboxsplit(stream, cur): |
95 for line in stream: | 95 for line in stream: |
96 if line.startswith(b'From '): | 96 if line.startswith(b'From '): |
97 for c in split(chunk(cur[1:])): | 97 yield from split(chunk(cur[1:])) |
98 yield c | |
99 cur = [] | 98 cur = [] |
100 | 99 |
101 cur.append(line) | 100 cur.append(line) |
102 | 101 |
103 if cur: | 102 if cur: |
104 for c in split(chunk(cur[1:])): | 103 yield from split(chunk(cur[1:])) |
105 yield c | |
106 | 104 |
107 def mimesplit(stream, cur): | 105 def mimesplit(stream, cur): |
108 def msgfp(m): | 106 def msgfp(m): |
109 fp = stringio() | 107 fp = stringio() |
110 # pytype: disable=wrong-arg-types | 108 # pytype: disable=wrong-arg-types |
2735 b += line[1:] | 2733 b += line[1:] |
2736 else: | 2734 else: |
2737 raise error.ProgrammingError(b'unexpected hunk line: %s' % line) | 2735 raise error.ProgrammingError(b'unexpected hunk line: %s' % line) |
2738 # fast path: if either side is empty, use diffsinglehunk | 2736 # fast path: if either side is empty, use diffsinglehunk |
2739 if not a or not b: | 2737 if not a or not b: |
2740 for t in diffsinglehunk(hunklines): | 2738 yield from diffsinglehunk(hunklines) |
2741 yield t | |
2742 return | 2739 return |
2743 # re-split the content into words | 2740 # re-split the content into words |
2744 al = wordsplitter.findall(bytes(a)) | 2741 al = wordsplitter.findall(bytes(a)) |
2745 bl = wordsplitter.findall(bytes(b)) | 2742 bl = wordsplitter.findall(bytes(b)) |
2746 # re-arrange the words to lines since the diff algorithm is line-based | 2743 # re-arrange the words to lines since the diff algorithm is line-based |
2822 # buffers a hunk, i.e. adjacent "-", "+" lines without other changes. | 2819 # buffers a hunk, i.e. adjacent "-", "+" lines without other changes. |
2823 hunkbuffer = [] | 2820 hunkbuffer = [] |
2824 | 2821 |
2825 def consumehunkbuffer(): | 2822 def consumehunkbuffer(): |
2826 if hunkbuffer: | 2823 if hunkbuffer: |
2827 for token in dodiffhunk(hunkbuffer): | 2824 yield from dodiffhunk(hunkbuffer) |
2828 yield token | |
2829 hunkbuffer[:] = [] | 2825 hunkbuffer[:] = [] |
2830 | 2826 |
2831 for chunk in func(*args, **kw): | 2827 for chunk in func(*args, **kw): |
2832 lines = chunk.split(b'\n') | 2828 lines = chunk.split(b'\n') |
2833 linecount = len(lines) | 2829 linecount = len(lines) |
2853 if i + 1 < linecount: | 2849 if i + 1 < linecount: |
2854 bufferedline += b"\n" | 2850 bufferedline += b"\n" |
2855 hunkbuffer.append(bufferedline) | 2851 hunkbuffer.append(bufferedline) |
2856 else: | 2852 else: |
2857 # unbuffered | 2853 # unbuffered |
2858 for token in consumehunkbuffer(): | 2854 yield from consumehunkbuffer() |
2859 yield token | |
2860 stripline = line.rstrip() | 2855 stripline = line.rstrip() |
2861 for prefix, label in prefixes: | 2856 for prefix, label in prefixes: |
2862 if stripline.startswith(prefix): | 2857 if stripline.startswith(prefix): |
2863 yield (stripline, label) | 2858 yield (stripline, label) |
2864 if line != stripline: | 2859 if line != stripline: |
2869 break | 2864 break |
2870 else: | 2865 else: |
2871 yield (line, b'') | 2866 yield (line, b'') |
2872 if i + 1 < linecount: | 2867 if i + 1 < linecount: |
2873 yield (b'\n', b'') | 2868 yield (b'\n', b'') |
2874 for token in consumehunkbuffer(): | 2869 yield from consumehunkbuffer() |
2875 yield token | |
2876 | 2870 |
2877 | 2871 |
2878 def diffui(*args, **kw): | 2872 def diffui(*args, **kw): |
2879 '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' | 2873 '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' |
2880 return difflabel(diff, *args, **kw) | 2874 return difflabel(diff, *args, **kw) |