mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									c172fc5031
								
							
						
					
					
						commit
						21c7730761
					
				
					 4 changed files with 10 additions and 21 deletions
				
			
		|  | @ -532,26 +532,26 @@ def test_xdev(self): | ||||||
|         out = self.run_xdev("-c", code) |         out = self.run_xdev("-c", code) | ||||||
|         self.assertEqual(out, |         self.assertEqual(out, | ||||||
|                          "ignore::BytesWarning " |                          "ignore::BytesWarning " | ||||||
|                          "always::ResourceWarning " |                          "default::ResourceWarning " | ||||||
|                          "default::Warning") |                          "default::Warning") | ||||||
| 
 | 
 | ||||||
|         out = self.run_xdev("-b", "-c", code) |         out = self.run_xdev("-b", "-c", code) | ||||||
|         self.assertEqual(out, |         self.assertEqual(out, | ||||||
|                          "default::BytesWarning " |                          "default::BytesWarning " | ||||||
|                          "always::ResourceWarning " |                          "default::ResourceWarning " | ||||||
|                          "default::Warning") |                          "default::Warning") | ||||||
| 
 | 
 | ||||||
|         out = self.run_xdev("-bb", "-c", code) |         out = self.run_xdev("-bb", "-c", code) | ||||||
|         self.assertEqual(out, |         self.assertEqual(out, | ||||||
|                          "error::BytesWarning " |                          "error::BytesWarning " | ||||||
|                          "always::ResourceWarning " |                          "default::ResourceWarning " | ||||||
|                          "default::Warning") |                          "default::Warning") | ||||||
| 
 | 
 | ||||||
|         out = self.run_xdev("-Werror", "-c", code) |         out = self.run_xdev("-Werror", "-c", code) | ||||||
|         self.assertEqual(out, |         self.assertEqual(out, | ||||||
|                          "error::Warning " |                          "error::Warning " | ||||||
|                          "ignore::BytesWarning " |                          "ignore::BytesWarning " | ||||||
|                          "always::ResourceWarning " |                          "default::ResourceWarning " | ||||||
|                          "default::Warning") |                          "default::Warning") | ||||||
| 
 | 
 | ||||||
|         try: |         try: | ||||||
|  | @ -573,19 +573,6 @@ def test_xdev(self): | ||||||
|             out = self.run_xdev("-c", code) |             out = self.run_xdev("-c", code) | ||||||
|             self.assertEqual(out, "True") |             self.assertEqual(out, "True") | ||||||
| 
 | 
 | ||||||
|         # Make sure that ResourceWarning emitted twice at the same line number |  | ||||||
|         # is logged twice |  | ||||||
|         filename = support.TESTFN |  | ||||||
|         self.addCleanup(support.unlink, filename) |  | ||||||
|         with open(filename, "w", encoding="utf8") as fp: |  | ||||||
|             print("def func(): open(__file__)", file=fp) |  | ||||||
|             print("func()", file=fp) |  | ||||||
|             print("func()", file=fp) |  | ||||||
|             fp.flush() |  | ||||||
| 
 |  | ||||||
|         out = self.run_xdev(filename) |  | ||||||
|         self.assertEqual(out.count(':1: ResourceWarning: '), 2, out) |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| class IgnoreEnvironmentTest(unittest.TestCase): | class IgnoreEnvironmentTest(unittest.TestCase): | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -540,7 +540,7 @@ def _filters_mutated(): | ||||||
| 
 | 
 | ||||||
|     # resource usage warnings are enabled by default in pydebug mode |     # resource usage warnings are enabled by default in pydebug mode | ||||||
|     if dev_mode or py_debug: |     if dev_mode or py_debug: | ||||||
|         resource_action = "always" |         resource_action = "default" | ||||||
|     else: |     else: | ||||||
|         resource_action = "ignore" |         resource_action = "ignore" | ||||||
|     simplefilter(resource_action, category=ResourceWarning, append=1) |     simplefilter(resource_action, category=ResourceWarning, append=1) | ||||||
|  |  | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | warnings: In development (-X dev) and debug mode (pydebug build), use the | ||||||
|  | "default" action for ResourceWarning, rather than the "always" action, in | ||||||
|  | the default warnings filters. | ||||||
|  | @ -13,7 +13,6 @@ _Py_IDENTIFIER(argv); | ||||||
| _Py_IDENTIFIER(stderr); | _Py_IDENTIFIER(stderr); | ||||||
| _Py_IDENTIFIER(ignore); | _Py_IDENTIFIER(ignore); | ||||||
| _Py_IDENTIFIER(error); | _Py_IDENTIFIER(error); | ||||||
| _Py_IDENTIFIER(always); |  | ||||||
| _Py_static_string(PyId_default, "default"); | _Py_static_string(PyId_default, "default"); | ||||||
| 
 | 
 | ||||||
| static int | static int | ||||||
|  | @ -1208,9 +1207,9 @@ init_filters(const _PyCoreConfig *config) | ||||||
|     _Py_Identifier *resource_action; |     _Py_Identifier *resource_action; | ||||||
|     /* resource usage warnings are enabled by default in pydebug mode */ |     /* resource usage warnings are enabled by default in pydebug mode */ | ||||||
| #ifdef Py_DEBUG | #ifdef Py_DEBUG | ||||||
|     resource_action = &PyId_always; |     resource_action = &PyId_default; | ||||||
| #else | #else | ||||||
|     resource_action = (dev_mode ? &PyId_always : &PyId_ignore); |     resource_action = (dev_mode ? &PyId_default: &PyId_ignore); | ||||||
| #endif | #endif | ||||||
|     PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning, |     PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning, | ||||||
|                     resource_action)); |                     resource_action)); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner