Skip to content

Fix escaping of nulls and "0" in default filenames. #29781

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

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,7 @@ def get_default_filename(self):
# 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 '
removed_chars = '<>:"/\\|?*\0 '
default_basename = default_basename.translate(
{ord(c): "_" for c in removed_chars})
default_filetype = self.get_default_filetype()
Expand Down Expand Up @@ -2730,23 +2730,24 @@ def resize(self, w, h):
"""For GUI backends, resize the window (in physical pixels)."""

def get_window_title(self):
"""
Return the title text of the window containing the figure, or None
if there is no window (e.g., a PS backend).
"""
return 'image'
"""Return the title text of the window containing the figure."""
return self._window_title

def set_window_title(self, title):
"""
Set the title text of the window containing the figure.

This has no effect for non-GUI (e.g., PS) backends.

Examples
--------
>>> fig = plt.figure()
>>> fig.canvas.manager.set_window_title('My figure')
"""
# This attribute is not defined in __init__ (but __init__ calls this
# setter), as derived classes (real GUI managers) will store this
# information directly on the widget; only the base (non-GUI) manager
# class needs a specific attribute for it (so that filename escaping
# can be checked in the test suite).
self._window_title = title


cursors = tools.cursors
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def test_canvas_ctor():


def test_get_default_filename():
assert plt.figure().canvas.get_default_filename() == 'image.png'
fig = plt.figure()
assert fig.canvas.get_default_filename() == "Figure_1.png"
fig.canvas.manager.set_window_title("0:1/2<3")
assert fig.canvas.get_default_filename() == "0_1_2_3.png"


def test_canvas_change():
Expand Down
Loading