1 | # PyOS_ascii_formatd is deprecated and not called from anywhere in |
---|
2 | # Python itself. So this module is the only place it gets tested. |
---|
3 | # Test that it works, and test that it's deprecated. |
---|
4 | |
---|
5 | import unittest |
---|
6 | from test.test_support import check_warnings, run_unittest, import_module |
---|
7 | |
---|
8 | # Skip tests if _ctypes module does not exist |
---|
9 | import_module('_ctypes') |
---|
10 | |
---|
11 | from ctypes import pythonapi, create_string_buffer, sizeof, byref, c_double |
---|
12 | PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd |
---|
13 | |
---|
14 | |
---|
15 | class FormatDeprecationTests(unittest.TestCase): |
---|
16 | |
---|
17 | def test_format_deprecation(self): |
---|
18 | buf = create_string_buffer(' ' * 100) |
---|
19 | |
---|
20 | with check_warnings(('PyOS_ascii_formatd is deprecated', |
---|
21 | DeprecationWarning)): |
---|
22 | PyOS_ascii_formatd(byref(buf), sizeof(buf), '%+.10f', |
---|
23 | c_double(10.0)) |
---|
24 | self.assertEqual(buf.value, '+10.0000000000') |
---|
25 | |
---|
26 | |
---|
27 | class FormatTests(unittest.TestCase): |
---|
28 | # ensure that, for the restricted set of format codes, |
---|
29 | # %-formatting returns the same values os PyOS_ascii_formatd |
---|
30 | def test_format(self): |
---|
31 | buf = create_string_buffer(' ' * 100) |
---|
32 | |
---|
33 | tests = [ |
---|
34 | ('%f', 100.0), |
---|
35 | ('%g', 100.0), |
---|
36 | ('%#g', 100.0), |
---|
37 | ('%#.2g', 100.0), |
---|
38 | ('%#.2g', 123.4567), |
---|
39 | ('%#.2g', 1.234567e200), |
---|
40 | ('%e', 1.234567e200), |
---|
41 | ('%e', 1.234), |
---|
42 | ('%+e', 1.234), |
---|
43 | ('%-e', 1.234), |
---|
44 | ] |
---|
45 | |
---|
46 | with check_warnings(('PyOS_ascii_formatd is deprecated', |
---|
47 | DeprecationWarning)): |
---|
48 | for format, val in tests: |
---|
49 | PyOS_ascii_formatd(byref(buf), sizeof(buf), format, |
---|
50 | c_double(val)) |
---|
51 | self.assertEqual(buf.value, format % val) |
---|
52 | |
---|
53 | |
---|
54 | def test_main(): |
---|
55 | run_unittest(FormatDeprecationTests, FormatTests) |
---|
56 | |
---|
57 | if __name__ == '__main__': |
---|
58 | test_main() |
---|