From fc5447994446ee31824be0c816fe884b58c1219c Mon Sep 17 00:00:00 2001 From: benoitc Date: Thu, 31 Dec 2015 14:48:19 +0100 Subject: [PATCH 1/2] reuse util.is_fileobject is_fileobject usgae was removed due to the use of the `tell` method check. This change remove this check wich allows us to completely test if fileno() is usable. Also it handle most of the exceptions around created by breaking changes across Python versions. Hopefully we are good now. fix #1174 --- gunicorn/http/wsgi.py | 4 +--- gunicorn/util.py | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index 1d48467d12..00b8f74fe8 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -353,9 +353,7 @@ def sendfile(self, respiter): if self.cfg.is_ssl or not self.can_sendfile(): return False - try: - fileno = respiter.filelike.fileno() - except AttributeError: + if not util.is_fileobject(respiter.filelike): return False try: diff --git a/gunicorn/util.py b/gunicorn/util.py index ac2ca769e1..a8875d90e5 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -511,13 +511,13 @@ def to_bytestring(value, encoding="utf8"): return value.encode(encoding) def is_fileobject(obj): - if not hasattr(obj, "tell") or not hasattr(obj, "fileno"): + if not hasattr(obj, "fileno"): return False # check BytesIO case and maybe others try: obj.fileno() - except (IOError, io.UnsupportedOperation): + except (AttributeError, IOError, io.UnsupportedOperation): return False return True From 02f750637496bba1a46136e1b794e5e410c91072 Mon Sep 17 00:00:00 2001 From: benoitc Date: Thu, 31 Dec 2015 15:32:08 +0100 Subject: [PATCH 2/2] rename util.is_fileobject to util.has_fileno be more descriptive --- gunicorn/http/wsgi.py | 2 +- gunicorn/util.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index 00b8f74fe8..6ca3b4fb7b 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -353,7 +353,7 @@ def sendfile(self, respiter): if self.cfg.is_ssl or not self.can_sendfile(): return False - if not util.is_fileobject(respiter.filelike): + if not util.has_fileno(respiter.filelike): return False try: diff --git a/gunicorn/util.py b/gunicorn/util.py index a8875d90e5..896a22141a 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -510,7 +510,7 @@ def to_bytestring(value, encoding="utf8"): return value.encode(encoding) -def is_fileobject(obj): +def has_fileno(obj): if not hasattr(obj, "fileno"): return False