From 903c185ee642d4212943293c3863a7b1b69ce65d Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 19 Nov 2020 10:39:41 +0100 Subject: [PATCH] distinguish 'file not found' and 'invalid file' --- python/damask/_vtk.py | 5 ++--- python/damask/mechanics.py | 3 +++ python/tests/test_Geom.py | 2 +- python/tests/test_VTK.py | 15 ++++++++++----- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/python/damask/_vtk.py b/python/damask/_vtk.py index 8104c3cca..8e6cf490c 100644 --- a/python/damask/_vtk.py +++ b/python/damask/_vtk.py @@ -139,6 +139,8 @@ class VTK: vtkUnstructuredGrid, and vtkPolyData. """ + if not os.path.isfile(fname): # vtk has a strange error handling + raise FileNotFoundError(f'No such file: {fname}') ext = Path(fname).suffix if ext == '.vtk' or dataset_type is not None: reader = vtk.vtkGenericDataObjectReader() @@ -166,9 +168,6 @@ class VTK: else: raise TypeError(f'Unknown file extension {ext}') - if not os.path.isfile(fname): - raise FileNotFoundError(f'No such file: {fname}') - reader.SetFileName(str(fname)) reader.Update() vtk_data = reader.GetOutput() diff --git a/python/damask/mechanics.py b/python/damask/mechanics.py index d1a208e3c..c8d865885 100644 --- a/python/damask/mechanics.py +++ b/python/damask/mechanics.py @@ -299,6 +299,9 @@ def _polar_decomposition(T,requested): if 'U' in requested: output.append(_np.einsum('...ji,...jk',R,T)) + if len(output) == 0: + raise ValueError('Output needs to be out of V,R,U') + return tuple(output) diff --git a/python/tests/test_Geom.py b/python/tests/test_Geom.py index 3b7ef61ce..10220559b 100644 --- a/python/tests/test_Geom.py +++ b/python/tests/test_Geom.py @@ -55,7 +55,7 @@ class TestGeom: def test_invalid_vtr(self,tmp_path): v = VTK.from_rectilinear_grid(np.random.randint(5,10,3)*2,np.random.random(3) + 1.0) - v.save(tmp_path/'no_materialpoint.vtr') + v.save(tmp_path/'no_materialpoint.vtr',parallel=False) with pytest.raises(ValueError): Geom.load(tmp_path/'no_materialpoint.vtr') diff --git a/python/tests/test_VTK.py b/python/tests/test_VTK.py index 48dce4707..af4ecf918 100644 --- a/python/tests/test_VTK.py +++ b/python/tests/test_VTK.py @@ -100,12 +100,17 @@ class TestVTK: v = VTK.from_poly_data(points) v.save(tmp_path/fname) - @pytest.mark.parametrize('name,dataset_type',[('this_file_does_not_exist.vtk', None), - ('this_file_does_not_exist.vtk','vtk'), - ('this_file_does_not_exist.vtx', None)]) - def test_invalid_dataset_type(self,name,dataset_type): + @pytest.mark.parametrize('fname,dataset_type',[('a_file.vtk', None), + ('a_file.vtk','vtk'), + ('a_file.vtx', None)]) + def test_invalid_dataset_type(self,tmp_path,fname,dataset_type): + open(tmp_path/fname,'a').close() with pytest.raises(TypeError): - VTK.load(name,dataset_type) + VTK.load(tmp_path/fname,dataset_type) + + def test_file_not_found(self): + with pytest.raises(FileNotFoundError): + VTK.load('/dev/null') def test_add_extension(self,tmp_path,default): default.save(tmp_path/'default.txt',parallel=False)