MCPcopy Index your code
hub / github.com/RustPython/RustPython / _ProxyFile

Class _ProxyFile

Lib/mailbox.py:1998–2093  ·  view source on GitHub ↗

A read-only wrapper of a file.

Source from the content-addressed store, hash-verified

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

Callers 2

get_fileMethod · 0.85
get_fileMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected