MCPcopy
hub / github.com/pallets/click / KeepOpenFile

Class KeepOpenFile

src/click/utils.py:207–243  ·  view source on GitHub ↗

Proxy a file object but keep it open across a ``with`` block. Wraps a borrowed file (such as ``sys.stdin`` or ``sys.stdout``) so that leaving a ``with`` block does not close it, as used by :func:`open_file` for the ``-`` filename. The caller stays responsible for the file: an explic

Source from the content-addressed store, hash-verified

205
206
207class KeepOpenFile:
208 """Proxy a file object but keep it open across a ``with`` block.
209
210 Wraps a borrowed file (such as ``sys.stdin`` or ``sys.stdout``) so that
211 leaving a ``with`` block does not close it, as used by :func:`open_file`
212 for the ``-`` filename. The caller stays responsible for the file: an
213 explicit :meth:`close` still passes through to the wrapped object.
214
215 Dunder methods are proxied explicitly: implicit special-method lookups
216 bypass :meth:`__getattr__`, because Python resolves them on the type rather
217 than the instance.
218 """
219
220 _file: t.IO[t.Any]
221
222 def __init__(self, file: t.IO[t.Any]) -> None:
223 self._file = file
224
225 def __getattr__(self, name: str) -> t.Any:
226 return getattr(self._file, name)
227
228 def __enter__(self) -> KeepOpenFile:
229 return self
230
231 def __exit__(
232 self,
233 exc_type: type[BaseException] | None,
234 exc_value: BaseException | None,
235 tb: TracebackType | None,
236 ) -> None:
237 pass
238
239 def __repr__(self) -> str:
240 return repr(self._file)
241
242 def __iter__(self) -> cabc.Iterator[t.AnyStr]:
243 return iter(self._file)
244
245
246def echo(

Callers 2

_nullpagerFunction · 0.85
open_fileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected