1
0
mirror of synced 2024-11-24 18:24:23 +01:00

Raise a ValueError explicitly

This commit is contained in:
jvoisin 2020-11-23 19:50:46 +01:00
parent 88b7ec2c48
commit 61dce89fbd
3 changed files with 30 additions and 21 deletions

View File

@ -40,7 +40,10 @@ def _get_parsers() -> List[T]:
def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]: def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]:
""" Return the appropriate parser for a given filename. """ """ Return the appropriate parser for a given filename.
:raises ValueError: Raised if the instantiation of the parser went wrong.
"""
mtype, _ = mimetypes.guess_type(filename) mtype, _ = mimetypes.guess_type(filename)
_, extension = os.path.splitext(filename) _, extension = os.path.splitext(filename)
@ -53,10 +56,6 @@ def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]:
for parser_class in _get_parsers(): # type: ignore for parser_class in _get_parsers(): # type: ignore
if mtype in parser_class.mimetypes: if mtype in parser_class.mimetypes:
try: # This instantiation might raise a ValueError on malformed files
return parser_class(filename), mtype return parser_class(filename), mtype
except ValueError as e:
logging.info("Got an exception when trying to instantiate "
"%s for %s: %s", parser_class, filename, e)
return None, mtype
return None, mtype return None, mtype

12
mat2
View File

@ -85,7 +85,11 @@ def show_meta(filename: str, sandbox: bool):
if not __check_file(filename): if not __check_file(filename):
return return
p, mtype = parser_factory.get_parser(filename) # type: ignore try:
p, mtype = parser_factory.get_parser(filename) # type: ignore
except ValueError as e:
print("[-] something went wrong when processing %s: %s" % (filename, e))
return
if p is None: if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype)) print("[-] %s's format (%s) is not supported" % (filename, mtype))
return return
@ -126,7 +130,11 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool
if not __check_file(filename, mode): if not __check_file(filename, mode):
return False return False
p, mtype = parser_factory.get_parser(filename) # type: ignore try:
p, mtype = parser_factory.get_parser(filename) # type: ignore
except ValueError as e:
print("[-] something went wrong when cleaning %s: %s" % (filename, e))
return False
if p is None: if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype)) print("[-] %s's format (%s) is not supported" % (filename, mtype))
return False return False

View File

@ -65,8 +65,10 @@ class TestCorruptedEmbedded(unittest.TestCase):
def test_docx(self): def test_docx(self):
shutil.copy('./tests/data/embedded_corrupted.docx', './tests/data/clean.docx') shutil.copy('./tests/data/embedded_corrupted.docx', './tests/data/clean.docx')
parser, _ = parser_factory.get_parser('./tests/data/clean.docx') parser, _ = parser_factory.get_parser('./tests/data/clean.docx')
self.assertFalse(parser.remove_all()) with self.assertRaises(ValueError):
self.assertIsNotNone(parser.get_meta()) parser.remove_all()
with self.assertRaises(ValueError):
self.assertIsNotNone(parser.get_meta())
os.remove('./tests/data/clean.docx') os.remove('./tests/data/clean.docx')
def test_odt(self): def test_odt(self):
@ -120,8 +122,8 @@ class TestCorruptedFiles(unittest.TestCase):
def test_png2(self): def test_png2(self):
shutil.copy('./tests/test_libmat2.py', './tests/clean.png') shutil.copy('./tests/test_libmat2.py', './tests/clean.png')
parser, _ = parser_factory.get_parser('./tests/clean.png') with self.assertRaises(ValueError):
self.assertIsNone(parser) parser_factory.get_parser('./tests/clean.png')
os.remove('./tests/clean.png') os.remove('./tests/clean.png')
def test_torrent(self): def test_torrent(self):
@ -237,10 +239,10 @@ class TestCorruptedFiles(unittest.TestCase):
zout.write('./tests/data/embedded_corrupted.docx') zout.write('./tests/data/embedded_corrupted.docx')
p, mimetype = parser_factory.get_parser('./tests/data/clean.zip') p, mimetype = parser_factory.get_parser('./tests/data/clean.zip')
self.assertEqual(mimetype, 'application/zip') self.assertEqual(mimetype, 'application/zip')
meta = p.get_meta() with self.assertRaises(ValueError):
self.assertEqual(meta['tests/data/dirty.flac']['comments'], 'Thank you for using MAT !') p.get_meta()
self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!') with self.assertRaises(ValueError):
self.assertFalse(p.remove_all()) self.assertFalse(p.remove_all())
os.remove('./tests/data/clean.zip') os.remove('./tests/data/clean.zip')
def test_html(self): def test_html(self):
@ -315,10 +317,10 @@ class TestCorruptedFiles(unittest.TestCase):
zout.addfile(tarinfo, f) zout.addfile(tarinfo, f)
p, mimetype = parser_factory.get_parser('./tests/data/clean.tar') p, mimetype = parser_factory.get_parser('./tests/data/clean.tar')
self.assertEqual(mimetype, 'application/x-tar') self.assertEqual(mimetype, 'application/x-tar')
meta = p.get_meta() with self.assertRaises(ValueError):
self.assertEqual(meta['./tests/data/dirty.flac']['comments'], 'Thank you for using MAT !') p.get_meta()
self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!') with self.assertRaises(ValueError):
self.assertFalse(p.remove_all()) self.assertFalse(p.remove_all())
os.remove('./tests/data/clean.tar') os.remove('./tests/data/clean.tar')
shutil.copy('./tests/data/dirty.png', './tests/data/clean.tar') shutil.copy('./tests/data/dirty.png', './tests/data/clean.tar')