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

Class _ProxyFile

Lib/mailbox.py:2005–2098  ·  view source on GitHub ↗

A read-only wrapper of a file.

Source from the content-addressed store, hash-verified

2003
2004
2005class _ProxyFile:
2006 """A read-only wrapper of a file."""
2007
2008 def __init__(self, f, pos=None):
2009 """Initialize a _ProxyFile."""
2010 self._file = f
2011 if pos is None:
2012 self._pos = f.tell()
2013 else:
2014 self._pos = pos
2015
2016 def read(self, size=None):
2017 """Read bytes."""
2018 return self._read(size, self._file.read)
2019
2020 def read1(self, size=None):
2021 """Read bytes."""
2022 return self._read(size, self._file.read1)
2023
2024 def readline(self, size=None):
2025 """Read a line."""
2026 return self._read(size, self._file.readline)
2027
2028 def readlines(self, sizehint=None):
2029 """Read multiple lines."""
2030 result = []
2031 for line in self:
2032 result.append(line)
2033 if sizehint is not None:
2034 sizehint -= len(line)
2035 if sizehint <= 0:
2036 break
2037 return result
2038
2039 def __iter__(self):
2040 """Iterate over lines."""
2041 while line := self.readline():
2042 yield line
2043
2044 def tell(self):
2045 """Return the position."""
2046 return self._pos
2047
2048 def seek(self, offset, whence=0):
2049 """Change position."""
2050 if whence == 1:
2051 self._file.seek(self._pos)
2052 self._file.seek(offset, whence)
2053 self._pos = self._file.tell()
2054
2055 def close(self):
2056 """Close the file."""
2057 if hasattr(self, '_file'):
2058 try:
2059 if hasattr(self._file, 'close'):
2060 self._file.close()
2061 finally:
2062 del self._file

Callers 2

get_fileMethod · 0.85
get_fileMethod · 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…