Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing 'rb' mode for opening files #2077

Merged
merged 3 commits into from Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion satpy/readers/hrit_base.py
Expand Up @@ -188,7 +188,7 @@ def _get_hd(self, hdr_info):
"""Open the file, read and get the basic file header info and set the mda dictionary."""
hdr_map, variable_length_headers, text_headers = hdr_info

with utils.generic_open(self.filename) as fp:
with utils.generic_open(self.filename, mode='rb') as fp:
total_header_length = 16
while fp.tell() < total_header_length:
hdr_id = get_header_id(fp)
Expand Down
4 changes: 2 additions & 2 deletions satpy/readers/seviri_l1b_hrit.py
Expand Up @@ -246,7 +246,7 @@ def __init__(self, filename, filename_info, filetype_info, calib_mode='nominal',

def read_prologue(self):
"""Read the prologue metadata."""
with utils.generic_open(self.filename) as fp_:
with utils.generic_open(self.filename, mode='rb') as fp_:
fp_.seek(self.mda['total_header_length'])
data = np.frombuffer(fp_.read(hrit_prologue.itemsize), dtype=hrit_prologue, count=1)
self.prologue.update(recarray2dict(data))
Expand Down Expand Up @@ -319,7 +319,7 @@ def __init__(self, filename, filename_info, filetype_info, calib_mode='nominal',

def read_epilogue(self):
"""Read the epilogue metadata."""
with utils.generic_open(self.filename) as fp_:
with utils.generic_open(self.filename, mode='rb') as fp_:
fp_.seek(self.mda['total_header_length'])
data = np.frombuffer(fp_.read(hrit_epilogue.itemsize), dtype=hrit_epilogue, count=1)
self.epilogue.update(recarray2dict(data))
Expand Down
42 changes: 42 additions & 0 deletions satpy/tests/reader_tests/test_utils.py
Expand Up @@ -313,6 +313,48 @@ def test_generic_open(self, bz2_mock):

assert mock_bz2_open.read.called

def test_generic_open_text(self):
"""Test the bz2 file unzipping context manager using dummy text data."""
dummy_text_data = 'Hello'
dummy_text_filename = 'dummy.txt'
with open(dummy_text_filename, 'w') as f:
f.write(dummy_text_data)

with hf.generic_open(dummy_text_filename, 'r') as f:
read_text_data = f.read()

assert read_text_data == dummy_text_data

dummy_text_filename = 'dummy.txt.bz2'
with hf.bz2.open(dummy_text_filename, 'wt') as f:
f.write(dummy_text_data)

with hf.generic_open(dummy_text_filename, 'rt') as f:
read_text_data = f.read()

assert read_text_data == dummy_text_data

def test_generic_open_binary(self):
"""Test the bz2 file unzipping context manager using dummy binary data."""
dummy_binary_data = b'Hello'
dummy_binary_filename = 'dummy.dat'
with open(dummy_binary_filename, 'wb') as f:
f.write(dummy_binary_data)

with hf.generic_open(dummy_binary_filename, 'rb') as f:
read_binary_data = f.read()

assert read_binary_data == dummy_binary_data

dummy_binary_filename = 'dummy.dat.bz2'
with hf.bz2.open(dummy_binary_filename, 'wb') as f:
f.write(dummy_binary_data)

with hf.generic_open(dummy_binary_filename, 'rb') as f:
read_binary_data = f.read()

assert read_binary_data == dummy_binary_data

@mock.patch("os.remove")
@mock.patch("satpy.readers.utils.unzip_file", return_value='dummy.txt')
def test_pro_reading_gets_unzipped_file(self, fake_unzip_file, fake_remove):
Expand Down