2009-04-25 18:40:15 -03:00
|
|
|
# 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
|
2010-03-24 14:32:58 -03:00
|
|
|
from test.test_support import check_warnings, run_unittest, import_module
|
|
|
|
|
|
|
|
# Skip tests if _ctypes module does not exist
|
|
|
|
import_module('_ctypes')
|
|
|
|
|
|
|
|
from ctypes import pythonapi, create_string_buffer, sizeof, byref, c_double
|
|
|
|
PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd
|
2009-04-25 18:40:15 -03:00
|
|
|
|
|
|
|
|
|
|
|
class FormatDeprecationTests(unittest.TestCase):
|
|
|
|
|
2010-03-23 08:07:54 -03:00
|
|
|
def test_format_deprecation(self):
|
2009-04-25 18:40:15 -03:00
|
|
|
buf = create_string_buffer(' ' * 100)
|
|
|
|
|
2010-03-23 08:07:54 -03:00
|
|
|
with check_warnings(('PyOS_ascii_formatd is deprecated',
|
|
|
|
DeprecationWarning)):
|
2009-04-25 18:40:15 -03:00
|
|
|
PyOS_ascii_formatd(byref(buf), sizeof(buf), '%+.10f',
|
|
|
|
c_double(10.0))
|
|
|
|
self.assertEqual(buf.value, '+10.0000000000')
|
|
|
|
|
2010-03-23 08:07:54 -03:00
|
|
|
|
2009-04-25 18:40:15 -03:00
|
|
|
class FormatTests(unittest.TestCase):
|
|
|
|
# ensure that, for the restricted set of format codes,
|
|
|
|
# %-formatting returns the same values os PyOS_ascii_formatd
|
2010-03-23 08:07:54 -03:00
|
|
|
def test_format(self):
|
2009-04-25 18:40:15 -03:00
|
|
|
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),
|
|
|
|
]
|
|
|
|
|
2010-03-23 08:07:54 -03:00
|
|
|
with check_warnings(('PyOS_ascii_formatd is deprecated',
|
|
|
|
DeprecationWarning)):
|
2009-04-25 18:40:15 -03:00
|
|
|
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()
|