From bb997708c08dae01ac95104f1f33d8c08c4715d4 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 1 Apr 2022 11:46:32 -0400 Subject: [PATCH 1/5] FIX: account for constant deprecations in Pillow 9.1 https://pillow.readthedocs.io/en/stable/deprecations.html#constants https://pillow.readthedocs.io/en/stable/releasenotes/9.1.0.html#constants Image.None -> Image.Dither.None Image.ADAPTIVE -> Image.Palette.ADAPTIVE --- lib/matplotlib/backends/backend_pdf.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index b6ae9cf06686..071380edad00 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1700,8 +1700,19 @@ def _writeImg(self, data, id, smask=None): # Convert to indexed color if there are 256 colors or fewer # This can significantly reduce the file size num_colors = len(img_colors) - img = img.convert(mode='P', dither=Image.NONE, - palette=Image.ADAPTIVE, colors=num_colors) + # These constants were converted to IntEnums and deprecated in + # Pillow 9.2 + dither = ( + Image.Dither.NONE if hasattr(Image, 'Dither') + else Image.NONE + ) + pmode = ( + Image.Palette.ADAPTIVE if hasattr(Image, 'Palette') + else Image.ADAPTIVE + ) + img = img.convert( + mode='P', dither=dither, palette=pmode, colors=num_colors + ) png_data, bit_depth, palette = self._writePng(img) if bit_depth is None or palette is None: raise RuntimeError("invalid PNG header") From a367714544056bec4a1afc263ce04a8d46e0b84c Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 1 Apr 2022 17:49:47 -0400 Subject: [PATCH 2/5] FIX: leaking temporary files in animation saving --- lib/matplotlib/animation.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index fb8c033bbabe..023d90668206 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -468,15 +468,13 @@ def grab_frame(self, **savefig_kwargs): def finish(self): # Call run here now that all frame grabbing is done. All temp files # are available to be assembled. - self._run() - super().finish() # Will call clean-up - - def _cleanup(self): # Inline to finish() once cleanup() is removed. - super()._cleanup() - if self._tmpdir: - _log.debug('MovieWriter: clearing temporary path=%s', self._tmpdir) - self._tmpdir.cleanup() - + try: + self._run() + super().finish() + finally: + if self._tmpdir: + _log.debug('MovieWriter: clearing temporary path=%s', self._tmpdir) + self._tmpdir.cleanup() @writers.register('pillow') class PillowWriter(AbstractMovieWriter): From e3fea7ed00b665e535d5013820daf7171baafd83 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 1 Apr 2022 13:37:05 -0400 Subject: [PATCH 3/5] CI: add pip list to azure pipeline --- azure-pipelines.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index eafffb231a54..9c8ac1f4d7a4 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -138,6 +138,9 @@ stages: - script: env displayName: 'print env' + - script: pip list + displayName: 'print pip' + - bash: | PYTHONFAULTHANDLER=1 python -m pytest --junitxml=junit/test-results.xml -raR --maxfail=50 --timeout=300 --durations=25 --cov-report= --cov=lib -n 2 || [[ "$PYTHON_VERSION" = 'Pre' ]] From f930098dd67cea3cd47b081064827c0fcfdb0fe8 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 1 Apr 2022 18:18:12 -0400 Subject: [PATCH 4/5] STY: fix whitespace --- lib/matplotlib/animation.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 023d90668206..8b53eba50bf7 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -17,6 +17,7 @@ # * Can blit be enabled for movies? # * Need to consider event sources to allow clicking through multiple figures + import abc import base64 import contextlib @@ -473,9 +474,12 @@ def finish(self): super().finish() finally: if self._tmpdir: - _log.debug('MovieWriter: clearing temporary path=%s', self._tmpdir) + _log.debug( + 'MovieWriter: clearing temporary path=%s', self._tmpdir + ) self._tmpdir.cleanup() + @writers.register('pillow') class PillowWriter(AbstractMovieWriter): @classmethod From bd6f04bf3336ea1872942bb3b7159495c90f5d6d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 4 Apr 2022 11:00:35 -0400 Subject: [PATCH 5/5] MNT: shorter version of Pillow API shims Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/backends/backend_pdf.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 071380edad00..fd786789845f 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1702,14 +1702,8 @@ def _writeImg(self, data, id, smask=None): num_colors = len(img_colors) # These constants were converted to IntEnums and deprecated in # Pillow 9.2 - dither = ( - Image.Dither.NONE if hasattr(Image, 'Dither') - else Image.NONE - ) - pmode = ( - Image.Palette.ADAPTIVE if hasattr(Image, 'Palette') - else Image.ADAPTIVE - ) + dither = getattr(Image, 'Dither', Image).NONE + pmode = getattr(Image, 'Palette', Image).ADAPTIVE img = img.convert( mode='P', dither=dither, palette=pmode, colors=num_colors )