From 7d2289d678de6e840853e65bfa2032e05b9254f6 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. Co-authored-by: Greg Lucas MNT: remove unneeded access of savefig.directory --- lib/matplotlib/backend_bases.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index f4273bc03919..ab3104be0805 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2241,15 +2241,22 @@ def get_default_filetype(cls): def get_default_filename(self): """ - Return a string, which includes extension, suitable for use as - a default filename. - """ - basename = (self.manager.get_window_title() if self.manager is not None - else '') - basename = (basename or 'image').replace(' ', '_') - filetype = self.get_default_filetype() - filename = basename + '.' + filetype - return filename + Return a suitable default filename, including the extension. + """ + default_basename = ( + self.manager.get_window_title() + if self.manager is not None + else '' + ) + default_basename = default_basename 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() + return f'{default_basename}.{default_filetype}' @_api.deprecated("3.8") def switch_backends(self, FigureCanvasClass): From 4356bfecd51509c3cf4df433214223a9032b5b9f 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 ab3104be0805..28815f60630a 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2251,8 +2251,8 @@ def get_default_filename(self): default_basename = default_basename 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()