Skip to content

Commit 0bbfded

Browse files
committed
Catch exceptions from opening invalid files
This ensures very badly invalid files are still recorded as such, while allowing further files to be processed.
1 parent 620044e commit 0bbfded

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

CHANGELOG.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Version 1.4.1 (2022-07-12)
66

77
* Add file names to mrcfile-header output
88
* Add file names and a success message for valid files to the output of the
9-
validation functions (mrcfile-validate command and the validate() method)
9+
validation functions (mrcfile-validate command and the mrcfile.validate()
10+
function)
1011
* Add support for numpy 1.23 and Python 3.10 and 3.11
1112

1213
Version 1.4.0 (2022-07-02)

mrcfile/validator.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import argparse
2020
import sys
21+
import traceback
2122

2223
from . import load_functions
2324

@@ -155,11 +156,15 @@ def validate(name, print_file=None):
155156
is not the same size as expected from the header.
156157
"""
157158
print("Checking if {} is a valid MRC2014 file...".format(name), file=print_file)
158-
with load_functions.open(name, permissive=True) as mrc:
159-
result = mrc.validate(print_file=print_file)
160-
if result:
161-
print("File appears to be valid.", file=print_file)
162-
return result
159+
try:
160+
with load_functions.open(name, permissive=True) as mrc:
161+
result = mrc.validate(print_file=print_file)
162+
except Exception:
163+
result = False
164+
traceback.print_exc(file=print_file)
165+
if result:
166+
print("File appears to be valid.", file=print_file)
167+
return result
163168

164169

165170
if __name__ == '__main__':

tests/test_validation.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99
from __future__ import (absolute_import, division, print_function,
1010
unicode_literals)
1111

12-
import io
1312
import os
1413
import shutil
1514
import sys
1615
import tempfile
1716
import unittest
1817
import warnings
1918

19+
# Avoid str/unicode issues when traceback.print_exc tries to write to StringIO
20+
# (see https://stackoverflow.com/a/34872005)
21+
try:
22+
# Python 2
23+
from cStringIO import StringIO
24+
except ImportError:
25+
# Python 3
26+
from io import StringIO
27+
2028
import numpy as np
2129

2230
import mrcfile
@@ -43,15 +51,16 @@ def setUp(self):
4351
self.ext_header_mrc_name = os.path.join(self.test_data, 'EMD-3001.map')
4452
self.fei1_ext_header_mrc_name = os.path.join(self.test_data, 'fei-extended.mrc')
4553
self.fei2_ext_header_mrc_name = os.path.join(self.test_data, 'epu2.9_example.mrc')
54+
self.not_an_mrc_name = os.path.join(self.test_data, 'README.txt')
4655

4756
# Set up stream to catch print output from validate()
48-
self.print_stream = io.StringIO()
57+
self.print_stream = StringIO()
4958

5059
# Replace stdout and stderr to capture output for checking
5160
self.orig_stdout = sys.stdout
5261
self.orig_stderr = sys.stderr
53-
sys.stdout = io.StringIO()
54-
sys.stderr = io.StringIO()
62+
sys.stdout = StringIO()
63+
sys.stderr = StringIO()
5564

5665
def tearDown(self):
5766
# Restore stdout and stderr
@@ -562,6 +571,7 @@ def test_validate_good_files(self):
562571

563572
def test_validate_bad_files(self):
564573
bad_files = [
574+
self.not_an_mrc_name,
565575
self.example_mrc_name,
566576
self.ext_header_mrc_name,
567577
self.gzip_mrc_name
@@ -570,11 +580,17 @@ def test_validate_bad_files(self):
570580
assert result == False
571581
print_output = self.print_stream.getvalue()
572582
assert len(print_output) > 0
583+
assert "Checking if " + bad_files[0] + " is a valid MRC2014 file..." in print_output
584+
assert "Checking if " + bad_files[1] + " is a valid MRC2014 file..." in print_output
585+
assert "Checking if " + bad_files[2] + " is a valid MRC2014 file..." in print_output
586+
assert "Checking if " + bad_files[3] + " is a valid MRC2014 file..." in print_output
587+
assert "ValueError:" in print_output
573588
assert len(sys.stdout.getvalue()) == 0
574589
assert len(sys.stderr.getvalue()) == 0
575590

576591
def test_validate_good_and_bad_files(self):
577592
files = self.create_good_files() + [
593+
self.not_an_mrc_name,
578594
self.example_mrc_name,
579595
self.ext_header_mrc_name,
580596
self.gzip_mrc_name

0 commit comments

Comments
 (0)