mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-03-01 10:30:34 +03:00
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`.
This commit is contained in:
parent
1a98590697
commit
ec17dc11ba
|
@ -77,3 +77,9 @@ Calculating the :py:attr:`~PIL.ImageStat.Stat.count` and
|
||||||
:py:attr:`~PIL.ImageStat.Stat.extrema` statistics is now faster. After the
|
:py:attr:`~PIL.ImageStat.Stat.extrema` statistics is now faster. After the
|
||||||
histogram is created in ``st = ImageStat.Stat(im)``, ``st.count`` is 3x as fast
|
histogram is created in ``st = ImageStat.Stat(im)``, ``st.count`` is 3x as fast
|
||||||
on average and ``st.extrema`` is 12x as fast on average.
|
on average and ``st.extrema`` is 12x as fast on average.
|
||||||
|
|
||||||
|
Encoder errors now report error detail as string
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
:py:exc:`OSError` exceptions from image encoders now include a textual description of
|
||||||
|
the error instead of a numeric error code.
|
||||||
|
|
|
@ -63,15 +63,19 @@ Dict of known error codes returned from :meth:`.PyDecoder.decode`,
|
||||||
# Helpers
|
# Helpers
|
||||||
|
|
||||||
|
|
||||||
def raise_oserror(error):
|
def _get_oserror(error, *, encoder):
|
||||||
try:
|
try:
|
||||||
msg = Image.core.getcodecstatus(error)
|
msg = Image.core.getcodecstatus(error)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
msg = ERRORS.get(error)
|
msg = ERRORS.get(error)
|
||||||
if not msg:
|
if not msg:
|
||||||
msg = f"decoder error {error}"
|
msg = f"{'encoder' if encoder else 'decoder'} error {error}"
|
||||||
msg += " when reading image file"
|
msg += f" when {'writing' if encoder else 'reading'} image file"
|
||||||
raise OSError(msg)
|
return OSError(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def raise_oserror(error):
|
||||||
|
raise _get_oserror(error, encoder=False)
|
||||||
|
|
||||||
|
|
||||||
def _tilesort(t):
|
def _tilesort(t):
|
||||||
|
@ -551,8 +555,7 @@ def _encode_tile(im, fp, tile: list[_Tile], bufsize, fh, exc=None):
|
||||||
# slight speedup: compress to real file object
|
# slight speedup: compress to real file object
|
||||||
errcode = encoder.encode_to_file(fh, bufsize)
|
errcode = encoder.encode_to_file(fh, bufsize)
|
||||||
if errcode < 0:
|
if errcode < 0:
|
||||||
msg = f"encoder error {errcode} when writing image file"
|
raise _get_oserror(errcode, encoder=True) from exc
|
||||||
raise OSError(msg) from exc
|
|
||||||
finally:
|
finally:
|
||||||
encoder.cleanup()
|
encoder.cleanup()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user