diff --git a/thirdparty/cnpy/cnpy.cpp b/thirdparty/cnpy/cnpy.cpp index ed277deb5fe..ea89bea9c08 100644 --- a/thirdparty/cnpy/cnpy.cpp +++ b/thirdparty/cnpy/cnpy.cpp @@ -12,6 +12,10 @@ #include #include +#define assert_throw(expression) \ + if (!(expression)) \ + throw std::runtime_error(#expression) + char cnpy::BigEndianTest() { int x = 1; return (((char *)&x)[0]) ? '<' : '>'; @@ -93,10 +97,10 @@ void cnpy::parse_npy_header(unsigned char* buffer,size_t& word_size, std::vector bool littleEndian = false; if (loc1 < header.size()) littleEndian = (header[loc1] == '<' || header[loc1] == '|' ? true : false); - assert(littleEndian); + assert_throw(littleEndian); //char type = header[loc1+1]; - //assert(type == map_type(T)); + //assert_throw(type == map_type(T)); std::string str_ws = header.substr(loc1+2); loc2 = str_ws.find("'"); @@ -116,7 +120,7 @@ void cnpy::parse_npy_header(FILE* fp, size_t& word_size, std::vector& sh else { header = ""; } - assert(header[header.size()-1] == '\n'); + assert_throw(header[header.size()-1] == '\n'); size_t loc1, loc2; @@ -153,10 +157,10 @@ void cnpy::parse_npy_header(FILE* fp, size_t& word_size, std::vector& sh bool littleEndian = false; if (loc1 < header.size()) littleEndian = (header[loc1] == '<' || header[loc1] == '|' ? true : false); - assert(littleEndian); + assert_throw(littleEndian); //char type = header[loc1+1]; - //assert(type == map_type(T)); + //assert_throw(type == map_type(T)); std::string str_ws = header.substr(loc1+2); loc2 = str_ws.find("'"); @@ -180,10 +184,10 @@ void cnpy::parse_zip_footer(FILE* fp, uint16_t& nrecs, size_t& global_header_siz global_header_offset = *(uint32_t*) &footer[16]; comment_len = *(uint16_t*) &footer[20]; - assert(disk_no == 0); - assert(disk_start == 0); - assert(nrecs_on_disk == nrecs); - assert(comment_len == 0); + assert_throw(disk_no == 0); + assert_throw(disk_start == 0); + assert_throw(nrecs_on_disk == nrecs); + assert_throw(comment_len == 0); } cnpy::NpyArray load_the_npy_file(FILE* fp) { @@ -356,11 +360,15 @@ cnpy::NpyArray cnpy::npy_load(std::string fname) { if(!fp) throw std::runtime_error("npy_load: Unable to open file "+fname); - NpyArray arr = load_the_npy_file(fp); + try { + NpyArray arr = load_the_npy_file(fp); + fclose(fp); + return arr; + } catch (...) { + fclose(fp); + throw; + } - fclose(fp); - return arr; } -