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

Fixed BUFR, GRIB and HDF5 stub saving #6071

Merged
merged 1 commit into from Feb 19, 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
32 changes: 32 additions & 0 deletions Tests/test_file_bufrstub.py
Expand Up @@ -45,3 +45,35 @@ def test_save(tmp_path):
# Act / Assert: stub cannot save without an implemented handler
with pytest.raises(OSError):
im.save(tmpfile)


def test_handler(tmp_path):
class TestHandler:
opened = False
loaded = False
saved = False

def open(self, im):
self.opened = True

def load(self, im):
self.loaded = True
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename):
self.saved = True

handler = TestHandler()
BufrStubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im:
assert handler.opened
assert not handler.loaded

im.load()
assert handler.loaded

temp_file = str(tmp_path / "temp.bufr")
im.save(temp_file)
assert handler.saved

BufrStubImagePlugin._handler = None
32 changes: 32 additions & 0 deletions Tests/test_file_gribstub.py
Expand Up @@ -45,3 +45,35 @@ def test_save(tmp_path):
# Act / Assert: stub cannot save without an implemented handler
with pytest.raises(OSError):
im.save(tmpfile)


def test_handler(tmp_path):
class TestHandler:
opened = False
loaded = False
saved = False

def open(self, im):
self.opened = True

def load(self, im):
self.loaded = True
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename):
self.saved = True

handler = TestHandler()
GribStubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im:
assert handler.opened
assert not handler.loaded

im.load()
assert handler.loaded

temp_file = str(tmp_path / "temp.grib")
im.save(temp_file)
assert handler.saved

GribStubImagePlugin._handler = None
32 changes: 32 additions & 0 deletions Tests/test_file_hdf5stub.py
Expand Up @@ -46,3 +46,35 @@ def test_save():
im.save(dummy_filename)
with pytest.raises(OSError):
Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename)


def test_handler(tmp_path):
class TestHandler:
opened = False
loaded = False
saved = False

def open(self, im):
self.opened = True

def load(self, im):
self.loaded = True
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename):
self.saved = True

handler = TestHandler()
Hdf5StubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im:
assert handler.opened
assert not handler.loaded

im.load()
assert handler.loaded

temp_file = str(tmp_path / "temp.h5")
im.save(temp_file)
assert handler.saved

Hdf5StubImagePlugin._handler = None
2 changes: 1 addition & 1 deletion src/PIL/BufrStubImagePlugin.py
Expand Up @@ -59,7 +59,7 @@ def _load(self):


def _save(im, fp, filename):
if _handler is None or not hasattr("_handler", "save"):
if _handler is None or not hasattr(_handler, "save"):
raise OSError("BUFR save handler not installed")
_handler.save(im, fp, filename)

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GribStubImagePlugin.py
Expand Up @@ -59,7 +59,7 @@ def _load(self):


def _save(im, fp, filename):
if _handler is None or not hasattr("_handler", "save"):
if _handler is None or not hasattr(_handler, "save"):
raise OSError("GRIB save handler not installed")
_handler.save(im, fp, filename)

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Hdf5StubImagePlugin.py
Expand Up @@ -59,7 +59,7 @@ def _load(self):


def _save(im, fp, filename):
if _handler is None or not hasattr("_handler", "save"):
if _handler is None or not hasattr(_handler, "save"):
raise OSError("HDF5 save handler not installed")
_handler.save(im, fp, filename)

Expand Down