diff -r 3bbe18aaea8c Lib/formatter.py --- a/Lib/formatter.py Tue Apr 14 10:35:43 2015 -0400 +++ b/Lib/formatter.py Fri Apr 24 11:12:57 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 3bbe18aaea8c Lib/imp.py --- a/Lib/imp.py Tue Apr 14 10:35:43 2015 -0400 +++ b/Lib/imp.py Fri Apr 24 11:12:57 2015 -0400 @@ -29,7 +29,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 3bbe18aaea8c Lib/warnings.py --- a/Lib/warnings.py Tue Apr 14 10:35:43 2015 -0400 +++ b/Lib/warnings.py Fri Apr 24 11:12:57 2015 -0400 @@ -7,6 +7,21 @@ "resetwarnings", "catch_warnings"] +def _calc_stacklevel(skip_substring): + """Find the first frame that doesn't have skip_substring in the filename.""" + try: + # Start two levels down to skip this function and the call location. + level = 2 + while True: + frame = sys._getframe(level) + if skip_substring not in frame.f_code.co_filename: + break + level += 1 + 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: