aboutsummaryrefslogtreecommitdiff
path: root/urllib3/util/response.py
diff options
context:
space:
mode:
Diffstat (limited to 'urllib3/util/response.py')
-rw-r--r--urllib3/util/response.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/urllib3/util/response.py b/urllib3/util/response.py
new file mode 100644
index 0000000..45fff55
--- /dev/null
+++ b/urllib3/util/response.py
@@ -0,0 +1,22 @@
+def is_fp_closed(obj):
+ """
+ Checks whether a given file-like object is closed.
+
+ :param obj:
+ The file-like object to check.
+ """
+
+ try:
+ # Check via the official file-like-object way.
+ return obj.closed
+ except AttributeError:
+ pass
+
+ try:
+ # Check if the object is a container for another file-like object that
+ # gets released on exhaustion (e.g. HTTPResponse).
+ return obj.fp is None
+ except AttributeError:
+ pass
+
+ raise ValueError("Unable to determine whether fp is closed.")