Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
gh-39615: Add warnings.warn() skip_file_prefixes support#100840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -12,6 +12,7 @@ | ||
from test.support import warnings_helper | ||
from test.support.script_helper import assert_python_ok, assert_python_failure | ||
from test.test_warnings.data import package_helper | ||
from test.test_warnings.data import stacklevel as warning_tests | ||
import warnings as original_warnings | ||
@@ -472,6 +473,42 @@ def test_stacklevel_import(self): | ||
self.assertEqual(len(w), 1) | ||
self.assertEqual(w[0].filename, __file__) | ||
def test_skip_file_prefixes(self): | ||
with warnings_state(self.module): | ||
with original_warnings.catch_warnings(record=True, | ||
module=self.module) as w: | ||
self.module.simplefilter('always') | ||
# Warning never attributed to the data/ package. | ||
package_helper.inner_api( | ||
"inner_api", stacklevel=2, | ||
warnings_module=warning_tests.warnings) | ||
self.assertEqual(w[-1].filename, __file__) | ||
warning_tests.package("package api", stacklevel=2) | ||
self.assertEqual(w[-1].filename, __file__) | ||
self.assertEqual(w[-2].filename, w[-1].filename) | ||
# Low stacklevels are overridden to 2 behavior. | ||
warning_tests.package("package api 1", stacklevel=1) | ||
self.assertEqual(w[-1].filename, __file__) | ||
warning_tests.package("package api 0", stacklevel=0) | ||
self.assertEqual(w[-1].filename, __file__) | ||
warning_tests.package("package api -99", stacklevel=-99) | ||
self.assertEqual(w[-1].filename, __file__) | ||
# The stacklevel still goes up out of the package. | ||
warning_tests.package("prefix02", stacklevel=3) | ||
self.assertIn("unittest", w[-1].filename) | ||
def test_skip_file_prefixes_type_errors(self): | ||
with warnings_state(self.module): | ||
warn = warning_tests.warnings.warn | ||
with self.assertRaises(TypeError): | ||
warn("msg", skip_file_prefixes=[]) | ||
with self.assertRaises(TypeError): | ||
warn("msg", skip_file_prefixes=(b"bytes",)) | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
with self.assertRaises(TypeError): | ||
warn("msg", skip_file_prefixes="a sequence of strs") | ||
def test_exec_filename(self): | ||
filename = "<warnings-test>" | ||
codeobj = compile(("import warnings\n" | ||
@@ -895,7 +932,7 @@ def test_formatwarning(self): | ||
message = "msg" | ||
category = Warning | ||
file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' | ||
line_num =5 | ||
file_line = linecache.getline(file_name, line_num).strip() | ||
format = "%s:%s: %s: %s\n %s\n" | ||
expect = format % (file_name, line_num, category.__name__, message, | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# helper to the helper for testing skip_file_prefixes. | ||
import os | ||
package_path = os.path.dirname(__file__) | ||
def inner_api(message, *, stacklevel, warnings_module): | ||
warnings_module.warn( | ||
message, stacklevel=stacklevel, | ||
skip_file_prefixes=(package_path,)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
# Helper module for testing stacklevel and skip_file_prefixes arguments | ||
# of warnings.warn() | ||
import warnings | ||
from test.test_warnings.data import package_helper | ||
def outer(message, stacklevel=1): | ||
inner(message, stacklevel) | ||
def inner(message, stacklevel=1): | ||
warnings.warn(message, stacklevel=stacklevel) | ||
def package(message, *, stacklevel): | ||
package_helper.inner_api(message, stacklevel=stacklevel, | ||
warnings_module=warnings) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:func:`warnings.warn` now has the ability to skip stack frames based on code | ||
filename prefix rather than only a numeric ``stacklevel`` via the new | ||
``skip_file_prefixes`` keyword argument. |
Uh oh!
There was an error while loading.Please reload this page.