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

Skip to content

Commit 9088ee2

Browse files
committed
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.
1 parent 225a4a0 commit 9088ee2

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
22772277
self.figure.set_edgecolor(origedgecolor)
22782278
self.figure.set_canvas(self)
22792279
self._is_saving = False
2280-
#self.figure.canvas.draw() ## seems superfluous
22812280
return result
22822281

22832282
@classmethod
@@ -2298,32 +2297,32 @@ def get_window_title(self):
22982297
return self.manager.get_window_title()
22992298

23002299
def set_window_title(self, title):
2301-
"""
2302-
Set the title text of the window containing the figure. Note that
2303-
this has no effect if there is no window (e.g., a PS backend).
2300+
"""Set the title text of the window containing the figure.
2301+
2302+
This has no effect if there is no window (e.g., a PS backend).
23042303
"""
23052304
if hasattr(self, "manager"):
23062305
self.manager.set_window_title(title)
23072306

23082307
def get_default_filename(self):
2309-
"""
2310-
Return a string, which includes extension, suitable for use as
2311-
a default filename.
2312-
"""
2313-
default_basename = self.get_window_title() or 'image'
2314-
default_basename = default_basename.replace(' ', '_')
2308+
"""Return a suitable default filename, including the extension.
2309+
"""
2310+
default_basename = self.get_window_title() or "image"
2311+
# Characters to be avoided in a NT path:
2312+
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions
2313+
removed_chars = \
2314+
{"posix": " /\0", "nt": r'<>:"/\|?*\0'}.get(os.name, "_")
2315+
default_basename = default_basename.translate(
2316+
{ord(c): "_" for c in removed_chars})
23152317
default_filetype = self.get_default_filetype()
2316-
default_filename = default_basename + '.' + default_filetype
2317-
2318-
save_dir = os.path.expanduser(rcParams.get('savefig.directory', ''))
2319-
2320-
# ensure non-existing filename in save dir
2321-
i = 1
2322-
while os.path.isfile(os.path.join(save_dir, default_filename)):
2323-
# attach numerical count to basename
2324-
default_filename = '{0}-{1}.{2}'.format(default_basename, i, default_filetype)
2325-
i += 1
2326-
2318+
save_dir = os.path.expanduser(rcParams.get("savefig.directory", ""))
2319+
# Ensure non-existing filename in save dir.
2320+
default_filename = next(
2321+
filename for filename in itertools.chain(
2322+
["{}.{}".format(default_basename, default_filetype)],
2323+
("{}-{}.{}".format(default_basename, i, default_filetype)
2324+
for i in itertools.count(1)))
2325+
if not os.path.isfile(os.path.join(save_dir, filename)))
23272326
return default_filename
23282327

23292328
def switch_backends(self, FigureCanvasClass):

0 commit comments

Comments
 (0)