diff -r 748c55375225 Lib/formatter.py --- a/Lib/formatter.py Fri May 29 09:06:24 2015 -0400 +++ b/Lib/formatter.py Fri May 29 13:39:54 2015 -0400 @@ -21,7 +21,8 @@ import sys import warnings warnings.warn('the formatter module is deprecated and will be removed in ' - 'Python 3.6', DeprecationWarning, stacklevel=2) + 'Python 3.6', DeprecationWarning, + stacklevel=warnings._calc_stacklevel(['_bootstrap'])) AS_IS = None diff -r 748c55375225 Lib/imp.py --- a/Lib/imp.py Fri May 29 09:06:24 2015 -0400 +++ b/Lib/imp.py Fri May 29 13:39:54 2015 -0400 @@ -30,7 +30,8 @@ warnings.warn("the imp module is deprecated in favour of importlib; " "see the module's documentation for alternative uses", - PendingDeprecationWarning, stacklevel=2) + PendingDeprecationWarning, + stacklevel=warnings._calc_stacklevel(['_bootstrap'])) # DEPRECATED SEARCH_ERROR = 0 diff -r 748c55375225 Lib/warnings.py --- a/Lib/warnings.py Fri May 29 09:06:24 2015 -0400 +++ b/Lib/warnings.py Fri May 29 13:39:54 2015 -0400 @@ -7,6 +7,27 @@ "resetwarnings", "catch_warnings"] +def _calc_stacklevel(skip_substrings): + """Find the first frame that doesn't have a member of skip_substrings. + + The depth will be 2 at minimum in order to skip the caller of this function + along with the call itself. + """ + skip_substrings = frozenset(skip_substrings) + try: + level = 2 + while True: + frame = sys._getframe(level) + filename = frame.f_code.co_filename + if any(skip in filename for skip in skip_substrings): + level += 1 + else: + break + except (AttributeError, ValueError): + level = 1 + return level + + def showwarning(message, category, filename, lineno, file=None, line=None): """Hook to write a warning to a file; replace if you like.""" if file is None: