Do not load image during save if file extension is unknown by radarhere · Pull Request #8835 · python-pillow/Pillow
| # may mutate self! | |
| self._ensure_mutable() | |
| save_all = params.pop("save_all", False) | |
| self.encoderinfo = {**getattr(self, "encoderinfo", {}), **params} | |
| self.encoderconfig: tuple[Any, ...] = () | |
| preinit() | |
| filename_ext = os.path.splitext(filename)[1].lower() | |
| ext = filename_ext.decode() if isinstance(filename_ext, bytes) else filename_ext | |
| if not format: | |
| if ext not in EXTENSION: | |
| init() | |
| try: | |
| format = EXTENSION[ext] | |
| except KeyError as e: | |
| msg = f"unknown file extension: {ext}" | |
| raise ValueError(msg) from e |
where
| def _ensure_mutable(self) -> None: | |
| if self.readonly: | |
| self._copy() | |
| else: | |
| self.load() |
If the ValueError is hit, there is no need to have loaded the image first. Avoiding that is a performance improvement for this situation.
A test needed to be updated to continue testing a failed load operation, rather than an unknown file extension.