summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2009-04-25 21:40:15 (GMT)
committerEric Smith <eric@trueblade.com>2009-04-25 21:40:15 (GMT)
commit068f06568be288b8628a4e24118503e4d9b7af1b (patch)
treee1d01c08ed08489c994e7cbf167fd703c7f2c64e /Lib
parentdfcffd40447de8e04376f8946fb71a9c16151563 (diff)
downloadcpython-068f06568be288b8628a4e24118503e4d9b7af1b.zip
cpython-068f06568be288b8628a4e24118503e4d9b7af1b.tar.gz
cpython-068f06568be288b8628a4e24118503e4d9b7af1b.tar.bz2
Issue #5835, deprecate PyOS_ascii_formatd.
If anyone wants to clean up the documentation, feel free. It's my first documentation foray, and it's not that great. Will port to py3k with a different strategy.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_ascii_formatd.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/Lib/test/test_ascii_formatd.py b/Lib/test/test_ascii_formatd.py
new file mode 100644
index 0000000..3501955
--- /dev/null
+++ b/Lib/test/test_ascii_formatd.py
@@ -0,0 +1,62 @@
+# 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
+
+
+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:
+ PyOS_ascii_formatd(byref(buf), sizeof(buf), '%+.10f',
+ c_double(10.0))
+ self.assertEqual(buf.value, '+10.0000000000')
+
+ self.assertEqual(str(w.message), 'PyOS_ascii_formatd is deprecated, '
+ 'use PyOS_double_to_string instead')
+
+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()