Changeset e070dc0 in sasview


Ignore:
Timestamp:
Aug 8, 2016 9:30:53 AM (8 years ago)
Author:
lewis
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
b61bd57
Parents:
9b7c596
Message:

Raise error if error reading binary file instead of crashing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/file_converter/c_ext/bsl_loader.c

    r535e181 re070dc0  
    141141/*                        ----- Instance Methods -----                       */ 
    142142 
    143 float reverse_float(const float in_float){ 
     143float reverse_float(const float in_float) { 
     144    // Reverse the order of the bytes of a float 
    144145    float retval; 
    145146    char *to_convert = (char *)&in_float; 
     
    163164    PyArrayObject *data; 
    164165 
     166    // Create a new numpy array to store the data in 
    165167    data = (PyArrayObject *)PyArray_SimpleNew(2, size, NPY_FLOAT); 
    166168 
     169    // Attempt to open the file specified 
    167170    input_file = fopen(self->params.filename, "rb"); 
    168171    if (!input_file) { 
     
    170173    } 
    171174 
     175    // Move the file cursor the the position where the data we're interested 
     176    // in begins 
    172177    frame_pos = self->params.n_pixels * self->params.n_rasters * self->params.frame; 
    173178    fseek(input_file, frame_pos*sizeof(float), SEEK_SET); 
     
    175180    for (raster = 0; raster < self->params.n_rasters; raster++) { 
    176181        for (pixel = 0; pixel < self->params.n_pixels; pixel++) { 
    177             fread(&cur_val, sizeof(float), 1, input_file); 
     182            // Try reading the file 
     183            if (fread(&cur_val, sizeof(float), 1, input_file) == 0) { 
     184                PyErr_SetString(PyExc_RuntimeError, "Error reading file or EOF reached."); 
     185                return NULL; 
     186            } 
     187 
     188            // Swap the order of the bytes read, if specified that we should do 
     189            // so in the header file 
    178190            if (self->params.swap_bytes == 0) 
    179191                cur_val = reverse_float(cur_val); 
     192 
     193            // Add the read value to the numpy array 
    180194            PyArray_SETITEM(data, PyArray_GETPTR2(data, raster, pixel), PyFloat_FromDouble(cur_val)); 
    181195        } 
Note: See TracChangeset for help on using the changeset viewer.