Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dns/_file_util.py: 38%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
3import contextlib
4import os
5from typing import IO, Any, cast
8def as_filename(f: object) -> str | None:
9 if isinstance(f, (str, os.PathLike)):
10 return os.fsdecode(f)
11 return None
14def maybe_open(
15 f: str | os.PathLike | IO[Any], mode: str = "r", encoding: str | None = None
16) -> contextlib.AbstractContextManager[IO[Any]]:
17 filename = as_filename(f)
18 if filename is not None:
19 return open(filename, mode, encoding=encoding)
20 # as_filename() is not a type guard, so narrow f by hand
21 return contextlib.nullcontext(cast(IO[Any], f))