From 48832dcf9550c99ffc312ef95cc76460a24451be Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Wed, 7 Nov 2018 18:26:59 -0800 Subject: [PATCH 1/2] Do not rely on external stack frame to exist When using matplotlib in an embedded Python interpreter (such as in Julia package PyCall.jl), stack frame may not exist outside matplotlib. Therefore, it is better to not assume `frame.f_back` to be an existing call frame. See: https://github.com/JuliaPy/PyPlot.jl/issues/409 --- lib/matplotlib/cbook/__init__.py | 2 ++ lib/matplotlib/tests/test_cbook.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 22c804b88da5..63d8e2da3f37 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1990,6 +1990,8 @@ def _warn_external(message, category=None): """ frame = sys._getframe() for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable] + if frame is None: + break if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)", # Work around sphinx-gallery not setting __name__. frame.f_globals.get("__name__", "")): diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 69b887020d91..6c250ed524e5 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -2,6 +2,7 @@ import pickle from weakref import ref import warnings +from unittest.mock import patch, Mock from datetime import datetime @@ -350,6 +351,15 @@ def test_normalize_kwargs_pass(inp, expected, kwargs_to_norm): assert len(w) == 0 +def test_warn_external_frame_embedded_python(): + with patch.object(cbook, "sys") as mock_sys: + mock_sys._getframe = Mock(return_value=None) + with warnings.catch_warnings(record=True) as w: + cbook._warn_external("dummy") + assert len(w) == 1 + assert str(w[0].message) == "dummy" + + def test_to_prestep(): x = np.arange(4) y1 = np.arange(4) From 052c6338d3355141925678a6494453dd2906c3b9 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 8 Nov 2018 10:02:16 -0500 Subject: [PATCH 2/2] DOC: add code comment --- lib/matplotlib/cbook/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 63d8e2da3f37..8323d7e67373 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1991,6 +1991,7 @@ def _warn_external(message, category=None): frame = sys._getframe() for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable] if frame is None: + # when called in embedded context may hit frame is None break if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)", # Work around sphinx-gallery not setting __name__.