MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _read_source_bytes

Function _read_source_bytes

graphify/extractors/base.py:159–209  ·  view source on GitHub ↗

Read a source file as the UTF-8 bytes tree-sitter parses. tree-sitter treats its input as UTF-8. Raw bytes in another encoding were parsed as-is: a UTF-16 file (interleaved NUL bytes) produced no nodes at all, and a Windows-1252 identifier was cut at its first non-UTF-8 byte (``Café

(path: Path, *, warn: bool = True)

Source from the content-addressed store, hash-verified

157
158
159def _read_source_bytes(path: Path, *, warn: bool = True) -> bytes:
160 """Read a source file as the UTF-8 bytes tree-sitter parses.
161
162 tree-sitter treats its input as UTF-8. Raw bytes in another encoding were
163 parsed as-is: a UTF-16 file (interleaved NUL bytes) produced no nodes at
164 all, and a Windows-1252 identifier was cut at its first non-UTF-8 byte
165 (``CaféOrder`` came out as ``Order``) — both silently.
166
167 Valid UTF-8, with or without a BOM, is returned byte-for-byte, so node ids,
168 byte offsets and cache keys for those files are unchanged. A UTF-16 or
169 UTF-32 file with a BOM is decoded exactly and re-encoded as UTF-8. Anything
170 else is decoded as cp1252, then latin-1 (which cannot fail), so names come
171 out whole; line numbers are unchanged in every case. ``warn`` names the file
172 once per process on that last, guessed path — pass ``warn=False`` from
173 secondary passes that re-read a file the extractor already read.
174 """
175 # Reuse the bytes cache.file_hash() just read for the cache key, when the
176 # current context holds them for this path, so a cold build reads the file
177 # once instead of twice (#perf). Falls back to a disk read for any path the
178 # key hash did not read (warm stat fastpath, direct extractor calls, tests).
179 raw = _take_preread_bytes(path)
180 if raw is None:
181 raw = path.read_bytes()
182 if raw.isascii():
183 return raw
184 try:
185 raw.decode("utf-8")
186 return raw
187 except UnicodeDecodeError:
188 pass
189 for boms, encoding in ((_UTF32_BOMS, "utf-32"), (_UTF16_BOMS, "utf-16")):
190 if raw.startswith(boms):
191 try:
192 return raw.decode(encoding).encode("utf-8")
193 except UnicodeDecodeError:
194 continue # FF FE 00 00 is also UTF-16-LE: BOM + U+0000
195 for encoding in _SOURCE_FALLBACK_ENCODINGS:
196 try:
197 text = raw.decode(encoding)
198 except UnicodeDecodeError:
199 continue
200 key = str(path)
201 if warn and key not in _warned_source_encodings:
202 _warned_source_encodings.add(key)
203 print(
204 f"[graphify] WARNING: {path} is not valid UTF-8; read it as {encoding}. "
205 "Re-save it as UTF-8 if names in the graph look wrong.",
206 file=sys.stderr,
207 )
208 return text.encode("utf-8")
209 return raw # unreachable: latin-1 decodes every byte
210
211
212def _read_source_text(path: Path, *, warn: bool = True) -> str:

Callers 15

extract_jsFunction · 0.90
extract_cppFunction · 0.90
extract_csharpFunction · 0.90
extract_phpFunction · 0.90
extract_swiftFunction · 0.90
extract_rFunction · 0.90
extract_dmFunction · 0.90
extract_terraformFunction · 0.90
extract_objcFunction · 0.90
extract_erlangFunction · 0.90

Calls

no outgoing calls