MCPcopy Index your code
hub / github.com/ipython/ipython / strip_encoding_cookie

Function strip_encoding_cookie

IPython/utils/openpy.py:45–60  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

43 return text.read()
44
45def 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
62def read_py_file(filename: str | Path, skip_encoding_cookie: bool = True) -> str:
63 """Read a Python file, using the encoding declared inside the file.

Callers 2

source_to_unicodeFunction · 0.85
read_py_fileFunction · 0.85

Calls 1

matchMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…