Patchwork [3,of,9] patch: use `iter(callable, sentinel)` instead of while True

login
register
mail settings
Submitter Augie Fackler
Date Aug. 6, 2016, 3:02 p.m.
Message ID <c2fd50f63ebfefc68335.1470495753@imladris.local>
Download mbox | patch
Permalink /patch/16157/
State Accepted
Headers show

Comments

Augie Fackler - Aug. 6, 2016, 3:02 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1470420014 14400
#      Fri Aug 05 14:00:14 2016 -0400
# Node ID c2fd50f63ebfefc68335cf8b5eed78c58cb15ee2
# Parent  1ae7c430ce625c90317d4b7d36e41c97023b00b3
patch: use `iter(callable, sentinel)` instead of while True

This is functionally equivalent, but is a little more concise.

Patch

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -410,11 +410,7 @@  class linereader(object):
         return self.fp.readline()
 
     def __iter__(self):
-        while True:
-            l = self.readline()
-            if not l:
-                break
-            yield l
+        return iter(self.readline, '')
 
 class abstractbackend(object):
     def __init__(self, ui):
@@ -1688,10 +1684,7 @@  def scanpatch(fp):
     def scanwhile(first, p):
         """scan lr while predicate holds"""
         lines = [first]
-        while True:
-            line = lr.readline()
-            if not line:
-                break
+        for line in iter(lr.readline, ''):
             if p(line):
                 lines.append(line)
             else:
@@ -1699,10 +1692,7 @@  def scanpatch(fp):
                 break
         return lines
 
-    while True:
-        line = lr.readline()
-        if not line:
-            break
+    for line in iter(lr.readline, ''):
         if line.startswith('diff --git a/') or line.startswith('diff -r '):
             def notheader(line):
                 s = line.split(None, 1)
@@ -1772,10 +1762,7 @@  def iterhunks(fp):
     context = None
     lr = linereader(fp)
 
-    while True:
-        x = lr.readline()
-        if not x:
-            break
+    for x in iter(lr.readline, ''):
         if state == BFILE and (
             (not context and x[0] == '@')
             or (context is not False and x.startswith('***************'))