mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	 6fdd3dcb6a
			
		
	
	
		6fdd3dcb6a
		
	
	
	
	
		
			
			This was originally suggested by Guido, discussed on the stdlib-sig mailing list, and given the OK by Guido directly to me. What this change essentially means is that Python has taken a policy of silencing warnings that are only of interest to developers by default. This should prevent users from seeing warnings which are triggered by an application being run against a new interpreter before the app developer has a chance to update their code. Closes issue #7319. Thanks to Antoine Pitrou, Ezio Melotti, and Brian Curtin for helping with the issue.
		
			
				
	
	
		
			63 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| # PyOS_ascii_formatd is deprecated and not called from anywhere in
 | |
| #  Python itself. So this module is the only place it gets tested.
 | |
| # Test that it works, and test that it's deprecated.
 | |
| 
 | |
| import unittest
 | |
| from test_support import check_warnings, run_unittest, cpython_only
 | |
| import warnings
 | |
| 
 | |
| 
 | |
| class FormatDeprecationTests(unittest.TestCase):
 | |
| 
 | |
|     @cpython_only
 | |
|     def testFormatDeprecation(self):
 | |
|         # delay importing ctypes until we know we're in CPython
 | |
|         from ctypes import (pythonapi, create_string_buffer, sizeof, byref,
 | |
|                             c_double)
 | |
|         PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd
 | |
|         buf = create_string_buffer(' ' * 100)
 | |
| 
 | |
|         with check_warnings() as w:
 | |
|             warnings.simplefilter('default')
 | |
|             PyOS_ascii_formatd(byref(buf), sizeof(buf), '%+.10f',
 | |
|                                c_double(10.0))
 | |
|             self.assertEqual(buf.value, '+10.0000000000')
 | |
| 
 | |
|         self.assertEqual(w.category, DeprecationWarning)
 | |
| 
 | |
| class FormatTests(unittest.TestCase):
 | |
|     # ensure that, for the restricted set of format codes,
 | |
|     # %-formatting returns the same values os PyOS_ascii_formatd
 | |
|     @cpython_only
 | |
|     def testFormat(self):
 | |
|         # delay importing ctypes until we know we're in CPython
 | |
|         from ctypes import (pythonapi, create_string_buffer, sizeof, byref,
 | |
|                             c_double)
 | |
|         PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd
 | |
|         buf = create_string_buffer(' ' * 100)
 | |
| 
 | |
|         tests = [
 | |
|             ('%f', 100.0),
 | |
|             ('%g', 100.0),
 | |
|             ('%#g', 100.0),
 | |
|             ('%#.2g', 100.0),
 | |
|             ('%#.2g', 123.4567),
 | |
|             ('%#.2g', 1.234567e200),
 | |
|             ('%e', 1.234567e200),
 | |
|             ('%e', 1.234),
 | |
|             ('%+e', 1.234),
 | |
|             ('%-e', 1.234),
 | |
|             ]
 | |
| 
 | |
|         with check_warnings():
 | |
|             for format, val in tests:
 | |
|                 PyOS_ascii_formatd(byref(buf), sizeof(buf), format,
 | |
|                                    c_double(val))
 | |
|                 self.assertEqual(buf.value, format % val)
 | |
| 
 | |
| 
 | |
| def test_main():
 | |
|     run_unittest(FormatDeprecationTests, FormatTests)
 | |
| 
 | |
| if __name__ == '__main__':
 | |
|     test_main()
 |