Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 638c0a2

Browse filesBrowse files
committed
Simplify _api.warn_external on Python 3.12+
Python 3.12 added the `skip_file_prefixes` argument, which essentially does what this helper function does for us. Technically, I think the old implementation would set the stack level to the tests, if called in one, but this one doesn't. However, that shouldn't be a problem, as either 1) warnings are errors, or 2), we catch the warning with `pytest.warns` and don't see the stack level.
1 parent 9f49b07 commit 638c0a2
Copy full SHA for 638c0a2

File tree

Expand file treeCollapse file tree

1 file changed

+23
-13
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+23
-13
lines changed

‎lib/matplotlib/_api/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/_api/__init__.py
+23-13Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import functools
1414
import itertools
15+
import pathlib
1516
import re
1617
import sys
1718
import warnings
@@ -366,16 +367,25 @@ def warn_external(message, category=None):
366367
warnings.warn`` (or ``functools.partial(warnings.warn, stacklevel=2)``,
367368
etc.).
368369
"""
369-
frame = sys._getframe()
370-
for stacklevel in itertools.count(1):
371-
if frame is None:
372-
# when called in embedded context may hit frame is None
373-
break
374-
if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.(?!tests\.))",
375-
# Work around sphinx-gallery not setting __name__.
376-
frame.f_globals.get("__name__", "")):
377-
break
378-
frame = frame.f_back
379-
# preemptively break reference cycle between locals and the frame
380-
del frame
381-
warnings.warn(message, category, stacklevel)
370+
kwargs = {}
371+
if sys.version_info[:2] >= (3, 12):
372+
# Go to Python's `site-packages` or `lib` from an editable install.
373+
basedir = pathlib.Path(__file__).parents[2]
374+
kwargs['skip_file_prefixes'] = (str(basedir / 'matplotlib'),
375+
str(basedir / 'mpl_toolkits'))
376+
else:
377+
frame = sys._getframe()
378+
for stacklevel in itertools.count(1):
379+
if frame is None:
380+
# when called in embedded context may hit frame is None
381+
kwargs['stacklevel'] = stacklevel
382+
break
383+
if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.(?!tests\.))",
384+
# Work around sphinx-gallery not setting __name__.
385+
frame.f_globals.get("__name__", "")):
386+
kwargs['stacklevel'] = stacklevel
387+
break
388+
frame = frame.f_back
389+
# preemptively break reference cycle between locals and the frame
390+
del frame
391+
warnings.warn(message, category, **kwargs)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.