| 148 | """ |
| 149 | |
| 150 | def __init__(self, fp, strict=True): |
| 151 | if strict: |
| 152 | valid = lambda f: ( |
| 153 | isinstance(f, (io.BufferedIOBase, io.RawIOBase)) |
| 154 | and f.seekable() and f.readable()) |
| 155 | else: |
| 156 | valid = lambda f: all([ |
| 157 | hasattr(f, 'readinto'), hasattr(f, 'seek'), hasattr(f, 'tell')]) |
| 158 | |
| 159 | if not valid(fp): |
| 160 | raise TypeError("expected file-like, seekable, readable object") |
| 161 | |
| 162 | # store file size, assuming it won't change |
| 163 | self._size = fp.seek(0, os.SEEK_END) |
| 164 | if self._size is None: |
| 165 | # handle non-python3 and/or non-standard file seek() impl. |
| 166 | # (like tempfile.SpooledTemporaryFile) |
| 167 | # note: not thread-safe! |
| 168 | self._size = fp.tell() |
| 169 | |
| 170 | self._fp = fp |
| 171 | |
| 172 | # multiple threads will be accessing the underlying file |
| 173 | self._lock = threading.RLock() |
| 174 | |
| 175 | def __len__(self): |
| 176 | return self._size |