78 expected_identity: str |
None =
None,
80 """Return the line index where file content begins.
82 Scans at most the first 5 lines of *lines*, in strict order:
83 shebang → encoding → file-identity comment → blank line.
88 Source file split by newlines (as returned by ``str.splitlines()``).
90 The file's path; its basename is used as the fallback identity
91 match when *expected_identity* is not provided.
92 expected_identity : str | None, optional
93 The exact expected file-identity comment (e.g.
94 ``"# oct/core/parsing.py"``). When supplied the match is strict
95 against this string; otherwise any ``"# ...<path.name>"`` line
98 limit = min(len(lines), 5)
102 if i < limit
and lines[i].strip().startswith(
"#!"):
106 if i < limit
and _ENCODING_RE.match(lines[i].strip()):
111 stripped = lines[i].strip()
112 if expected_identity
is not None:
114 stripped == expected_identity
115 or (stripped.startswith(
"# ")
and stripped.endswith(path.name))
119 stripped.startswith(
"# ")
and stripped.endswith(path.name)
125 if i > 0
and i < limit
and lines[i].strip() ==
"":