[issue23810] Suboptimal stacklevel of deprecation warnings for formatter and imp modules

Brett Cannon report at bugs.python.org
Mon Mar 30 15:33:21 CEST 2015


Brett Cannon added the comment:

Probably need to introduce a new keyword argument just for deprecated imports or some helper function in importlib to get the stack depth right (else there is a risk of breaking the stack depth in any minor release whenever importlib's depth shifts). Something like the following should be enough (obviously done in warnings instead of per-file):

try:
    level = 1
    while True:
        frame = sys._getframe(level)
        print(frame.f_code.co_filename)
        if '_bootstrap' not in frame.f_code.co_filename:
            break
        level += 1
except ValueError:
    pass
print(sys._getframe(2).f_code.co_filename)
warnings.warn("the imp module is deprecated in favour of importlib; "
              "see the module's documentation for alternative uses",
              PendingDeprecationWarning, stacklevel=level+1)

Otherwise the depths should just go back to what they were at.

----------
nosy: +larry
priority: normal -> release blocker
stage:  -> test needed
type:  -> behavior

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23810>
_______________________________________


More information about the Python-bugs-list mailing list