From 032271a030a6bbe39d22e9ebadf2a86ddf3b6705 Mon Sep 17 00:00:00 2001 From: Benno Rice Date: Wed, 24 May 2017 21:10:09 -0700 Subject: [PATCH] Empty sequences are false, there's no need to check the len. --- gunicorn/arbiter.py | 2 +- gunicorn/http/body.py | 4 ++-- gunicorn/http/message.py | 4 ++-- gunicorn/http/unreader.py | 2 +- gunicorn/instrument/statsd.py | 2 +- tests/treq.py | 20 ++++++++++---------- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index a2fe4bd1..7fca128a 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -205,7 +205,7 @@ class Arbiter(object): while True: self.maybe_promote_master() - sig = self.SIG_QUEUE.pop(0) if len(self.SIG_QUEUE) else None + sig = self.SIG_QUEUE.pop(0, None) if sig is None: self.sleep() self.murder_workers() diff --git a/gunicorn/http/body.py b/gunicorn/http/body.py index e00d924d..fb8633ed 100644 --- a/gunicorn/http/body.py +++ b/gunicorn/http/body.py @@ -210,7 +210,7 @@ class Body(object): while size > self.buf.tell(): data = self.reader.read(1024) - if not len(data): + if not data: break self.buf.write(data) @@ -248,7 +248,7 @@ class Body(object): def readlines(self, size=None): ret = [] data = self.read() - while len(data): + while data: pos = data.find(b"\n") if pos < 0: ret.append(data) diff --git a/gunicorn/http/message.py b/gunicorn/http/message.py index 0c83b89f..2d53b54a 100644 --- a/gunicorn/http/message.py +++ b/gunicorn/http/message.py @@ -64,7 +64,7 @@ class Message(object): # Parse headers into key/value pairs paying attention # to continuation lines. - while len(lines): + while lines: if len(headers) >= self.limit_request_fields: raise LimitRequestHeaders("limit request headers fields") @@ -81,7 +81,7 @@ class Message(object): name, value = name.strip(), [value.lstrip()] # Consume value continuation lines - while len(lines) and lines[0].startswith((" ", "\t")): + while lines and lines[0].startswith((" ", "\t")): curr = lines.pop(0) header_length += len(curr) if header_length > self.limit_request_field_size > 0: diff --git a/gunicorn/http/unreader.py b/gunicorn/http/unreader.py index d7e411a4..9f312a80 100644 --- a/gunicorn/http/unreader.py +++ b/gunicorn/http/unreader.py @@ -40,7 +40,7 @@ class Unreader(object): while self.buf.tell() < size: chunk = self.chunk() - if not len(chunk): + if not chunk: ret = self.buf.getvalue() self.buf = six.BytesIO() return ret diff --git a/gunicorn/instrument/statsd.py b/gunicorn/instrument/statsd.py index 89aa8c2d..1efe77a7 100644 --- a/gunicorn/instrument/statsd.py +++ b/gunicorn/instrument/statsd.py @@ -80,7 +80,7 @@ class Statsd(Logger): pass # Log to parent logger only if there is something to say - if msg is not None and len(msg) > 0: + if msg: Logger.log(self, lvl, msg, *args, **kwargs) except Exception: Logger.warning(self, "Failed to log to statsd", exc_info=True) diff --git a/tests/treq.py b/tests/treq.py index 163664c4..efebd80c 100644 --- a/tests/treq.py +++ b/tests/treq.py @@ -74,7 +74,7 @@ class request(object): yield lines[:pos+2] lines = lines[pos+2:] pos = lines.find(b"\r\n") - if len(lines): + if lines: yield lines def send_bytes(self): @@ -137,7 +137,7 @@ class request(object): def match_read(self, req, body, sizes): data = self.szread(req.body.read, sizes) count = 1000 - while len(body): + while body: if body[:len(data)] != data: raise AssertionError("Invalid body data read: %r != %r" % ( data, body[:len(data)])) @@ -148,9 +148,9 @@ class request(object): if count <= 0: raise AssertionError("Unexpected apparent EOF") - if len(body): + if body: raise AssertionError("Failed to read entire body: %r" % body) - elif len(data): + elif data: raise AssertionError("Read beyond expected body: %r" % data) data = req.body.read(sizes()) if data: @@ -159,7 +159,7 @@ class request(object): def match_readline(self, req, body, sizes): data = self.szread(req.body.readline, sizes) count = 1000 - while len(body): + while body: if body[:len(data)] != data: raise AssertionError("Invalid data read: %r" % data) if b'\n' in data[:-1]: @@ -170,9 +170,9 @@ class request(object): count -= 1 if count <= 0: raise AssertionError("Apparent unexpected EOF") - if len(body): + if body: raise AssertionError("Failed to read entire body: %r" % body) - elif len(data): + elif data: raise AssertionError("Read beyond expected body: %r" % data) data = req.body.readline(sizes()) if data: @@ -190,7 +190,7 @@ class request(object): raise AssertionError("Invalid body data read: %r != %r" % ( line, body[:len(line)])) body = body[len(line):] - if len(body): + if body: raise AssertionError("Failed to read entire body: %r" % body) data = req.body.readlines(sizes()) if data: @@ -207,7 +207,7 @@ class request(object): raise AssertionError("Invalid body data read: %r != %r" % ( line, body[:len(line)])) body = body[len(line):] - if len(body): + if body: raise AssertionError("Failed to read entire body: %r" % body) try: data = six.next(iter(req.body)) @@ -254,7 +254,7 @@ class request(object): p = RequestParser(cfg, sender()) for req in p: self.same(req, sizer, matcher, cases.pop(0)) - assert len(cases) == 0 + assert not cases def same(self, req, sizer, matcher, exp): assert req.method == exp["method"]