mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
bpo-44852: Support ignoring specific DeprecationWarnings wholesale in regrtest (GH-27634)
This commit is contained in:
parent
4f51fa9e2d
commit
a0a6d39295
4 changed files with 70 additions and 0 deletions
|
@ -13,6 +13,7 @@ import sysconfig
|
|||
import time
|
||||
import types
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
from .testresult import get_test_runner
|
||||
|
||||
|
@ -2053,3 +2054,31 @@ def infinite_recursion(max_depth=75):
|
|||
yield
|
||||
finally:
|
||||
sys.setrecursionlimit(original_depth)
|
||||
|
||||
def ignore_deprecations_from(module: str, *, like: str) -> object:
|
||||
token = object()
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
category=DeprecationWarning,
|
||||
module=module,
|
||||
message=like + fr"(?#support{id(token)})",
|
||||
)
|
||||
return token
|
||||
|
||||
def clear_ignored_deprecations(*tokens: object) -> None:
|
||||
if not tokens:
|
||||
raise ValueError("Provide token or tokens returned by ignore_deprecations_from")
|
||||
|
||||
new_filters = []
|
||||
for action, message, category, module, lineno in warnings.filters:
|
||||
if action == "ignore" and category is DeprecationWarning:
|
||||
if isinstance(message, re.Pattern):
|
||||
message = message.pattern
|
||||
if tokens:
|
||||
endswith = tuple(rf"(?#support{id(token)})" for token in tokens)
|
||||
if message.endswith(endswith):
|
||||
continue
|
||||
new_filters.append((action, message, category, module, lineno))
|
||||
if warnings.filters != new_filters:
|
||||
warnings.filters[:] = new_filters
|
||||
warnings._filters_mutated()
|
||||
|
|
|
@ -187,3 +187,13 @@ def save_restore_warnings_filters():
|
|||
yield
|
||||
finally:
|
||||
warnings.filters[:] = old_filters
|
||||
|
||||
|
||||
def _warn_about_deprecation():
|
||||
warnings.warn(
|
||||
"This is used in test_support test to ensure"
|
||||
" support.ignore_deprecations_from() works as expected."
|
||||
" You should not be seeing this.",
|
||||
DeprecationWarning,
|
||||
stacklevel=0,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue