-
Notifications
You must be signed in to change notification settings - Fork 874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zopen
: explicit binary/text mode
, and explicit encoding
as UTF-8
#4219
zopen
: explicit binary/text mode
, and explicit encoding
as UTF-8
#4219
Conversation
mode
for zopen
mode
for zopen
src/pymatgen/io/cif.py
Outdated
@@ -1761,5 +1761,5 @@ def write_file( | |||
mode: Literal["w", "a", "wt", "at"] = "w", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would fail if filename
is a name for compressed file (e.g. file.gz
), in this case file
would be BinaryIO
and we cannot write str
into it:
pymatgen/src/pymatgen/io/cif.py
Lines 1758 to 1765 in 31f1e1f
def write_file( | |
self, | |
filename: str | Path, | |
mode: Literal["w", "a", "wt", "at"] = "w", | |
) -> None: | |
"""Write the CIF file.""" | |
with zopen(filename, mode=mode) as file: | |
file.write(str(self)) |
w
and a
have been removed in 4fccd59, with default changed to wt
mode
for zopen
zopen
: explicit binary/text mode
, and explicit encoding
as UTF-8
ba0bad7
to
68e78fc
Compare
68e78fc
to
0ece310
Compare
This reverts commit d899c87.
Summary
mode
forzopen
, I have opened a PR frommonty
's side to prevent thiszopen
changes: forbid implicit binary/textmode
, signature change, default UTF-8encoding
in text mode, drop.z
support after one-year materialsvirtuallab/monty#730encoding
forzopen
Detailed reasoning and example are given in materialsvirtuallab/monty#730 (comment). In a word, not passing
b/t
inmode
would cause different and unexpected behaviour when opening a text file (file.txt
) versus a compressed file (file.gz
), for the former we would getTextIO
which gives string butBinaryIO
for the latter which gives bytes.There're few cases where this wouldn't cause problem, for example
json.load
accepts both binary and text IO:For such cases, I tested with the pdag3_complete_dos.json.gz file, "text mode" seems slightly faster: