MCPcopy Create free account
hub / github.com/codespell-project/codespell / open_with_chardet

Method open_with_chardet

codespell_lib/_codespell.py:246–273  ·  view source on GitHub ↗
(
        self, filename: str
    )

Source from the content-addressed store, hash-verified

244 return self.open_with_internal(filename)
245
246 def open_with_chardet(
247 self, filename: str
248 ) -> tuple[list[tuple[bool, int, list[str]]], str]:
249 self.encdetector.reset()
250 with open(filename, "rb") as fb:
251 for line in fb:
252 self.encdetector.feed(line)
253 if self.encdetector.done:
254 break
255 self.encdetector.close()
256 encoding = self.encdetector.result["encoding"]
257
258 try:
259 f = open(filename, encoding=encoding, newline="")
260 except UnicodeDecodeError:
261 print(f"ERROR: Could not detect encoding: {filename}", file=sys.stderr)
262 raise
263 except LookupError:
264 print(
265 f"ERROR: Don't know how to handle encoding {encoding}: {filename}",
266 file=sys.stderr,
267 )
268 raise
269 else:
270 lines = self.get_lines(f)
271 f.close()
272
273 return lines, f.encoding
274
275 def open_with_internal(
276 self, filename: str

Callers 1

openMethod · 0.95

Calls 1

get_linesMethod · 0.95

Tested by

no test coverage detected