A read-only wrapper of part of a file.
| 2099 | |
| 2100 | |
| 2101 | class _PartialFile(_ProxyFile): |
| 2102 | """A read-only wrapper of part of a file.""" |
| 2103 | |
| 2104 | def __init__(self, f, start=None, stop=None): |
| 2105 | """Initialize a _PartialFile.""" |
| 2106 | _ProxyFile.__init__(self, f, start) |
| 2107 | self._start = start |
| 2108 | self._stop = stop |
| 2109 | |
| 2110 | def tell(self): |
| 2111 | """Return the position with respect to start.""" |
| 2112 | return _ProxyFile.tell(self) - self._start |
| 2113 | |
| 2114 | def seek(self, offset, whence=0): |
| 2115 | """Change position, possibly with respect to start or stop.""" |
| 2116 | if whence == 0: |
| 2117 | self._pos = self._start |
| 2118 | whence = 1 |
| 2119 | elif whence == 2: |
| 2120 | self._pos = self._stop |
| 2121 | whence = 1 |
| 2122 | _ProxyFile.seek(self, offset, whence) |
| 2123 | |
| 2124 | def _read(self, size, read_method): |
| 2125 | """Read size bytes using read_method, honoring start and stop.""" |
| 2126 | remaining = self._stop - self._pos |
| 2127 | if remaining <= 0: |
| 2128 | return b'' |
| 2129 | if size is None or size < 0 or size > remaining: |
| 2130 | size = remaining |
| 2131 | return _ProxyFile._read(self, size, read_method) |
| 2132 | |
| 2133 | def close(self): |
| 2134 | # do *not* close the underlying file object for partial files, |
| 2135 | # since it's global to the mailbox object |
| 2136 | if hasattr(self, '_file'): |
| 2137 | del self._file |
| 2138 | |
| 2139 | |
| 2140 | def _lock_file(f, dotlock=True): |
no outgoing calls
no test coverage detected
searching dependent graphs…