Skip to content

Commit f5eb5c8

Browse files
jimisolaclaude
andauthored
fix: read and write pyproject.toml and reqstool_config.yml as UTF-8 (#171)
The plugin opened pyproject.toml and the generated reqstool_config.yml with the platform default encoding, which is cp1252 on Windows. - `poetry install` crashed with UnicodeDecodeError when pyproject.toml contains a UTF-8 character whose bytes cp1252 leaves undefined (for example "Á" or most non-Latin scripts). - reqstool_config.yml was written in cp1252, so a non-ASCII dataset path produced invalid UTF-8 for reqstool, or raised UnicodeEncodeError for characters cp1252 cannot encode. Pass encoding="utf-8" at all five call sites (TOML is UTF-8 by spec). Claude-Session: https://claude.ai/code/session_01Mt2FAXZTXnbKG2Ho6GYy1z Signed-off-by: Jimisola Laursen <jimisola@jimisola.com> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 18b5a7e commit f5eb5c8

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

‎src/reqstool_python_poetry_plugin/plugin.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ def _cleanup_pyproject_install_after_install(self) -> None:
7575
pyproject_path = Path(str(self._poetry.package.root_dir)) / "pyproject.toml"
7676
if not pyproject_path.exists():
7777
return
78-
content = pyproject_path.read_text()
78+
content = pyproject_path.read_text(encoding="utf-8")
7979
cleaned = re.sub(r"\n{3,}", "\n\n", content)
8080
if cleaned != content:
81-
pyproject_path.write_text(cleaned)
81+
pyproject_path.write_text(cleaned, encoding="utf-8")
8282
self._cleo_io.write_line("[reqstool] Cleaned up excess blank lines in pyproject.toml")
8383

8484
@Requirements("POETRY_PLUGIN_003")
@@ -105,7 +105,7 @@ def _update_sdist_include(self) -> None:
105105
f"{reqstool_output_directory}/**/*",
106106
]
107107

108-
with open(pyproject_path) as f:
108+
with open(pyproject_path, encoding="utf-8") as f:
109109
data = tomlkit.load(f)
110110

111111
tool_poetry = data.get("tool", {}).get("poetry", {})
@@ -119,7 +119,7 @@ def _update_sdist_include(self) -> None:
119119

120120
if added:
121121
tool_poetry["include"] = existing_includes
122-
with open(pyproject_path, "w") as f:
122+
with open(pyproject_path, "w", encoding="utf-8") as f:
123123
tomlkit.dump(data, f)
124124
for entry in added:
125125
self._cleo_io.write_line(f"[reqstool] Added to pyproject.toml include: {entry}")
@@ -204,7 +204,7 @@ def _generate_reqstool_config(self) -> None:
204204

205205
output_path = self.get_reqstool_config_file(self._poetry)
206206

207-
with open(output_path, "w") as f:
207+
with open(output_path, "w", encoding="utf-8") as f:
208208
f.write(f"{self.YAML_LANGUAGE_SERVER}\n")
209209
f.write(f"# version: {self._poetry.package.version}\n")
210210
yaml.dump(reqstool_yaml_data, f)

‎tests/unit/reqstool_python_poetry_plugin/test_plugin.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,76 @@ def test_generate_reqstool_config_normalizes_test_results_to_a_list(tmp_path, mo
171171
output = tmp_path / "reqstool_config.yml"
172172
data = YAML().load(output.read_text())
173173
assert data["resources"]["test_results"] == ["build/junit.xml"]
174+
175+
176+
# ---------------------------------------------------------------------------
177+
# Text files are UTF-8 whatever the platform default encoding is (cp1252 on Windows)
178+
# ---------------------------------------------------------------------------
179+
180+
# "Á" is C3 81 in UTF-8 and 0x81 is undefined in cp1252; "✓" has no cp1252 mapping at all.
181+
_NON_ASCII = "Ágnes Müller ✓"
182+
183+
184+
@pytest.fixture
185+
def cp1252_default_encoding(monkeypatch):
186+
"""Make open() and Path.read_text()/write_text() default to cp1252, as on a Windows machine."""
187+
import builtins
188+
import pathlib
189+
190+
real_open = builtins.open
191+
real_read_text = pathlib.Path.read_text
192+
real_write_text = pathlib.Path.write_text
193+
194+
def open_cp1252_default(file, mode="r", *args, **kwargs):
195+
if "b" not in mode and kwargs.get("encoding") is None and len(args) < 2:
196+
kwargs["encoding"] = "cp1252"
197+
return real_open(file, mode, *args, **kwargs)
198+
199+
def read_text_cp1252_default(self, encoding=None, errors=None, newline=None):
200+
return real_read_text(self, encoding=encoding or "cp1252", errors=errors, newline=newline)
201+
202+
def write_text_cp1252_default(self, data, encoding=None, errors=None, newline=None):
203+
return real_write_text(self, data, encoding=encoding or "cp1252", errors=errors, newline=newline)
204+
205+
monkeypatch.setattr("reqstool_python_poetry_plugin.plugin.open", open_cp1252_default, raising=False)
206+
monkeypatch.setattr(pathlib.Path, "read_text", read_text_cp1252_default)
207+
monkeypatch.setattr(pathlib.Path, "write_text", write_text_cp1252_default)
208+
209+
210+
@SVCs("SVC_POETRY_PLUGIN_003")
211+
def test_update_sdist_include_preserves_utf8_pyproject(tmp_path, cp1252_default_encoding):
212+
pyproject_path = tmp_path / "pyproject.toml"
213+
pyproject_path.write_bytes(f'[tool.poetry]\nname = "x"\nauthors = ["{_NON_ASCII}"]\n'.encode("utf-8"))
214+
215+
plugin = _make_plugin(tmp_path)
216+
plugin._update_sdist_include()
217+
218+
data = tomlkit.loads(pyproject_path.read_bytes().decode("utf-8"))
219+
assert data["tool"]["poetry"]["authors"] == [_NON_ASCII]
220+
assert "reqstool_config.yml" in list(data["tool"]["poetry"]["include"])
221+
222+
223+
@SVCs("SVC_POETRY_PLUGIN_005")
224+
def test_cleanup_pyproject_preserves_utf8_pyproject(tmp_path, cp1252_default_encoding):
225+
pyproject_path = tmp_path / "pyproject.toml"
226+
pyproject_path.write_bytes(f'[tool.poetry]\nname = "x"\n\n\n\nauthors = ["{_NON_ASCII}"]\n'.encode("utf-8"))
227+
228+
plugin = _make_plugin(tmp_path)
229+
plugin._cleanup_pyproject_install_after_install()
230+
231+
expected = f'[tool.poetry]\nname = "x"\n\nauthors = ["{_NON_ASCII}"]\n'
232+
assert pyproject_path.read_bytes().decode("utf-8") == expected
233+
234+
235+
@SVCs("SVC_POETRY_PLUGIN_001")
236+
def test_generate_reqstool_config_is_written_as_utf8(tmp_path, monkeypatch, cp1252_default_encoding):
237+
monkeypatch.chdir(tmp_path)
238+
dataset_dir = tmp_path / "dokumentation ✓"
239+
dataset_dir.mkdir()
240+
(dataset_dir / "requirements.yml").write_bytes(b"requirements: []\n")
241+
242+
plugin = _make_plugin(tmp_path, reqstool_config={"dataset_directory": "dokumentation ✓"})
243+
plugin._generate_reqstool_config()
244+
245+
written = (tmp_path / "reqstool_config.yml").read_bytes().decode("utf-8")
246+
assert YAML().load(written)["resources"]["requirements"] == "dokumentation ✓/requirements.yml"

0 commit comments

Comments
 (0)