Fix leaking file object when loading non-blend file

This commit is contained in:
Sybren A. Stüvel 2018-02-23 14:05:27 +01:00
parent 8e0b135eb3
commit eb8281ea82
2 changed files with 10 additions and 1 deletions

View File

@ -94,6 +94,7 @@ class BlendFile:
fileobj.close()
self.fileobj = tmpfile
elif magic != BLENDFILE_MAGIC:
fileobj.close()
raise exceptions.BlendFileError("File is not a blend file", path)
self.header = header.BlendFileHeader(self.fileobj, self.raw_filepath)

View File

@ -1,7 +1,9 @@
import os
import pathlib
import unittest
from blender_asset_tracer import blendfile
from blender_asset_tracer.blendfile import iterators
from blender_asset_tracer.blendfile import iterators, exceptions
from abstract_test import AbstractBlendFileTest
@ -206,3 +208,9 @@ class LoadCompressedTest(AbstractBlendFileTest):
ob = self.bf.code_index[b'OB'][0]
name = ob.get((b'id', b'name'))
self.assertEqual('OBümlaut', name)
class LoadNonBlendfileTest(unittest.TestCase):
def test_loading(self):
with self.assertRaises(exceptions.BlendFileError):
blendfile.BlendFile(pathlib.Path(__file__))