Skip to content

Commit 9ff725d

Browse files
authored
Revert "zopen: explicit binary/text mode , and explicit encoding as UTF…" (#4221)
This reverts commit b2a5e0f.
1 parent ac663b4 commit 9ff725d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+170
-168
lines changed

dev_scripts/potcar_scrambler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def scramble_single_potcar(self, potcar: PotcarSingle) -> str:
124124
return scrambled_potcar_str
125125

126126
def to_file(self, filename: str) -> None:
127-
with zopen(filename, mode="wt", encoding="utf-8") as file:
127+
with zopen(filename, mode="wt") as file:
128128
file.write(self.scrambled_potcars_str)
129129

130130
@classmethod

src/pymatgen/apps/borg/hive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def _get_transformation_history(path: PathLike):
445445
"""Check for a transformations.json* file and return the history."""
446446
if trans_json := glob(f"{path!s}/transformations.json*"):
447447
try:
448-
with zopen(trans_json[0], mode="rt", encoding="utf-8") as file:
448+
with zopen(trans_json[0]) as file:
449449
return json.load(file)["history"]
450450
except Exception:
451451
return None

src/pymatgen/apps/borg/queen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ def save_data(self, filename: PathLike) -> None:
103103
that if the filename ends with gz or bz2, the relevant gzip
104104
or bz2 compression will be applied.
105105
"""
106-
with zopen(filename, mode="wt", encoding="utf-8") as file:
106+
with zopen(filename, mode="wt") as file:
107107
json.dump(list(self._data), file, cls=MontyEncoder)
108108

109109
def load_data(self, filename: PathLike) -> None:
110110
"""Load assimilated data from a file."""
111-
with zopen(filename, mode="rt", encoding="utf-8") as file:
111+
with zopen(filename, mode="rt") as file:
112112
self._data = json.load(file, cls=MontyDecoder)
113113

114114

src/pymatgen/core/structure.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,15 +2953,15 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29532953
elif fmt == "json" or fnmatch(filename.lower(), "*.json*"):
29542954
json_str = json.dumps(self.as_dict())
29552955
if filename:
2956-
with zopen(filename, mode="wt", encoding="utf-8") as file:
2956+
with zopen(filename, mode="wt") as file:
29572957
file.write(json_str)
29582958
return json_str
29592959
elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
29602960
from pymatgen.io.xcrysden import XSF
29612961

29622962
res_str = XSF(self).to_str()
29632963
if filename:
2964-
with zopen(filename, mode="wt", encoding="utf-8") as file:
2964+
with zopen(filename, mode="wt", encoding="utf8") as file:
29652965
file.write(res_str)
29662966
return res_str
29672967
elif (
@@ -2987,15 +2987,15 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29872987
yaml.dump(self.as_dict(), str_io)
29882988
yaml_str = str_io.getvalue()
29892989
if filename:
2990-
with zopen(filename, mode="wt", encoding="utf-8") as file:
2990+
with zopen(filename, mode="wt") as file:
29912991
file.write(yaml_str)
29922992
return yaml_str
29932993
elif fmt == "aims" or fnmatch(filename, "geometry.in"):
29942994
from pymatgen.io.aims.inputs import AimsGeometryIn
29952995

29962996
geom_in = AimsGeometryIn.from_structure(self)
29972997
if filename:
2998-
with zopen(filename, mode="wt", encoding="utf-8") as file:
2998+
with zopen(filename, mode="w") as file:
29992999
file.write(geom_in.get_header(filename))
30003000
file.write(geom_in.content)
30013001
file.write("\n")
@@ -3010,7 +3010,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
30103010

30113011
res_str = ResIO.structure_to_str(self)
30123012
if filename:
3013-
with zopen(filename, mode="wt", encoding="utf-8") as file:
3013+
with zopen(filename, mode="wt", encoding="utf8") as file:
30143014
file.write(res_str)
30153015
return res_str
30163016
elif fmt == "pwmat" or fnmatch(filename.lower(), "*.pwmat") or fnmatch(filename.lower(), "*.config"):
@@ -3173,7 +3173,7 @@ def from_file(
31733173
return struct
31743174

31753175
fname = os.path.basename(filename)
3176-
with zopen(filename, mode="rt", errors="replace", encoding="utf-8") as file:
3176+
with zopen(filename, mode="rt", errors="replace") as file:
31773177
contents = file.read()
31783178
if fnmatch(fname.lower(), "*.cif*") or fnmatch(fname.lower(), "*.mcif*"):
31793179
return cls.from_str(
@@ -3919,7 +3919,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
39193919
elif fmt == "json" or fnmatch(filename, "*.json*") or fnmatch(filename, "*.mson*"):
39203920
json_str = json.dumps(self.as_dict())
39213921
if filename:
3922-
with zopen(filename, mode="wt", encoding="utf-8") as file:
3922+
with zopen(filename, mode="wt", encoding="utf8") as file:
39233923
file.write(json_str)
39243924
return json_str
39253925
elif fmt in {"yaml", "yml"} or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
@@ -3928,7 +3928,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
39283928
yaml.dump(self.as_dict(), str_io)
39293929
yaml_str = str_io.getvalue()
39303930
if filename:
3931-
with zopen(filename, mode="wt", encoding="utf-8") as file:
3931+
with zopen(filename, mode="wt", encoding="utf8") as file:
39323932
file.write(yaml_str)
39333933
return yaml_str
39343934
else:
@@ -4010,7 +4010,7 @@ def from_file(cls, filename: PathLike) -> Self | None:
40104010
"""
40114011
filename = str(filename)
40124012

4013-
with zopen(filename, mode="rt", encoding="utf-8") as file:
4013+
with zopen(filename) as file:
40144014
contents = file.read()
40154015
fname = filename.lower()
40164016
if fnmatch(fname, "*.xyz*"):

src/pymatgen/core/trajectory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def write_Xdatcar(
467467

468468
xdatcar_str = "\n".join(lines) + "\n"
469469

470-
with zopen(filename, mode="wt", encoding="utf-8") as file:
470+
with zopen(filename, mode="wt") as file:
471471
file.write(xdatcar_str)
472472

473473
def as_dict(self) -> dict:

src/pymatgen/io/adf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ def _parse_logfile(self, logfile):
645645
# The last non-empty line of the logfile must match the end pattern.
646646
# Otherwise the job has some internal failure. The TAPE13 part of the
647647
# ADF manual has a detailed explanation.
648-
with zopen(logfile, mode="rt", encoding="utf-8") as file:
648+
with zopen(logfile, mode="rt") as file:
649649
for line in reverse_readline(file):
650650
if line == "":
651651
continue

src/pymatgen/io/aims/inputs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def from_file(cls, filepath: str | Path) -> Self:
133133
Returns:
134134
AimsGeometryIn: The input object represented in the file
135135
"""
136-
with zopen(filepath, mode="rt", encoding="utf-8") as in_file:
136+
with zopen(filepath, mode="rt") as in_file:
137137
content = in_file.read()
138138
return cls.from_str(content)
139139

@@ -753,7 +753,7 @@ def from_file(cls, filename: str, label: str | None = None) -> Self:
753753
Returns:
754754
AimsSpeciesFile
755755
"""
756-
with zopen(filename, mode="rt", encoding="utf-8") as file:
756+
with zopen(filename, mode="rt") as file:
757757
return cls(data=file.read(), label=label)
758758

759759
@classmethod

src/pymatgen/io/cif.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def from_file(cls, filename: PathLike) -> Self:
299299
Returns:
300300
CifFile
301301
"""
302-
with zopen(filename, mode="rt", errors="replace", encoding="utf-8") as file:
302+
with zopen(filename, mode="rt", errors="replace") as file:
303303
return cls.from_str(file.read())
304304

305305

@@ -1760,9 +1760,9 @@ def cif_file(self) -> CifFile:
17601760

17611761
def write_file(
17621762
self,
1763-
filename: PathLike,
1764-
mode: Literal["wt", "at"] = "wt",
1763+
filename: str | Path,
1764+
mode: Literal["w", "a", "wt", "at"] = "w",
17651765
) -> None:
17661766
"""Write the CIF file."""
1767-
with zopen(filename, mode=mode, encoding="utf-8") as file:
1767+
with zopen(filename, mode=mode) as file:
17681768
file.write(str(self))

src/pymatgen/io/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def to_cube(self, filename, comment: str = ""):
354354
filename (str): Name of the cube file to be written.
355355
comment (str): If provided, this will be added to the second comment line
356356
"""
357-
with zopen(filename, mode="wt", encoding="utf-8") as file:
357+
with zopen(filename, mode="wt") as file:
358358
file.write(f"# Cube file for {self.structure.formula} generated by Pymatgen\n")
359359
file.write(f"# {comment}\n")
360360
file.write(f"\t {len(self.structure)} 0.000000 0.000000 0.000000\n")
@@ -386,7 +386,7 @@ def from_cube(cls, filename: str | Path) -> Self:
386386
Args:
387387
filename (str): of the cube to read
388388
"""
389-
file = zopen(filename, mode="rt", encoding="utf-8")
389+
file = zopen(filename, mode="rt")
390390

391391
# skip header lines
392392
file.readline()
@@ -529,7 +529,7 @@ def __getitem__(self, item):
529529
f"No parser defined for {item}. Contents are returned as a string.",
530530
stacklevel=2,
531531
)
532-
with zopen(fpath, mode="rt", encoding="utf-8") as f:
532+
with zopen(fpath, "rt") as f:
533533
return f.read()
534534

535535
def get_files_by_name(self, name: str) -> dict[str, Any]:

src/pymatgen/io/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def write_file(self, filename: PathLike) -> None:
7474
Args:
7575
filename: The filename to output to, including path.
7676
"""
77-
with zopen(Path(filename), mode="wt", encoding="utf-8") as file:
77+
with zopen(Path(filename), mode="wt") as file:
7878
file.write(self.get_str())
7979

8080
@classmethod
@@ -102,7 +102,7 @@ def from_file(cls, path: PathLike) -> None:
102102
Returns:
103103
InputFile
104104
"""
105-
with zopen(Path(path), mode="rt", encoding="utf-8") as file:
105+
with zopen(Path(path), mode="rt") as file:
106106
return cls.from_str(file.read()) # from_str not implemented
107107

108108

@@ -218,7 +218,7 @@ def write_input(
218218
if isinstance(contents, InputFile):
219219
contents.write_file(file_path)
220220
else:
221-
with zopen(file_path, mode="wt", encoding="utf-8") as file:
221+
with zopen(file_path, mode="wt") as file:
222222
file.write(str(contents))
223223

224224
if zip_inputs:

0 commit comments

Comments
 (0)