Generator to pull lines from a text-mode file, skipping the encoding cookie if it is found in the first two lines.
(filelike: Iterable[str])
| 43 | return text.read() |
| 44 | |
| 45 | def strip_encoding_cookie(filelike: Iterable[str]) -> Generator[str, None, None]: |
| 46 | """Generator to pull lines from a text-mode file, skipping the encoding |
| 47 | cookie if it is found in the first two lines. |
| 48 | """ |
| 49 | it = iter(filelike) |
| 50 | try: |
| 51 | first = next(it) |
| 52 | if not cookie_comment_re.match(first): |
| 53 | yield first |
| 54 | second = next(it) |
| 55 | if not cookie_comment_re.match(second): |
| 56 | yield second |
| 57 | except StopIteration: |
| 58 | return |
| 59 | |
| 60 | yield from it |
| 61 | |
| 62 | def read_py_file(filename: str | Path, skip_encoding_cookie: bool = True) -> str: |
| 63 | """Read a Python file, using the encoding declared inside the file. |
no test coverage detected
searching dependent graphs…