Skip to content

Commit

Permalink
feat: handle re-opening files in binary mode internally
Browse files Browse the repository at this point in the history
  • Loading branch information
rien333 committed Dec 16, 2024
1 parent 68b65c1 commit fa15b61
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions mdto/mdto.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,7 @@ def save(
"""Save object to an XML file.
Args:
file_or_filename (str | TextIO): Path or file-object to write the object's XML representation to.
If passing a file-like object, the file must be opened
in writeable binary mode (i.e. `wb`).
file_or_filename (str | TextIO): Path or file-object to write the object's XML representation to
lxml_args (Optional[dict]): Extra keyword arguments to pass to lxml's write() method.
Defaults to `{xml_declaration=True, pretty_print=True, encoding="UTF-8"}`.
Expand All @@ -472,6 +470,10 @@ def save(
Raises:
ValidationError: object is invalid MDTO
"""
if hasattr(file_or_filename, "write"):
# "re-open" file in binary mode
file_or_filename = file_or_filename.buffer.raw

# validate before serialization to ensure correctness
# (doing this in to_xml would be slow, and perhaps unexpected)
self.validate()
Expand Down

0 comments on commit fa15b61

Please sign in to comment.