Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21c7730

Browse files
authored
bpo-32089: Use default action for ResourceWarning (#4584)
In development and debug mode, use the "default" action, rather than the "always" action, for ResourceWarning in the default warnings filters.
1 parent c172fc5 commit 21c7730

4 files changed

Lines changed: 10 additions & 21 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -532,26 +532,26 @@ def test_xdev(self):
532532
out = self.run_xdev("-c", code)
533533
self.assertEqual(out,
534534
"ignore::BytesWarning "
535-
"always::ResourceWarning "
535+
"default::ResourceWarning "
536536
"default::Warning")
537537

538538
out = self.run_xdev("-b", "-c", code)
539539
self.assertEqual(out,
540540
"default::BytesWarning "
541-
"always::ResourceWarning "
541+
"default::ResourceWarning "
542542
"default::Warning")
543543

544544
out = self.run_xdev("-bb", "-c", code)
545545
self.assertEqual(out,
546546
"error::BytesWarning "
547-
"always::ResourceWarning "
547+
"default::ResourceWarning "
548548
"default::Warning")
549549

550550
out = self.run_xdev("-Werror", "-c", code)
551551
self.assertEqual(out,
552552
"error::Warning "
553553
"ignore::BytesWarning "
554-
"always::ResourceWarning "
554+
"default::ResourceWarning "
555555
"default::Warning")
556556

557557
try:
@@ -573,19 +573,6 @@ def test_xdev(self):
573573
out = self.run_xdev("-c", code)
574574
self.assertEqual(out, "True")
575575

576-
# Make sure that ResourceWarning emitted twice at the same line number
577-
# is logged twice
578-
filename = support.TESTFN
579-
self.addCleanup(support.unlink, filename)
580-
with open(filename, "w", encoding="utf8") as fp:
581-
print("def func(): open(__file__)", file=fp)
582-
print("func()", file=fp)
583-
print("func()", file=fp)
584-
fp.flush()
585-
586-
out = self.run_xdev(filename)
587-
self.assertEqual(out.count(':1: ResourceWarning: '), 2, out)
588-
589576

590577
class IgnoreEnvironmentTest(unittest.TestCase):
591578

Lib/warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def _filters_mutated():
540540

541541
# resource usage warnings are enabled by default in pydebug mode
542542
if dev_mode or py_debug:
543-
resource_action = "always"
543+
resource_action = "default"
544544
else:
545545
resource_action = "ignore"
546546
simplefilter(resource_action, category=ResourceWarning, append=1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
warnings: In development (-X dev) and debug mode (pydebug build), use the
2+
"default" action for ResourceWarning, rather than the "always" action, in
3+
the default warnings filters.

Python/_warnings.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ _Py_IDENTIFIER(argv);
1313
_Py_IDENTIFIER(stderr);
1414
_Py_IDENTIFIER(ignore);
1515
_Py_IDENTIFIER(error);
16-
_Py_IDENTIFIER(always);
1716
_Py_static_string(PyId_default, "default");
1817

1918
static int
@@ -1208,9 +1207,9 @@ init_filters(const _PyCoreConfig *config)
12081207
_Py_Identifier *resource_action;
12091208
/* resource usage warnings are enabled by default in pydebug mode */
12101209
#ifdef Py_DEBUG
1211-
resource_action = &PyId_always;
1210+
resource_action = &PyId_default;
12121211
#else
1213-
resource_action = (dev_mode ? &PyId_always : &PyId_ignore);
1212+
resource_action = (dev_mode ? &PyId_default: &PyId_ignore);
12141213
#endif
12151214
PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
12161215
resource_action));

0 commit comments

Comments
 (0)