Coverage for astrocyte/_text_files.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-10-29 22:10 +0000

1"""Reading text files people edit by hand, on every platform.""" 

2 

3from __future__ import annotations 

4 

5import locale 

6from pathlib import Path 

7 

8 

9def read_user_text(path: str | Path) -> str: 

10 """UTF-8 (with or without the BOM some Windows editors add), else the 

11 platform's legacy encoding. Python's default is that legacy encoding on 

12 Windows (cp1252), so files written there before reads were made UTF-8, 

13 or saved by an editor that still uses it, keep loading.""" 

14 data = Path(path).read_bytes() 

15 try: 

16 return data.decode("utf-8-sig") 

17 except UnicodeDecodeError: 

18 return data.decode(locale.getpreferredencoding(False), errors="replace")