From 90747a04b1dfe16ea3808974402d20dda0331347 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 19 Mar 2025 23:49:21 +0100 Subject: [PATCH] Fix escaping of nulls and "0" in default filenames. Adding a test for this required tweaking a bit the behavior of non-GUI managers (so that they also store the window title); note that the comment of non-GUIs returning None was clearly wrong. --- lib/matplotlib/backend_bases.py | 17 +++++++++-------- lib/matplotlib/tests/test_backend_bases.py | 5 ++++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 62c26a90a91c..2c22808800a8 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -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() @@ -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 diff --git a/lib/matplotlib/tests/test_backend_bases.py b/lib/matplotlib/tests/test_backend_bases.py index ef5a52d988bb..30db5ebf5511 100644 --- a/lib/matplotlib/tests/test_backend_bases.py +++ b/lib/matplotlib/tests/test_backend_bases.py @@ -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(): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy