Fixed file leak when opening corrupted file.
This commit is contained in:
parent
eb8281ea82
commit
9750a91571
@ -97,15 +97,19 @@ class BlendFile:
|
||||
fileobj.close()
|
||||
raise exceptions.BlendFileError("File is not a blend file", path)
|
||||
|
||||
self.header = header.BlendFileHeader(self.fileobj, self.raw_filepath)
|
||||
self.block_header_struct = self.header.create_block_header_struct()
|
||||
self.blocks = []
|
||||
self.code_index = collections.defaultdict(list)
|
||||
self.structs = []
|
||||
self.sdna_index_from_id = {}
|
||||
self.block_from_addr = {}
|
||||
|
||||
try:
|
||||
self.header = header.BlendFileHeader(self.fileobj, self.raw_filepath)
|
||||
self.block_header_struct = self.header.create_block_header_struct()
|
||||
self._load_blocks()
|
||||
except Exception:
|
||||
fileobj.close()
|
||||
raise
|
||||
|
||||
def _load_blocks(self):
|
||||
"""Read the blend file to load its DNA structure to memory."""
|
||||
@ -304,7 +308,7 @@ class BlendFileBlock:
|
||||
if len(data) != header_struct.size:
|
||||
self.log.warning("Blend file %s seems to be truncated, "
|
||||
"expected %d bytes but could read only %d",
|
||||
header_struct.size, len(data))
|
||||
bfile.filepath, header_struct.size, len(data))
|
||||
self.code = b'ENDB'
|
||||
return
|
||||
|
||||
|
||||
BIN
tests/blendfiles/corrupt_only_magic.blend
Normal file
BIN
tests/blendfiles/corrupt_only_magic.blend
Normal file
Binary file not shown.
@ -1,6 +1,5 @@
|
||||
import os
|
||||
import pathlib
|
||||
import unittest
|
||||
|
||||
from blender_asset_tracer import blendfile
|
||||
from blender_asset_tracer.blendfile import iterators, exceptions
|
||||
@ -210,7 +209,11 @@ class LoadCompressedTest(AbstractBlendFileTest):
|
||||
self.assertEqual('OBümlaut', name)
|
||||
|
||||
|
||||
class LoadNonBlendfileTest(unittest.TestCase):
|
||||
class LoadNonBlendfileTest(AbstractBlendFileTest):
|
||||
def test_loading(self):
|
||||
with self.assertRaises(exceptions.BlendFileError):
|
||||
blendfile.BlendFile(pathlib.Path(__file__))
|
||||
|
||||
def test_no_datablocks(self):
|
||||
with self.assertRaises(exceptions.NoDNA1Block):
|
||||
blendfile.BlendFile(self.blendfiles / 'corrupt_only_magic.blend')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user