"...git@developer.sourcefind.cn:OpenDAS/torch-harmonics.git" did not exist on "3a3480b86a0b0be5c27cbcd75cb84365b63aa501"
Unverified Commit 5be4f414 authored by peterjc123's avatar peterjc123 Committed by GitHub
Browse files

Improve read_file for Windows (#2768)

* Improve read_file for Windows

* Fix lint

* Use _stat64 instead

* Apply suggesion

* Fix lint
parent f1f6a972
......@@ -215,10 +215,6 @@ class ImageTester(unittest.TestCase):
data = read_file(fpath)
expected = torch.tensor(list(content), dtype=torch.uint8)
self.assertTrue(data.equal(expected))
# Windows holds into the file until the tensor is alive
# so need to del the tensor before deleting the file see
# https://github.com/pytorch/vision/issues/2743#issuecomment-703817293
del data
os.unlink(fpath)
with self.assertRaisesRegex(
......
#include "read_write_file_cpu.h"
// According to
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=vs-2019,
// we should use _stat64 for 64-bit file size on Windows.
#ifdef _WIN32
#define VISION_STAT _stat64
#else
#define VISION_STAT stat
#endif
torch::Tensor read_file(std::string filename) {
// CHECK if this only works on Windows for files smaller than 2GB
// https://stackoverflow.com/questions/5840148/how-can-i-get-a-files-size-in-c
struct stat stat_buf;
int rc = stat(filename.c_str(), &stat_buf);
struct VISION_STAT stat_buf;
int rc = VISION_STAT(filename.c_str(), &stat_buf);
// errno is a variable defined in errno.h
TORCH_CHECK(
rc == 0, "[Errno ", errno, "] ", strerror(errno), ": '", filename, "'");
......@@ -13,8 +20,14 @@ torch::Tensor read_file(std::string filename) {
TORCH_CHECK(size > 0, "Expected a non empty file");
#ifdef _WIN32
auto data =
torch::from_file(filename, /*shared=*/false, /*size=*/size, torch::kU8)
.clone();
#else
auto data =
torch::from_file(filename, /*shared=*/false, /*size=*/size, torch::kU8);
#endif
return data;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment