1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/src/pip/_vendor/cachecontrol/filewrapper.py

81 lines
2.5 KiB
Python
Raw Normal View History

2014-04-24 13:20:51 +02:00
from io import BytesIO
class CallbackFileWrapper(object):
"""
Small wrapper around a fp object which will tee everything read into a
buffer, and when that file is closed it will execute a callback with the
contents of that buffer.
All attributes are proxied to the underlying file object.
This class uses members with a double underscore (__) leading prefix so as
not to accidentally shadow an attribute.
"""
def __init__(self, fp, callback):
self.__buf = BytesIO()
self.__fp = fp
self.__callback = callback
def __getattr__(self, name):
2014-09-17 18:55:11 +02:00
# The vaguaries of garbage collection means that self.__fp is
# not always set. By using __getattribute__ and the private
# name[0] allows looking up the attribute value and raising an
# AttributeError when it doesn't exist. This stop thigns from
# infinitely recursing calls to getattr in the case where
# self.__fp hasn't been set.
#
# [0] https://docs.python.org/2/reference/expressions.html#atom-identifiers
2018-06-21 14:59:26 +02:00
fp = self.__getattribute__("_CallbackFileWrapper__fp")
2014-09-17 18:55:11 +02:00
return getattr(fp, name)
2014-04-24 13:20:51 +02:00
2014-09-10 21:25:44 +02:00
def __is_fp_closed(self):
try:
return self.__fp.fp is None
2018-06-21 14:59:26 +02:00
2014-09-10 21:25:44 +02:00
except AttributeError:
pass
try:
return self.__fp.closed
2018-06-21 14:59:26 +02:00
2014-09-10 21:25:44 +02:00
except AttributeError:
pass
# We just don't cache it then.
# TODO: Add some logging here...
return False
def _close(self):
if self.__callback:
self.__callback(self.__buf.getvalue())
# We assign this to None here, because otherwise we can get into
# really tricky problems where the CPython interpreter dead locks
# because the callback is holding a reference to something which
# has a __del__ method. Setting this to None breaks the cycle
# and allows the garbage collector to do it's thing normally.
self.__callback = None
2014-04-24 13:20:51 +02:00
def read(self, amt=None):
data = self.__fp.read(amt)
self.__buf.write(data)
if self.__is_fp_closed():
self._close()
2014-04-24 13:20:51 +02:00
return data
def _safe_read(self, amt):
data = self.__fp._safe_read(amt)
2018-06-21 14:59:26 +02:00
if amt == 2 and data == b"\r\n":
# urllib executes this read to toss the CRLF at the end
# of the chunk.
return data
self.__buf.write(data)
2014-09-10 21:25:44 +02:00
if self.__is_fp_closed():
self._close()
2014-04-24 13:20:51 +02:00
return data