Emulate resetting the warnings registry for python 2.x

This commit is contained in:
Anthony Sottile 2018-10-18 10:40:47 -07:00
parent 1b51497936
commit cdd0e18ca8
2 changed files with 13 additions and 1 deletions

View File

@ -0,0 +1 @@
Fix filename reported by ``warnings.warn`` when using ``recwarn`` under python2.

View File

@ -158,6 +158,17 @@ class WarningsRecorder(warnings.catch_warnings):
def warn(*args, **kwargs):
kwargs.setdefault("stacklevel", 1)
kwargs["stacklevel"] += 1
# emulate resetting the warn registry
f_globals = sys._getframe(kwargs["stacklevel"] - 1).f_globals
if "__warningregistry__" in f_globals:
orig = f_globals["__warningregistry__"]
f_globals["__warningregistry__"] = None
try:
return self._saved_warn(*args, **kwargs)
finally:
f_globals["__warningregistry__"] = orig
else:
return self._saved_warn(*args, **kwargs)
warnings.warn, self._saved_warn = warn, warnings.warn