Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d2289d

Browse files
anntzertacaswell
authored andcommitted
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 <[email protected]> MNT: remove unneeded access of savefig.directory
1 parent 7157a19 commit 7d2289d

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,15 +2241,22 @@ def get_default_filetype(cls):
22412241

22422242
def get_default_filename(self):
22432243
"""
2244-
Return a string, which includes extension, suitable for use as
2245-
a default filename.
2246-
"""
2247-
basename = (self.manager.get_window_title() if self.manager is not None
2248-
else '')
2249-
basename = (basename or 'image').replace(' ', '_')
2250-
filetype = self.get_default_filetype()
2251-
filename = basename + '.' + filetype
2252-
return filename
2244+
Return a suitable default filename, including the extension.
2245+
"""
2246+
default_basename = (
2247+
self.manager.get_window_title()
2248+
if self.manager is not None
2249+
else ''
2250+
)
2251+
default_basename = default_basename or 'image'
2252+
# Characters to be avoided in a NT path:
2253+
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions
2254+
removed_chars = \
2255+
{"posix": " /\0", "nt": r'<>:"/\|?*\0'}.get(os.name, "_")
2256+
default_basename = default_basename.translate(
2257+
{ord(c): "_" for c in removed_chars})
2258+
default_filetype = self.get_default_filetype()
2259+
return f'{default_basename}.{default_filetype}'
22532260

22542261
@_api.deprecated("3.8")
22552262
def switch_backends(self, FigureCanvasClass):

0 commit comments

Comments
 (0)