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