From 586697ea0508998044700f29029c44b86bf82798 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 18 Aug 2017 05:28:10 -0700 Subject: [PATCH 1/2] Sanitize default filename. I would actually think that we should not do any sanitization ("you get what you asked for"), but given that there was some opposition last time I tried to restore the space in the default filename ("Figure 1.png"), even though the space is totally innocuous for the filesystem, we may as well fix the filenames that are *actually* problematic. --- lib/matplotlib/backend_bases.py | 42 ++++++++++++++++----------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 2bb642acf5a0..7c28e8194b0a 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -65,6 +65,7 @@ from matplotlib.path import Path from matplotlib.cbook import mplDeprecation, warn_deprecated import matplotlib.backend_tools as tools +import itertools try: from PIL import Image @@ -2265,7 +2266,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, self.figure.set_edgecolor(origedgecolor) self.figure.set_canvas(self) self._is_saving = False - #self.figure.canvas.draw() ## seems superfluous return result @classmethod @@ -2286,32 +2286,32 @@ def get_window_title(self): return self.manager.get_window_title() def set_window_title(self, title): - """ - Set the title text of the window containing the figure. Note that - this has no effect if there is no window (e.g., a PS backend). + """Set the title text of the window containing the figure. + + This has no effect if there is no window (e.g., a PS backend). """ if hasattr(self, "manager"): self.manager.set_window_title(title) def get_default_filename(self): - """ - Return a string, which includes extension, suitable for use as - a default filename. - """ - default_basename = self.get_window_title() or 'image' - default_basename = default_basename.replace(' ', '_') + """Return a suitable default filename, including the extension. + """ + default_basename = self.get_window_title() or "image" + # Characters to be avoided in a NT path: + # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions + removed_chars = \ + {"posix": " /\0", "nt": r'<>:"/\|?*\0'}.get(os.name, "_") + default_basename = default_basename.translate( + {ord(c): "_" for c in removed_chars}) default_filetype = self.get_default_filetype() - default_filename = default_basename + '.' + default_filetype - - save_dir = os.path.expanduser(rcParams['savefig.directory']) - - # ensure non-existing filename in save dir - i = 1 - while os.path.isfile(os.path.join(save_dir, default_filename)): - # attach numerical count to basename - default_filename = '{0}-{1}.{2}'.format(default_basename, i, default_filetype) - i += 1 - + save_dir = os.path.expanduser(rcParams.get("savefig.directory", "")) + # Ensure non-existing filename in save dir. + default_filename = next( + filename for filename in itertools.chain( + ["{}.{}".format(default_basename, default_filetype)], + ("{}-{}.{}".format(default_basename, i, default_filetype) + for i in itertools.count(1))) + if not os.path.isfile(os.path.join(save_dir, filename))) return default_filename def switch_backends(self, FigureCanvasClass): From 715f5da3a2430b7b7ab59af65c246f1d06cc2b59 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 27 Aug 2017 13:57:42 -0400 Subject: [PATCH 2/2] MNT: use the same replace chars on all OS This makes the default suggested filename independent of the OS. --- lib/matplotlib/backend_bases.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 7c28e8194b0a..1c133233bcd4 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2299,8 +2299,8 @@ def get_default_filename(self): default_basename = self.get_window_title() or "image" # Characters to be avoided in a NT path: # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions - removed_chars = \ - {"posix": " /\0", "nt": r'<>:"/\|?*\0'}.get(os.name, "_") + # plus ' ' + removed_chars = r'<>:"/\|?*\0 ' default_basename = default_basename.translate( {ord(c): "_" for c in removed_chars}) default_filetype = self.get_default_filetype()