AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary
Python ai-promptsai-skillblueskyclaudeclaude-codeclawhubdeep-researchhackernewsinstagramopenclawpolymarketrecencyredditresearchsocial-mediatiktoktrendstwitterweb-searchyoutube
fix(env): read .env as utf-8 with locale fallback (#780)
Sean Gearin5f4e7e73c3ac
diff --git a/skills/last30days/scripts/lib/env.py b/skills/last30days/scripts/lib/env.pyindex 11543593..09b23a12 100644--- a/skills/last30days/scripts/lib/env.py+++ b/skills/last30days/scripts/lib/env.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport datetimeimport json+import localeimport osimport sysfrom dataclasses import dataclass@@ -172,20 +173,29 @@ def load_env_file(path: Path) -> dict[str, str]:return env_check_file_permissions(path)- with open(path, 'r', encoding='utf-8-sig') as f:- for line in f:- line = line.strip()- if not line or line.startswith('#'):- continue- if '=' in line:- key, _, value = line.partition('=')- key = key.strip()- value = value.strip()- # Remove quotes if present- if value and value[0] in ('"', "'") and value[-1] == value[0]:- value = value[1:-1]- if key and value:- env.update({key: value})+ # Prefer UTF-8 (utf-8-sig transparently strips a BOM written by Windows+ # editors like Notepad). Fall back to the locale decoder for a genuinely+ # locale-encoded .env (e.g. cp1252) so an existing file that loaded before+ # keeps loading. If it decodes as neither, let UnicodeDecodeError surface+ # rather than corrupting keys/secrets with replacement characters.+ try:+ text = path.read_text(encoding='utf-8-sig')+ except UnicodeDecodeError:+ text = path.read_text(encoding=locale.getpreferredencoding(False))++ for line in text.splitlines():+ line = line.strip()+ if not line or line.startswith('#'):+ continue+ if '=' in line:+ key, _, value = line.partition('=')+ key = key.strip()+ value = value.strip()+ # Remove quotes if present+ if value and value[0] in ('"', "'") and value[-1] == value[0]:+ value = value[1:-1]+ if key and value:+ env.update({key: value})return envdiff --git a/tests/test_env_encoding.py b/tests/test_env_encoding.pynew file mode 100644index 00000000..6232234d--- /dev/null+++ b/tests/test_env_encoding.py@@ -0,0 +1,31 @@+from lib import env+++def test_load_env_file_strips_utf8_bom(tmp_path):+ # A Windows editor (e.g. Notepad) commonly saves .env with a UTF-8 BOM.+ # Without an explicit encoding, open() prepends the BOM to the first key,+ # corrupting it (KeyError below). utf-8-sig transparently strips the BOM.+ env_path = tmp_path / ".env"+ env_path.write_bytes("DISPLAY_NAME=café\nREAL_KEY=ok\n".encode("utf-8-sig"))++ loaded = env.load_env_file(env_path)++ assert loaded["DISPLAY_NAME"] == "café"+ assert loaded["REAL_KEY"] == "ok"+++def test_load_env_file_falls_back_to_locale_encoding(tmp_path, monkeypatch):+ # A pre-existing .env saved in a legacy codepage (e.g. cp1252 on Windows)+ # loaded fine when open() used the locale decoder. Force the fallback locale+ # to cp1252 so the test is deterministic on any runner, and assert the value+ # decodes correctly rather than being replaced/corrupted.+ monkeypatch.setattr(+ env.locale, "getpreferredencoding", lambda do_setlocale=True: "cp1252"+ )+ env_path = tmp_path / ".env"+ env_path.write_bytes("DISPLAY_NAME=Jos\xe9\nREAL_KEY=ok\n".encode("cp1252"))++ loaded = env.load_env_file(env_path)++ assert loaded["DISPLAY_NAME"] == "José"+ assert loaded["REAL_KEY"] == "ok"