Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-146358: Fix warnings.catch_warnings on Free Threading#146374

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

Merged
vstinner merged 3 commits intopython:mainfromvstinner:warnings
Mar 25, 2026
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletionsLib/_py_warnings.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,18 +703,18 @@ def __enter__(self):
context=None
self._filters=self._module.filters
self._module.filters=self._filters[:]
self._showwarning=self._module.showwarning
self._showwarnmsg_impl=self._module._showwarnmsg_impl
self._showwarning=self._module.showwarning
self._module._filters_mutated_lock_held()
ifself._record:
if_use_context:
context.log=log= []
else:
log= []
self._module._showwarnmsg_impl=log.append
# Reset showwarning() to the default implementation to make sure
# that _showwarnmsg() calls _showwarnmsg_impl()
self._module.showwarning=self._module._showwarning_orig
# Reset showwarning() to the default implementation to make sure
# that _showwarnmsg() calls _showwarnmsg_impl()
self._module.showwarning=self._module._showwarning_orig
else:
log=None
ifself._filterisnotNone:
Expand All@@ -729,8 +729,8 @@ def __exit__(self, *exc_info):
self._module._warnings_context.set(self._saved_context)
else:
self._module.filters=self._filters
self._module.showwarning=self._showwarning
self._module._showwarnmsg_impl=self._showwarnmsg_impl
self._module.showwarning=self._showwarning
self._module._filters_mutated_lock_held()


Expand Down
42 changes: 42 additions & 0 deletionsLib/test/test_warnings/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
fromcontextlibimportcontextmanager
importlinecache
importlogging
importos
importimportlib
importinspect
Expand DownExpand Up@@ -509,6 +510,47 @@ def test_catchwarnings_with_simplefilter_error(self):
stderr=stderr.getvalue()
self.assertIn(error_msg,stderr)

deftest_catchwarnings_with_showwarning(self):
# gh-146358: catch_warnings must override warnings.showwarning()
# if it's not the default implementation.

warns= []
defcustom_showwarning(message,category,filename,lineno,
file=None,line=None):
warns.append(message)

withself.module.catch_warnings():
self.module.resetwarnings()

withsupport.swap_attr(self.module,'showwarning',
custom_showwarning):
withself.module.catch_warnings(record=True)asrecorded:
self.module.warn("recorded")
self.assertEqual(len(recorded),1)
self.assertEqual(str(recorded[0].message),'recorded')
self.assertIs(self.module.showwarning,custom_showwarning)

self.module.warn("custom")

self.assertEqual(len(warns),1)
self.assertEqual(str(warns[0]),"custom")

deftest_catchwarnings_logging(self):
# gh-146358: catch_warnings(record=True) must replace the
# showwarning() function set by logging.captureWarnings(True).

withself.module.catch_warnings():
self.module.resetwarnings()
logging.captureWarnings(True)

withself.module.catch_warnings(record=True)asrecorded:
self.module.warn("recorded")
self.assertEqual(len(recorded),1)
self.assertEqual(str(recorded[0].message),'recorded')

logging.captureWarnings(False)


classCFilterTests(FilterTests,unittest.TestCase):
module=c_warnings

Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp