Skip to content

FIX: Sanitize default filename. #9057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
# plus ' '
removed_chars = r'<>:"/\|?*\0 '
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)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat trick! Combining next() with a generator like that, I'll have to remember that one.

return default_filename

def switch_backends(self, FigureCanvasClass):
Expand Down