Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shapash/report/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ def export_and_save_report(working_dir: str, output_file: str):
)
(body, resources) = exporter.from_filename(filename=os.path.join(working_dir, "base_report.ipynb"))

with open(output_file, "w") as file:
with open(output_file, "w", encoding="utf-8") as file:
file.write(body)
26 changes: 26 additions & 0 deletions tests/integration_tests/test_report_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import tempfile
import unittest
from unittest.mock import MagicMock, mock_open, patch

import catboost as cb
import category_encoders as ce
Expand Down Expand Up @@ -151,3 +152,28 @@ def test_export_and_save_report_1(self):
export_and_save_report(working_dir=tmp_dir_path, output_file=outfile)
assert os.path.exists(outfile)
shutil.rmtree(tmp_dir_path)

def test_export_and_save_report_writes_utf8(self):
"""
Regression test for https://github.com/MAIF/shapash/issues/699:
export_and_save_report must pass encoding='utf-8' to open(), otherwise
on Windows (cp1252 default) any non-Latin1 character in the HTML body
raises UnicodeEncodeError.
"""
fake_html = "café — déjà vu — résumé"
fake_exporter = MagicMock()
fake_exporter.from_filename.return_value = (fake_html, {})

m = mock_open()
with (
patch("shapash.report.generation.HTMLExporter", return_value=fake_exporter),
patch("shapash.report.generation.open", m, create=True),
):
export_and_save_report(working_dir="dummy", output_file="dummy.html")

write_calls = [c for c in m.call_args_list if len(c.args) >= 2 and c.args[1] == "w"]
assert write_calls, "expected at least one write-mode open() call"
for call in write_calls:
assert call.kwargs.get("encoding") == "utf-8", (
f"export_and_save_report must call open(..., 'w', encoding='utf-8'); got {call}"
)