Skip to content

Commit 1969c21

Browse files
tacaswelltimhoffm
authored andcommitted
Backport PR #12771: Do not rely on external stack frame to exist (#12782)
Conflicts: lib/matplotlib/cbook/__init__.py - also backported lgtm noqa flag
1 parent beb254a commit 1969c21

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/matplotlib/cbook/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,10 @@ def _warn_external(message, category=None):
20062006
etc.).
20072007
"""
20082008
frame = sys._getframe()
2009-
for stacklevel in itertools.count(1):
2009+
for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable]
2010+
if frame is None:
2011+
# when called in embedded context may hit frame is None
2012+
break
20102013
if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)",
20112014
frame.f_globals["__name__"]):
20122015
break

lib/matplotlib/tests/test_cbook.py

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pickle
33
from weakref import ref
44
import warnings
5+
from unittest.mock import patch, Mock
56

67
from datetime import datetime
78

@@ -350,6 +351,15 @@ def test_normalize_kwargs_pass(inp, expected, kwargs_to_norm):
350351
assert len(w) == 0
351352

352353

354+
def test_warn_external_frame_embedded_python():
355+
with patch.object(cbook, "sys") as mock_sys:
356+
mock_sys._getframe = Mock(return_value=None)
357+
with warnings.catch_warnings(record=True) as w:
358+
cbook._warn_external("dummy")
359+
assert len(w) == 1
360+
assert str(w[0].message) == "dummy"
361+
362+
353363
def test_to_prestep():
354364
x = np.arange(4)
355365
y1 = np.arange(4)

0 commit comments

Comments
 (0)