Skip to content

Commit

Permalink
Merge pull request #6071 from radarhere/handler
Browse files Browse the repository at this point in the history
Fixed BUFR, GRIB and HDF5 stub saving
  • Loading branch information
hugovk committed Feb 19, 2022
2 parents de06aba + 3f5fad3 commit b78e601
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
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

0 comments on commit b78e601

Please sign in to comment.