diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index 1d48467d1..00b8f74fe 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 ac2ca769e..a8875d90e 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