Skip to content

Commit

Permalink
fixes #1061
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhrisca committed Jul 26, 2024
1 parent 77073e9 commit ff4f6c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
27 changes: 16 additions & 11 deletions src/asammdf/blocks/mdf_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,22 @@ def __init__(
self._from_filelike = True
self._read(mapped=False, progress=progress)
else:
if sys.maxsize < 2**32:
self.name = Path(name)
self._file = open(self.name, "rb")
self._from_filelike = False
self._read(mapped=False, progress=progress)
else:
self.name = Path(name)
self._mapped_file = open(self.name, "rb")
self._file = mmap.mmap(self._mapped_file.fileno(), 0, access=mmap.ACCESS_READ)
self._from_filelike = False
self._read(mapped=True, progress=progress)
try:
if sys.maxsize < 2**32:
self.name = Path(name)
self._file = open(self.name, "rb")
self._from_filelike = False
self._read(mapped=False, progress=progress)
else:
self.name = Path(name)
self._mapped_file = open(self.name, "rb")
self._file = mmap.mmap(self._mapped_file.fileno(), 0, access=mmap.ACCESS_READ)
self._from_filelike = False
self._read(mapped=True, progress=progress)
except:
if self._file:
self._file.close()
raise
else:
self._from_filelike = False
version = validate_version_argument(version, hint=3)
Expand Down
55 changes: 30 additions & 25 deletions src/asammdf/blocks/mdf_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,34 +358,39 @@ def __init__(
self._from_filelike = True
self._read(mapped=False, progress=progress)
else:
with open(name, "rb") as stream:
identification = FileIdentificationBlock(stream=stream)
version = identification["version_str"]
version = version.decode("utf-8").strip(" \n\t\0")
flags = identification["unfinalized_standard_flags"]

if version >= "4.10" and flags:
tmpdir = Path(gettempdir())
if self.temporary_folder:
tmpdir = Path(self.temporary_folder)
self.name = tmpdir / f"{os.urandom(6).hex()}_{Path(name).name}"
shutil.copy(name, self.name)
self._file = open(self.name, "rb+")
self._from_filelike = False
self._delete_on_close = True
self._read(mapped=False, progress=progress)
else:
if sys.maxsize < 2**32:
self.name = Path(name)
self._file = open(self.name, "rb")
try:
with open(name, "rb") as stream:
identification = FileIdentificationBlock(stream=stream)
version = identification["version_str"]
version = version.decode("utf-8").strip(" \n\t\0")
flags = identification["unfinalized_standard_flags"]

if version >= "4.10" and flags:
tmpdir = Path(gettempdir())
if self.temporary_folder:
tmpdir = Path(self.temporary_folder)
self.name = tmpdir / f"{os.urandom(6).hex()}_{Path(name).name}"
shutil.copy(name, self.name)
self._file = open(self.name, "rb+")
self._from_filelike = False
self._delete_on_close = True
self._read(mapped=False, progress=progress)
else:
self.name = Path(name)
self._mapped_file = open(self.name, "rb")
self._file = mmap.mmap(self._mapped_file.fileno(), 0, access=mmap.ACCESS_READ)
self._from_filelike = False
self._read(mapped=True, progress=progress)
if sys.maxsize < 2**32:
self.name = Path(name)
self._file = open(self.name, "rb")
self._from_filelike = False
self._read(mapped=False, progress=progress)
else:
self.name = Path(name)
self._mapped_file = open(self.name, "rb")
self._file = mmap.mmap(self._mapped_file.fileno(), 0, access=mmap.ACCESS_READ)
self._from_filelike = False
self._read(mapped=True, progress=progress)
except:
if self._file:
self._file.close()
raise

else:
self._from_filelike = False
Expand Down

0 comments on commit ff4f6c2

Please sign in to comment.