Skip to content

Commit 7e59007

Browse files
committed
Apply PR feedback
Co-authored-by: Felix Divo <[email protected]> Rename method to decompress Add protection against memory error
1 parent 5088ec2 commit 7e59007

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

can/io/player.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def __new__( # type: ignore
8080
suffix = pathlib.PurePath(filename).suffix.lower()
8181

8282
if suffix == ".gz":
83-
suffix, filename = LogReader.unzip(filename)
83+
suffix, filename = LogReader.decompress(filename)
8484

8585
try:
8686
return typing.cast(
@@ -93,21 +93,16 @@ def __new__( # type: ignore
9393
) from None
9494

9595
@staticmethod
96-
def unzip(zipfile: "can.typechecking.StringPathLike"):
96+
def decompress(
97+
filename: "can.typechecking.StringPathLike",
98+
) -> typing.Tuple[str, typing.IO[typing.Any]]:
9799
"""
98100
Return the suffix and io object of the decompressed file.
99101
"""
100-
real_suffix = pathlib.Path(zipfile).suffixes[-2].lower()
101-
file = gzip.open(zipfile, "rt")
102-
103-
# Re-open in binary mode if file not readable.
104-
try:
105-
file.read()
106-
file.seek(0)
107-
except UnicodeDecodeError:
108-
file = gzip.open(zipfile, "rb")
102+
real_suffix = pathlib.Path(filename).suffixes[-2].lower()
103+
mode = "rb" if real_suffix == ".blf" else "rt"
109104

110-
return real_suffix, file
105+
return real_suffix, gzip.open(filename, mode)
111106

112107

113108
class MessageSync: # pylint: disable=too-few-public-methods

0 commit comments

Comments
 (0)