Skip to content

Commit 5426d7b

Browse files
committed
Translate encoder error codes to strings
When decoding, we use raise_oserror() to convert codec error codes to strings. Adapt that code to be used when encoding as well. Add a new helper function that returns the exception so we can still raise `from exc`. Ideally we'd replace the other two callers of raise_oserror() with `raise _get_oserror()`, but the former is part of the public API.
1 parent 1a98590 commit 5426d7b

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/PIL/ImageFile.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,19 @@
6363
# Helpers
6464

6565

66-
def raise_oserror(error):
66+
def _get_oserror(error, *, encoder=False):
6767
try:
6868
msg = Image.core.getcodecstatus(error)
6969
except AttributeError:
7070
msg = ERRORS.get(error)
7171
if not msg:
72-
msg = f"decoder error {error}"
73-
msg += " when reading image file"
74-
raise OSError(msg)
72+
msg = f"{'encoder' if encoder else 'decoder'} error {error}"
73+
msg += f" when {'writing' if encoder else 'reading'} image file"
74+
return OSError(msg)
75+
76+
77+
def raise_oserror(error):
78+
raise _get_oserror(error)
7579

7680

7781
def _tilesort(t):
@@ -551,8 +555,7 @@ def _encode_tile(im, fp, tile: list[_Tile], bufsize, fh, exc=None):
551555
# slight speedup: compress to real file object
552556
errcode = encoder.encode_to_file(fh, bufsize)
553557
if errcode < 0:
554-
msg = f"encoder error {errcode} when writing image file"
555-
raise OSError(msg) from exc
558+
raise _get_oserror(errcode, encoder=True) from exc
556559
finally:
557560
encoder.cleanup()
558561

0 commit comments

Comments
 (0)