From e0148f71815d7d9694c0217a59c369e40da09f4c Mon Sep 17 00:00:00 2001 From: xiefangqi Date: Mon, 10 May 2021 22:16:37 +0800 Subject: [PATCH] fix minddata directory path problem --- mindspore/ccsrc/minddata/dataset/core/tensor.cc | 4 ++++ tests/ut/python/dataset/test_datasets_imagefolder.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/mindspore/ccsrc/minddata/dataset/core/tensor.cc b/mindspore/ccsrc/minddata/dataset/core/tensor.cc index fa3f4535a8e..620211c0e03 100644 --- a/mindspore/ccsrc/minddata/dataset/core/tensor.cc +++ b/mindspore/ccsrc/minddata/dataset/core/tensor.cc @@ -260,6 +260,10 @@ Status Tensor::CreateFromByteList(const dataengine::BytesList &bytes_list, const #endif Status Tensor::CreateFromFile(const std::string &path, std::shared_ptr *out) { + Path file(path); + if (file.IsDirectory()) { + RETURN_STATUS_UNEXPECTED("Invalid file found: " + path + ", should be file, but got directory."); + } std::ifstream fs; fs.open(path, std::ios::binary | std::ios::in); CHECK_FAIL_RETURN_UNEXPECTED(!fs.fail(), "Fail to open file: " + path); diff --git a/tests/ut/python/dataset/test_datasets_imagefolder.py b/tests/ut/python/dataset/test_datasets_imagefolder.py index 1abc8175500..2ebccdcd168 100644 --- a/tests/ut/python/dataset/test_datasets_imagefolder.py +++ b/tests/ut/python/dataset/test_datasets_imagefolder.py @@ -759,6 +759,15 @@ def test_imagefolder_exception(): except RuntimeError as e: assert "map operation: [PyFunc] failed. The corresponding data files" in str(e) + data_dir_invalid = "../data/dataset/testPK" + try: + data = ds.ImageFolderDataset(data_dir_invalid) + for _ in data.__iter__(): + pass + assert False + except RuntimeError as e: + assert "should be file, but got directory" in str(e) + if __name__ == '__main__': test_imagefolder_basic()