Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f481a6e

Browse files
authored
Merge pull request #11298 from anntzer/autostacklevel
Automagically set the stacklevel on warnings.
2 parents ebac433 + fa1decd commit f481a6e

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ def _plot_args_replacer(args, data):
6464
except ValueError:
6565
pass
6666
else:
67-
warnings.warn(
67+
cbook._warn_external(
6868
"Second argument {!r} is ambiguous: could be a color spec but "
6969
"is in data; using as data. Either rename the entry in data "
7070
"or use three arguments to plot.".format(args[1]),
71-
RuntimeWarning, stacklevel=3)
71+
RuntimeWarning)
7272
return ["x", "y"]
7373
elif len(args) == 3:
7474
return ["x", "y", "c"]

lib/matplotlib/cbook/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import glob
1515
import gzip
1616
import io
17-
from itertools import repeat
17+
import itertools
1818
import locale
1919
import numbers
2020
import operator
@@ -1252,7 +1252,7 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
12521252

12531253
ncols = len(X)
12541254
if labels is None:
1255-
labels = repeat(None)
1255+
labels = itertools.repeat(None)
12561256
elif len(labels) != ncols:
12571257
raise ValueError("Dimensions of labels and X must be compatible")
12581258

@@ -2030,3 +2030,21 @@ def _setattr_cm(obj, **kwargs):
20302030
delattr(obj, attr)
20312031
else:
20322032
setattr(obj, attr, orig)
2033+
2034+
2035+
def _warn_external(message, category=None):
2036+
"""
2037+
`warnings.warn` wrapper that sets *stacklevel* to "outside Matplotlib".
2038+
2039+
The original emitter of the warning can be obtained by patching this
2040+
function back to `warnings.warn`, i.e. ``cbook._warn_external =
2041+
warnings.warn`` (or ``functools.partial(warnings.warn, stacklevel=2)``,
2042+
etc.).
2043+
"""
2044+
frame = sys._getframe()
2045+
for stacklevel in itertools.count(1):
2046+
if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)",
2047+
frame.f_globals["__name__"]):
2048+
break
2049+
frame = frame.f_back
2050+
warnings.warn(message, category, stacklevel)

0 commit comments

Comments
 (0)