Skip to content

Commit

Permalink
adding comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mrastgoo committed Dec 18, 2024
1 parent 911eb5a commit 036db59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion skrub/_reporting/_table_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,15 @@ def write_html(self, file):
with open(file, "w", encoding="utf8") as stream:
stream.write(html)
return

try:
# We don't have information about the write mode of the provided
# file-object. We start by writing bytes into it.
file.write(html.encode("utf-8"))
return
except TypeError:
# We end-up here if the file-object was open in text mode
# Let's give it another chance in this mode.
pass

if (encoding := getattr(file, "encoding", None)) is not None:
Expand All @@ -226,7 +231,8 @@ def write_html(self, file):
"If `file` is a text file it should use utf-8 encoding; got:"
f" {encoding!r}"
)

# We write into the file-object expecting it to be in text mode at this
# stage and with a UTF-8 encoding.
file.write(html)

def open(self):
Expand Down
2 changes: 2 additions & 0 deletions skrub/_reporting/tests/test_table_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def test_write_html(tmp_path, pd_module, filename_type):

tmp_file_path = tmp_path / Path("report.html")

# making sure we are closing the open files, and dealing with the first
# condition which doesn't require opening any file
with contextlib.ExitStack() as stack:
if filename_type == "str":
filename = str(tmp_file_path)
Expand Down

0 comments on commit 036db59

Please sign in to comment.