Skip to content

Commit a399218

Browse files
committed
Fix warn mode to work when warnings are configured as errors
Use warnings.catch_warnings() context manager to ensure ImportWarning is always emitted and never converted to an error, even when the test environment has warnings configured with -W error.
1 parent 84c9e5b commit a399218

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Lib/multiprocessing/forkserver.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,13 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None,
244244
raise
245245
case 'warn':
246246
import warnings
247-
warnings.warn(
248-
f"Failed to import __main__ from {main_path!r}: {e}",
249-
ImportWarning,
250-
stacklevel=2
251-
)
247+
with warnings.catch_warnings():
248+
warnings.simplefilter('always', ImportWarning)
249+
warnings.warn(
250+
f"Failed to import __main__ from {main_path!r}: {e}",
251+
ImportWarning,
252+
stacklevel=2
253+
)
252254
case 'ignore':
253255
pass
254256
finally:
@@ -262,11 +264,13 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None,
262264
raise
263265
case 'warn':
264266
import warnings
265-
warnings.warn(
266-
f"Failed to preload module {modname!r}: {e}",
267-
ImportWarning,
268-
stacklevel=2
269-
)
267+
with warnings.catch_warnings():
268+
warnings.simplefilter('always', ImportWarning)
269+
warnings.warn(
270+
f"Failed to preload module {modname!r}: {e}",
271+
ImportWarning,
272+
stacklevel=2
273+
)
270274
case 'ignore':
271275
pass
272276

0 commit comments

Comments
 (0)