MCPcopy Index your code
hub / github.com/python/cpython / _PaddedFile

Class _PaddedFile

Lib/gzip.py:83–122  ·  view source on GitHub ↗

Minimal read-only file object that prepends a string to the contents of an actual file. Shouldn't be used outside of gzip.py, as it lacks essential functionality.

Source from the content-addressed store, hash-verified

81 output.write(struct.pack("<L", value))
82
83class _PaddedFile:
84 """Minimal read-only file object that prepends a string to the contents
85 of an actual file. Shouldn&#x27;t be used outside of gzip.py, as it lacks
86 essential functionality."""
87
88 def __init__(self, f, prepend=b''):
89 self._buffer = prepend
90 self._length = len(prepend)
91 self.file = f
92 self._read = 0
93
94 def read(self, size):
95 if self._read is None:
96 return self.file.read(size)
97 if self._read + size <= self._length:
98 read = self._read
99 self._read += size
100 return self._buffer[read:self._read]
101 else:
102 read = self._read
103 self._read = None
104 return self._buffer[read:] + \
105 self.file.read(size-self._length+read)
106
107 def prepend(self, prepend=b''):
108 if self._read is None:
109 self._buffer = prepend
110 else: # Assume data was read since the last prepend() call
111 self._read -= len(prepend)
112 return
113 self._length = len(self._buffer)
114 self._read = 0
115
116 def seek(self, off):
117 self._read = None
118 self._buffer = None
119 return self.file.seek(off)
120
121 def seekable(self):
122 return True # Allows fast-forwarding even in unseekable streams
123
124
125class BadGzipFile(OSError):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…