From be6834fe527823a64269c5ab1107af4c97882a44 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Thu, 2 Apr 2020 22:35:04 +0200 Subject: avoid changing variable types The mypy type checker deals badly when a binding changes its type. To ease porting to mypy, avoid changing the type of variables. In some cases, variables can be eliminated. In other cases, they are renamed. --- wsgitools/filters.py | 3 +-- wsgitools/middlewares.py | 6 +++--- wsgitools/scgi/asynchronous.py | 4 ++-- wsgitools/scgi/forkpool.py | 18 +++++++++--------- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/wsgitools/filters.py b/wsgitools/filters.py index 60e4ebb..2a97066 100644 --- a/wsgitools/filters.py +++ b/wsgitools/filters.py @@ -388,8 +388,7 @@ class GzipWSGIFilter(BaseWSGIFilter): assert isinstance(environ, dict) if "HTTP_ACCEPT_ENCODING" in environ: acceptenc = environ["HTTP_ACCEPT_ENCODING"].split(',') - acceptenc = map(str.strip, acceptenc) - if "gzip" in acceptenc: + if "gzip" in map(str.strip, acceptenc): self.compress = True self.sio = io.BytesIO() self.gzip = gzip.GzipFile(fileobj=self.sio, mode="w") diff --git a/wsgitools/middlewares.py b/wsgitools/middlewares.py index 1cb5622..3e48ee0 100644 --- a/wsgitools/middlewares.py +++ b/wsgitools/middlewares.py @@ -354,12 +354,12 @@ class BasicAuthMiddleware(AuthenticationMiddleware): def authenticate(self, auth, environ): assert isinstance(auth, str) assert isinstance(environ, dict) - auth = str2bytes(auth) + authb = str2bytes(auth) try: - auth_info = base64.b64decode(auth) + auth_infob = base64.b64decode(authb) except TypeError: raise ProtocolViolation("failed to base64 decode auth_info") - auth_info = bytes2str(auth_info) + auth_info = bytes2str(auth_infob) try: username, password = auth_info.split(':', 1) except ValueError: diff --git a/wsgitools/scgi/asynchronous.py b/wsgitools/scgi/asynchronous.py index 80b5803..15594c9 100644 --- a/wsgitools/scgi/asynchronous.py +++ b/wsgitools/scgi/asynchronous.py @@ -77,9 +77,9 @@ class SCGIConnection(asyncore.dispatcher): self.inbuff += data if self.state == SCGIConnection.NEW: if b':' in self.inbuff: - reqlen, self.inbuff = self.inbuff.split(b':', 1) + reqlenb, self.inbuff = self.inbuff.split(b':', 1) try: - reqlen = int(reqlen) + reqlen = int(reqlenb) except ValueError: # invalid request format self.close() return diff --git a/wsgitools/scgi/forkpool.py b/wsgitools/scgi/forkpool.py index 361fb40..f3990cb 100644 --- a/wsgitools/scgi/forkpool.py +++ b/wsgitools/scgi/forkpool.py @@ -414,9 +414,9 @@ class SCGIServer(object): if not b':' in data: con.close() return - length, data = data.split(b':', 1) + lengthb, data = data.split(b':', 1) try: - length = int(length) + length = int(lengthb) except ValueError: # clear protocol violation con.close() return @@ -433,17 +433,17 @@ class SCGIServer(object): data += t # netstrings! - data = data.split(b'\0') + strings = data.split(b'\0') # the byte beyond has to be a ','. # and the number of netstrings excluding the final ',' has to be even - if data.pop() != b',' or len(data) % 2 != 0: + if strings.pop() != b',' or len(strings) % 2 != 0: con.close() return environ = self.config.copy() - while data: - key = bytes2str(data.pop(0)) - value = bytes2str(data.pop(0)) + while strings: + key = bytes2str(strings.pop(0)) + value = bytes2str(strings.pop(0)) environ[key] = value # elements: @@ -480,8 +480,8 @@ class SCGIServer(object): assert all(isinstance(k, str) and isinstance(v, str) for (k, v) in headers) assert not response_head[0] # unset or not sent - headers = "".join(map("%s: %s\r\n".__mod__, headers)) - full_header = "Status: %s\r\n%s\r\n" % (status, headers) + full_header = "Status: %s\r\n%s\r\n" % \ + (status, "".join(map("%s: %s\r\n".__mod__, headers))) response_head[1] = str2bytes(full_header) response_head[0] = False # set but nothing sent return dumbsend -- cgit v1.2.3