Skip to content

Inappropriate file opening #54

@jwaiton

Description

@jwaiton

In this line you have a bare file opening:

    # open file
    file = open(file_path, 'rb')

    event_number, timestamp, samples, sampling_period = read_defaults_WD2(file, byte_order)
    # attempt to read channels
    channels        = int.from_bytes(file.read(4), byteorder=byte_order)

    # then read in a full collection of data, and see if the following header makes sense.
    # if it explicitly breaks, assume 1 channel, raise a warning and continue.
    try:
        dataset         = file.read(4*samples*channels)
        event_number_1, timestamp_1, samples_1, sampling_period_1 = read_defaults_WD2(file, byte_order)
    except MemoryError as e:
        warnings.warn("process_header() unable to read file, defaulting to 1-channel description.\nIf this is not what you expect, please ensure your data was collected correctly.")
        event_number_1 = -1
        samples_1 = -1
        sampling_period_1 = -1

    # check that event header is as expected
    if (event_number_1 -1 == event_number) and (samples_1 == samples) and sampling_period_1 == (sampling_period):
        print(f"{channels} channels detected. Processing accordingly...")
    else:
        print(f"Single channel detected. If you're expecting more channels, something has gone wrong.\nProcessing accordingly...")
        channels = 1

    file.close()

This is not good code, as the file could be left open if anything breaks within the code between its opening and its closing...

This should be resolved! Preferably with a with statement on the file

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status

    Done

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions